May 20, 2014

Hosting ASP .NET MVC4 and WCF Data Service together with Entity Framework 6.1.0 [Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext']


I had to work on a POC which is based on ASP .NET MVC4 and Entity Framework 6.1.0, we also want to enable WCF Data Service in the same project. I faced few issues, which took some time for me to fix, so thought of writing it here:

As usual I created a ASP.NET MVC4 project (Internet application) using VS 2012 and as expected it worked fine:


Now I installed latest entity framework using Nuget, the version got installed is EntityFramework.6.1.0



As next step I created entity data model classes based on NorthWind database.



Next created a WCF Data service and configured it to use NorthWindEntites (see below selected section in red box)



I tried running the MVC app and browsing to 'NorthWindService.svc', but got the error:


Server Error in '/' Application.

The resource cannot be found.   Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name ...


After few trial and error found that this error can be fixed by adding following line in 'RegisterRountes' function in App_start\RoutingConfig.cs

routes.IgnoreRoute("NorthWindService.svc/{*pathInfo}");

Executed again, better this time i am not getting 404 error now but got another error:

Request Error: The server encountered an error processing the request. See server logs for more details

To debug this further, I again started debugging and enabled "Common Language Runtime Exceptions" from "Exception Dialog" (Ctr + Alt + E).

Browsing to http://localhost:1234/NoerhWindService.svc cause VS 2012 to throw below exception:

Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext'

I found this blog http://blogs.msdn.com/b/odatateam/archive/2013/10/02/using-wcf-data-services-5-6-0-with-entity-framework-6.aspx, which clearly says to use Entity Framework 6.* as data provider for a Data service class, the service needs to be derived from EntityFrameworkDataService<T> intead of DataService<T>


To get 'EntityFrameworkDataService' you need to install 'WCF Data Services Entity Framework Provide' nuget package https://www.nuget.org/packages/Microsoft.OData.EntityFrameworkProvider/1.0.0-alpha2

Run the command:

Install-Package Microsoft.OData.EntityFrameworkProvider -Pre

in Nuget Package Manager Console (Tools => Nuget Package Manager => Package Manager Console)

Update your data service class to inherit from EntityFrameworkDataService.


Now tried again browsing to http://localhost/NorthWindService.svc, this time it worked and i got the metadata!!

Few thing to note, when you add WCF Data service, following versions of packages will be added:

Microsoft.Data.Edm.5.2.0
Microsoft.Data.OData.5.2.0
Microsoft.Data.Services.5.0.0
Microsoft.Data.Services.Client.5.0.0

Once you install 'WCF Data Services Entity Framework Provider' (Microsoft.OData.EntityFrameworkProvider.1.0.0-alpha2), all the above packages will be updated to new versions:

Microsoft.Data.Edm.5.6.0
Microsoft.Data.OData.5.6.0
Microsoft.Data.Services.5.6.0
Microsoft.Data.Services.Client.5.6.0

Everything is working on my local environment now. I deployed this basic application to Microsoft Azure and browsing to data service threw below error:

Could not load file or assembly 'Microsoft.Data.Services, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

See data service is still trying to use version 5.0.0.0 of Microsoft.Data.Services, but installation of 'WCF Data Services Entity Framework Provider' updated the references to Microsoft.Data.Services 5.6.0.

If you open  the .svc file [right-click it, select View Markup] then you can see it still contains following line:

<%@ ServiceHost Language="C#"
  Factory="System.Data.Services.DataServiceHostFactory,
          Microsoft.Data.Services, Version=5.0.0.0,
          Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  Service="MVC4WCFDataServiceFE5.NorthWindService" %>

Just update the version number to '5.6.0.0' as below and all started working.

<%@ ServiceHost Language="C#"
  Factory="System.Data.Services.DataServiceHostFactory,
          Microsoft.Data.Services, Version=5.6.0.0,
          Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  Service="MVC4WCFDataServiceFE5.NorthWindService" %>

No comments:

Post a Comment