VerificationException与WCF数据服务

问题描述:

我已经使用实体框架6.1.3使用.Net 4.5.2创建了具有实体框架的基本WCF数据服务。在数据库中只有一个表。VerificationException与WCF数据服务

下面是配置:

public class MyDataService : DataService<DataItemsDataContext> 
    { 
     // This method is called only once to initialize service-wide policies. 
     public static void InitializeService(DataServiceConfiguration config) 
     {    
      config.UseVerboseErrors = true; 
      config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; 
      config.SetEntitySetAccessRule("DataItem", EntitySetRights.All); 
     } 
    } 

当我开始这个项目,我可以看到这一点:

网址:http://localhost:49570/MyDataService.svc/

内容:

<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="http://localhost:49570/MyDataService.svc/"> 
<workspace> 
<atom:title>Default</atom:title> 
<collection href="DataItem"> 
<atom:title>DataItem</atom:title> 
</collection> 
</workspace> 
</service> 

当我选择表中,我得到这个例外:

网址:http://localhost:49570/MyDataService.svc/DataItem

内容:

<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> 
<m:code/> 
<m:message xml:lang="en-US">An error occurred while processing this request.</m:message> 
<m:innererror> 
<m:message>Operation could destabilize the runtime.</m:message> 
<m:type>System.Security.VerificationException</m:type> 
<m:stacktrace> 
at: queryable_reader(Object) 
at: System.Data.Services.Providers.ReflectionServiceProvider.GetQueryRootForResourceSet(ResourceSet container) 
at: System.Data.Services.Providers.ReflectionDataServiceProvider.GetQueryRootForResourceSet(ResourceSet resourceSet) 
at: System.Data.Services.Providers.DataServiceProviderWrapper.GetQueryRootForResourceSet(ResourceSetWrapper resourceSet) 
at: System.Data.Services.RequestUriProcessor.ComposeExpressionForEntitySet(SegmentInfo segment, IDataService service, Boolean isLastSegment, Boolean checkRights) 
at: System.Data.Services.RequestUriProcessor.ComposeExpressionForSegments(IList`1 segments, IDataService service, Boolean isCrossReferencingUri) 
at: System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service, Boolean internalQuery) 
at: System.Data.Services.DataService`1.ProcessIncomingRequestUri() 
at: System.Data.Services.DataService`1.HandleRequest() 
</m:stacktrace> 
</m:innererror> 
</m:error> 

这可能是some .Net bug

这是什么原因造成的?我应该改变什么?

+0

您是否已将此发送给Microsoft? http://connect.microsoft.com/VisualStudio。我很伤心自己碰到这个问题,不得不降级到EF 5。 – Aligned

看来它确实是一个错误(或者至少是一个限制性的特性)。

首先,我使用实体框架6.1.3作为数据库ORM,并且存在错误。

当我切换回实体框架5.0,服务正常工作。

根据这一KB article

假设您的计算机上的.NET Framework的早期版本升级到 在.NET Framework 4.5。当您使用某些第三方控件时,您可能会收到System.Security.VerificationException异常 。如果满足以下条件,则会发生此问题:

  • 第三方控件使用泛型类型。
  • 通过声明标记为安全透明的程序集来启用CLR验证程序。

我认为,EF 6.x使用泛型类型,所以它会导致问题。

解决方法是使用实​​体框架5.0。而不是实体框架6.x.

http://blogs.msdn.com/b/odatateam/archive/2013/10/02/using-wcf-data-services-5-6-0-with-entity-framework-6.aspx有一个应该可以帮助的Nuget包,但它仍然在发布前(2015年9月22日)。

从2013年10月开始:“现在有一些令人兴奋的消息:您可以最终使用WCF Data Services with Entity Framework 6+!今天,我们正在上传一个名为WCF Data Services Entity Framework Provider的新NuGet包。这NuGet包弥合WCF数据服务5.6.0和实体框架之间的差距6+