当使用实体框架数据模型公开ADO.NET数据服务时,我收到一条错误消息,该数据模型包含一个属性上有内部setter的实体(称为“Case”)。如果我将setter修改为公共(使用实体设计器),则数据服务可以正常工作。
我不需要在数据服务中公开实体“Case”,因此我尝试使用SetEntitySetAccessRule限制公开哪些实体。这不起作用,服务端点失败并出现相同的错误。
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("User", EntitySetRights.AllRead);
}
调用.svc端点时,在浏览器中报告错误消息。它非常通用,并显示“请求错误。服务器遇到处理请求的错误。有关更多详细信息,请参阅服务器日志。“不幸的是,系统和应用程序事件日志中没有条目。
我发现这个stackoverflow question显示了如何在服务上配置跟踪。执行此操作后,跟踪日志中报告了以下NullReferenceExceptoin错误。
当包含具有内部setter的实体时,是否有人知道如何避免此异常?
<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& 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[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& 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& 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[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</ExceptionString>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>
答案 0 :(得分:2)
我最近遇到了类似的问题,我的案例中的修复程序将EntitySetAccessRule的名称更改为“*”,如下所示:
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
答案 1 :(得分:0)
我遇到了类似的问题,这是由于将错误的类传递给DataService引起的。您需要传递数据模型的数据实体类。
public class WebDataService : DataService< DataModel.DataEntities >
答案 2 :(得分:0)
我有同样的问题,解决方案结果非常简单:虽然我的表名和生成的实体对象是单数“Order”,但我需要在此上下文中将其称为“Orders”。仍然让我头脑发热。只是EDM标准。
config.SetEntitySetAccessRule("Orders", EntitySetRights.All);
希望这有帮助。
答案 3 :(得分:0)
我有类似的问题由EF6引起。
根据this article更改
public class WebDataService : DataService< DataModel.DataEntities >
到
public class WebDataService : EntityFrameworkDataService< DataModel.DataEntities >
应该帮助