Writing your first Odata service and debugging problems. UseVerboseErrors, DataServiceKey,IncludeExceptionDetailInFaults

Posted on

Today I wanted to created simple out of a box Odata service using WCF Dataservice. My entry page was http://www.odata.org/ I watched couple of tutorials how to add service to mvc project.

very simple and straight forward, but I failed getting message  “ An error occurred while processing this request. ”. While trying to debug it by normal means and looking into logs bring me nothing which can help solve issue.

I searched and find nice post http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx explaining how to get error details. essentially you need just two steps:

First set config.UseVerboseErrors = true in your service cs file

   1:   // This method is called only once to initialize service-wide policies.
   2:          public static void InitializeService(DataServiceConfiguration config)
   3:          {
   4:              // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
   5:              // Examples:
   6:              config.UseVerboseErrors = true;
   7:              config.SetEntitySetAccessRule("ExampleClass", EntitySetRights.AllRead);
   8:              // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
   9:              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  10:          }

Second: attribute your class with ServiceBehavior(IncludeExceptionDetailInFaults = true)]

[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
  public class ExampleService : DataService<  ExampleContext >

Ok once this has been changed I got nice error:

The server encountered an error processing the request. The exception message is ‘On data context type ‘ExampleContext’, there is a top IQueryable property ‘Examples’ whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.’. See server logs for more details. The exception stack trace is:

at System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 childTypes, IDictionary`2 entitySets) at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() at System.Data.Services.DataService`1.CreateProvider() at System.Data.Services.DataService`1.HandleRequest() 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.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)

 

After searching a little bit I found that you need attribute your entity with primary key attribute DataServiceKey

 

[DataServiceKey("Id")]
    public class ExampleClass
    {

         public string Name { get; set; }
         public string Id { get; set; }
    }

Tags: , ,