Debugging WCF Data Services

When developing your service , you might run into some issues and you want to debug your service.
Imagine that you are inserting data into the store using astoria and you start getting DataServiceExceptions in your client code.
The normal error message would be ..

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="">
  <message xml:lang="en-US">An error occurred while processing this request.</message>

While debugging the service , you would need more information that just this generic error message .
To switch to the dev error mode, you can use the following config settings. 

1) Set UseVerboseErrors to true in the ServiceConfiguration
public class YoruService : DataService<YourProvider> {
public static void InitializeService(IDataServiceConfiguration config) { config.UseVerboseErrors = true; . . . . . . }
Once this mode is setup , your error messages look like this ..
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="">
  <message xml:lang="en-US">An error occurred while processing this request.</message>
  <innererror xmlns="xmlns">
    <message>An error occurred while updating the entries. See the InnerException for details.</message>
    <stacktrace>   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&#xD;
   at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)&#xD;
   at System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()&#xD;
   at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description)&#xD;
   at System.Data.Services.DataService`1.HandleRequest()</stacktrace>
      <message>Violation of PRIMARY KEY constraint 'PK_Region'. Cannot insert duplicate key in object 'dbo.Region'.&#xD;
The statement has been terminated.</message>
      <stacktrace>   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)&#xD;
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)&#xD;
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)&#xD;
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)&#xD;
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)&#xD;
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)&#xD;
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)&#xD;
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()&#xD;
   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)&#xD;
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</stacktrace>

This works well when you have a working service all prepped up and ready to go .
But , what do you do when your service won't even start ?

If the service fails to initialize due to some error , you get the generic WCF Error message,
which is seldom useful .
The error message would look like this ..

The reason you see this and not the Pretty formatted error message from astoria is that the astoria framework
never initialized the service and failed in the WCF pipeline.
To see the error at this layer , you will need to
2) Configure your Servicebehavior with the IncludeExceptionDetailInFaults attribute.
Via Code :
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]   
public class YourService : DataService<YourProvider>

Via Config :

      <service name="ServiceNamespace.ServiceClassName"  behaviorConfiguration ="DebugEnabled">
      <serviceBehaviors >
        <behavior name="DebugEnabled">
          <serviceDebug includeExceptionDetailInFaults="True"/>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

After setting this config , this is what the error  looks like ..
Happy Debugging !

Comments (10)

  1. Adron says:

    Nice write up.  One of the more concise and useful entries I’ve seen on setting up the debugging.

    I’m still somewhat surprised at how awkward this release of these tools are.  The ADO.NET Entity Data Services and even a few of the other pieces are just kind of messy still – at least it “feels” that way.

    The potential however, absolutely outweighs the slight “messy feeling”.  🙂

  2. Hi Adron ,

    While we have better tools integration coming up , Can you please elaborate what we can do to alleviate the "Messy" feeling in our next release ? I can pass the feedback onto the tools team and see if we can work with them to help get rid of some messiness 🙂


  3. Ralph says:

    Something I wouldn’t call "messy", but feels more left out is being able to define IDataServiceConfiguration from a config file right out of the box.  I’m guessing this functionality is probably slated for the next release anyway.

  4. Sean Capes says:

    This doesn’t seem to work for me. I’m trying to use AddObject and BeginSaveChanges to save a new instance, but I get the An error occured even with the UseVerbose switch to true. Updates to self contained tables work, I’m having problems with tables that have FK constraints that are implemented as NavigationProperty.

  5. ASPInsiders says:

    Thanks to all who attended the Reston Silverlight Firestarter this past Saturday. I hope everyone had

  6. bayrak says:

    Introducing such a topic you’d like to congratulate you’ve let us know. Have good work.

  7. power1style says:

    Tks for the post.

  8. Weblog says:

    How to make an error visible in WCF Data Service

  9. Zepher says:

    Thanks a ton …. you saved me some effort !!!

  10. Lisa says:

    It's really helpful.

    Thanks for the post.

Skip to main content