2

使用实体框架数据模型公开 ADO.NET 数据服务时,我收到一条错误消息,该模型包含一个实体(称为“Case”),该实体在属性上具有内部设置器。如果我将设置器修改为公开的(使用实体设计器),则数据服务可以正常工作。

我不需要数据服务中公开的实体“Case”,因此我尝试使用 SetEntitySetAccessRule 限制公开哪些实体。这不起作用,服务端点失败并出现同样的错误。

public static void InitializeService(IDataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("User", EntitySetRights.AllRead);
}

调用 .svc 终结点时在浏览器中报告错误消息。它非常通用,显示为“请求错误。服务器在处理请求时遇到错误。有关更多详细信息,请参阅服务器日志。” 不幸的是,系统和应用程序事件日志中没有条目。

我发现了这个stackoverflow 问题,它显示了如何在服务上配置跟踪。这样做之后,跟踪日志中报告了以下 NullReferenceExceptoin 错误。

有谁知道在包含具有内部设置器的实体时如何避免此异常?

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131076</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2008-11-05T22:30:44.1523578Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{da77ee97-960f-4275-a5e7-a181c0b024b1}" />
<Execution ProcessName="WebDev.WebServer" ProcessID="6388" ThreadID="8" />
<Channel />
<Computer>MOTOJIM</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.TraceHandledException.aspx</TraceIdentifier>
<Description>Handling an exception.</Description>
<AppDomain>685a2910-19-128703978432492675</AppDomain>
<Exception>
<ExceptionType>System.NullReferenceException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Object reference not set to an instance of an object.</Message>
<StackTrace>
at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMemberMetadata(ResourceType resourceType, MetadataWorkspace workspace, IDictionary`2 entitySets, IDictionary`2 knownTypes)
at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 entitySets)
at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata()
at System.Data.Services.DataService`1.CreateProvider(Type dataServiceType, Object dataSourceInstance, DataServiceConfiguration&amp; configuration)
at System.Data.Services.DataService`1.EnsureProviderAndConfigForRequest()
at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody)
at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<ExceptionString>System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMemberMetadata(ResourceType resourceType, MetadataWorkspace workspace, IDictionary`2 entitySets, IDictionary`2 knownTypes)
   at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 entitySets)
   at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata()
   at System.Data.Services.DataService`1.CreateProvider(Type dataServiceType, Object dataSourceInstance, DataServiceConfiguration&amp; configuration)
   at System.Data.Services.DataService`1.EnsureProviderAndConfigForRequest()
   at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody)
   at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</ExceptionString>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>
4

4 回答 4

2

我最近遇到了类似的问题,我的解决方法是将 EntitySetAccessRule 的名称更改为“*”,如下所示:

public static void InitializeService(DataServiceConfiguration config)
{
  config.SetEntitySetAccessRule("*", EntitySetRights.All);
  config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
于 2010-06-22T04:51:37.723 回答
0

我有一个类似的问题,它是由错误的类传递给 DataService 引起的。您需要传递数据模型的数据实体类。

public class WebDataService : DataService< DataModel.DataEntities >
于 2009-01-07T18:39:32.007 回答
0

我遇到过由 EF6 引起的类似问题。

根据这篇文章改变

public class WebDataService : DataService< DataModel.DataEntities >

public class WebDataService : EntityFrameworkDataService< DataModel.DataEntities >

应该有帮助

于 2014-02-19T04:04:10.580 回答
0

我也遇到了同样的问题,结果发现解决方案非常简单:虽然我的表名和生成的实体对象是单数的“Order”,但在这种情况下我需要将其称为“Orders”。仍然让我头疼。只是EDM标准。

config.SetEntitySetAccessRule("Orders", EntitySetRights.All);

希望这可以帮助。

于 2011-05-16T14:28:25.043 回答