WCF DataService調試時的一些問題

最近在使用WCF DataService 正好看到一篇文章 大概翻譯一下http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspxhtml

在使用WCF DataService進行開發時,有時會遇到一些問題app

好比async

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <code></code> <message xml:lang="en-US">An error occurred while processing this request.</message> </error>



可是這種籠統的錯誤提示 顯然對於調試毫無幫助 因此咱們須要更多的調試選項ide

1)在ServiceConfigruation中 將UserVerboseErrors設爲truethis

代碼以下spa

public class YoruService : DataService<YourProvider> {
   public static void InitializeService(IDataServiceConfiguration config) {       
       config.UseVerboseErrors = true;
        . . . . . .        
    }
......
}

這樣你可能會看到這樣的錯誤提示:翻譯

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code></code>
  <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>
    <type>System.Data.UpdateException</type>
    <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>
    <internalexception>
      <message>Violation of PRIMARY KEY constraint 'PK_Region'. Cannot insert duplicate key in object 'dbo.Region'.&#xD;
The statement has been terminated.</message>
      <type>System.Data.SqlClient.SqlException</type>
      <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>
    </internalexception>
  </innererror>
</error>

可是若是服務都沒法啓動,提示信息可能以下debug

WCF_Generic_Error

說明WCF DataService 徹底沒有啓動 爲了看到更多錯誤信息 調試

2)在ServiceBehavior中設置 IncludeExceptionDetailInFaults屬性code

Via Code :
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]   
public class YourService : DataService<YourProvider>

 

Via Config :

 

<system.serviceModel> <services> <service name="ServiceNamespace.ServiceClassName" behaviorConfiguration ="DebugEnabled"> </service> </services> <behaviors> <serviceBehaviors > <behavior name="DebugEnabled"> <serviceDebug includeExceptionDetailInFaults="True"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> </system.serviceModel>
這樣一來 錯誤提示就會變成

WCF_Detailed_Error

相關文章
相關標籤/搜索