我第一次设置OData服务,我当然遇到了一些问题......
问题是我无法运行服务,我不断收到“请求错误”。
我有researched问题是什么,我发现一个常见问题是访问规则输入错误。所以我尝试用奇异名称,多个名字来解决这个问题,我也试过typeof(Post).getType().Name
这是我的代码。我希望你能帮助我,我已经被困了几个小时。
public class ODataService : DataService<Entity>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService( DataServiceConfiguration config )
{
//config.SetEntitySetAccessRule( "Users", EntitySetRights.All );
//config.SetEntitySetAccessRule( "Posts", EntitySetRights.All );
//config.SetEntitySetAccessRule( "Albums", EntitySetRights.All );
config.SetEntitySetAccessRule( "*", EntitySetRights.AllRead );
config.SetServiceOperationAccessRule( "*", ServiceOperationRights.AllRead );
//config.SetServiceOperationAccessRule( "GetPosts", ServiceOperationRights.AllRead );
config.UseVerboseErrors = true;
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
[WebGet]
public IQueryable<Post> GetPosts()
{
return CurrentDataSource.Posts.AsQueryable();
}
}
我的EntityFramework类的结构(db first)
实体类的方法和成员。这里的实体拼写为复数形式。
这是我的Web.config:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="Entity" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="data source=XXX;Initial Catalog=XXX;persist security info=True;user id=XXX;password=XXX;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime requestValidationMode="4.5" targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<pages controlRenderingCompatibilityVersion="4.5" />
<machineKey compatibilityMode="Framework45" />
</system.web>
<system.serviceModel>
<services>
<service name="LinkIT.Core.OData.ODataService" behaviorConfiguration ="DebugEnabled">
</service>
</services>
<behaviors>
<serviceBehaviors >
<behavior name="DebugEnabled">
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
详细的错误消息:
服务器在处理请求时遇到错误。例外 消息是'值不能为空。参数名称: propertyResourceType”。请参阅服务器日志以获取更多详例外 堆栈跟踪是:
at System.Data.Services.WebUtil.CheckArgumentNull [T](T value,String parameterName)at System.Data.Services.Providers.ResourceProperty..ctor(String name, ResourcePropertyKind类,ResourceType propertyResourceType)at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMemberMetadata(的ResourceType resourceType,IProviderMetadata workspace,IDictionary
2 knownTypes, PrimitiveResourceTypeMap primitiveResourceTypeMap) at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary
2 knownTypes,IDictionary2 childTypes, IDictionary
2 entitySets)at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() 在System.Data.Services.Providers.BaseServiceProvider.LoadMetadata() 在 System.Data.Services.DataService1.CreateMetadataAndQueryProviders(IDataServiceMetadataProvider& metadataProviderInstance, IDataServiceQueryProvider& queryProviderInstance, BaseServiceProvider& builtInProvider, Object& dataSourceInstance) at System.Data.Services.DataService
1.CreateProvider()at System.Data.Services.DataService1.HandleRequest() at System.Data.Services.DataService
1.ProcessRequestForMessage(流 messageBody)在SyncInvokeProcessRequestForMessage(Object,Object [], 对象[])在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象 实例,对象[]输入,对象[]&amp;输出) System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
答案 0 :(得分:10)
WCF数据服务团队confirms - 这是使用枚举时尚未支持的确切错误(尚不支持)。
删除Enum类型(或使用他们建议的解决方法并在枚举属性周围使用包装器),这应该消失。