WCF 数据服务的 VerificationException

VerificationException with WCF Data Services

我使用 .Net 4.5.2 和 Entity Framework 6.1.3 创建了一个基本的 WCF 数据服务 Entity Framework。数据库中只有一个table.

配置如下:

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);
        }
    }

当我启动项目时,我可以看到:

URL: 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>

当我选择 table 时,出现以下异常:

URL: 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

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

看来这确实是一个错误(或者至少是一个限制性功能)。

首先,我使用Entity Framework6.1.3作为数据库ORM,报错

当我切换回 Entity Framework 5.0 时,服务正常。

据此KB article:

Assume that you upgrade an earlier version of the .NET Framework to the .NET Framework 4.5 on a computer. When you use some third-party controls, you may receive a System.Security.VerificationException exception. This issue occurs if the following conditions are true:

  • The third-party controls use the generic types.
  • The CLR verifier is enabled by declaring an assembly that is marked as security-transparent.

我想,EF 6.x 使用泛型类型,所以它导致了问题。

解决方法是使用Entity Framework 5.0。而不是 Entity Framework 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 月起:“现在有一些激动人心的消息:您终于可以在 Entity Framework 6+ 上使用 WCF 数据服务了!今天我们上传了一个名为 WCF Data Services Entity Framework Provider 的新 NuGet 包。这NuGet 包弥合了 WCF 数据服务 5.6.0 和 Entity Framework 6+

之间的差距