Friday, August 10, 2012

WCF Data Services - OData - Initial errors developers encounters

When authoring WCF Data Services - OData, lot of developers tries to view it in the browser during several stages of developement. Mostly they will come across the following error at initial stages.

The server encountered an error processing the request. See  server logs for more details.

When we think of creating WCF Data services, its better to have the following minimal understanding.

WCF Data services / OData service : A .Net framework component which is used to create services that uses Open Data Protocol (OData). This service exposes data through the endpoint which allows to access the data.

OData client libraries : Though not strictly required, several client libraries available for easy and effective access of OData format. Based on the client applications we create, we can make use of the respective client libraries. For example we have separate libraries for .Net, java, javascript, Silverlight, etc.

OData data model : Makes use of the exiting .Net member called Entity Data Model(EDM).  The data to be represented need to be modeled using EDM.

OData Protocol : This protocol is REST based. Defines  how the data model will be represented over the wire - either in XML based format defined by ATOM or in JSON format.

Now, let us look at the cause for the issue.

In the InitializeService method of the svc.cs file, do ensure whether the SetEntitySetAccessRule is specified for the respective entity and most important, the name of the entity set should be pluralized (if opted while creating the entity data model).

Snippet 1


// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
    // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
    // Examples:
    config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}

References

Creative Commons License
This work by Tito is licensed under a Creative Commons Attribution 3.0 Unported License.