WCF learning journey-ServiceDebug-based Exception Handling (17th), wcfservicedebug

Source: Internet
Author: User

WCF learning journey-ServiceDebug-based Exception Handling (17th), wcfservicedebug
WCF learning journey-traditional exception handling in WCF (16)

 

Ii. ServiceDebug-based Exception Handling

From the previous example, we can see that the client has caught exceptions, which is the prerequisite for us to handle exceptions. To facilitate effective debugging, WCF provides ServiceDebug Service Behavior. We can set the <serviceDebug includeExceptionDetailInFaults = "True"/> attribute to true. If the server throws an exception, WCF will simply wrap this exception and place it in the Soap Response to the visitor on the server. Now, let's simply modify the Hosting configuration information and add a <system. diagnostics> node so that the client can write the received error information to the wcf. svclog file, as shown below:

<?xml version="1.0" encoding="utf-8"?><configuration>  <configSections>    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <system.serviceModel> <diagnostics> <messageLogging logEntireMessage="true" logKnownPii="false" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" /> <endToEndTracing propagateActivity="true" activityTracing="true" messageFlowTracing="true" /> </diagnostics> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8888/BookService/metadata" /> <serviceDebug includeExceptionDetailInFaults="True" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="SCF.WcfService.BookService"> <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding" contract="SCF.Contracts.IBookService" /> </service> </services> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <connectionStrings> <add name="TestEntities" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=SSPI"
providerName="System.Data.SqlClient" /> <add name="Entities" connectionString="metadata=res://*/BookModel.csdl|res://*/BookModel.ssdl|res:
//*/BookModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;
initial catalog=Test;integrated security=SSPI;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true"> <listeners> <add name="xml" /> </listeners> </source> </sources> <sharedListeners> <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="D:\wcf.svclog" /> </sharedListeners></system.diagnostics> </configuration>

 

Run the program again and check the running result:

After the preceding settings, you can view detailed error log information in the wcf. svclog log file. Based on the following log information, you can easily locate the problem. Of course, this is suitable for development.

For example:

 

<ExceptionString> System. Data. Entity. Core. UpdateException: An error occurred while updating the entry. For more information, see internal exceptions.
--- & Amp; gt; System. Data. SqlClient. SqlException: duplicate keys cannot be inserted in the "dbo. Books" object with the unique index "IX_Books_name. The duplicate key value is (When Harry Met Sally 2 ). The statement has been terminated. In System. data. sqlClient. sqlConnection. onError (SqlException exception, Boolean breakConnection, Action '1 wrapCloseInAction) in System. data. sqlClient. sqlInternalConnection. onError (SqlException exception, Boolean breakConnection, Action '1 wrapCloseInAction) in System. data. sqlClient. tdsParser. throwExceptionAndWarning (TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) in System. data. sqlClient. tdsParser. tryRun (RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean & amp; dataReady) in System. data. sqlClient. sqlDataReader. tryConsumeMetaData () in System. data. sqlClient. sqlDataReader. get_MetaData () in System. data. sqlClient. sqlCommand. finishExecuteReader (SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) in System. data. sqlClient. sqlCommand. runExecuteReaderTds (CommandBehavior describehavior, RunBehavior runBehavior,
Boolean returnStream, Boolean async, Int32 timeout, Task & amp; task, Boolean asyncWrite, SqlDataReader ds,
Boolean describeParameterEncryptionRequest) in System. Data. SqlClient. SqlCommand. RunExecuteReader (CommandBehavior describehavior, RunBehavior runBehavior,
Boolean returnStream, String method, TaskCompletionSource '1 completion, Int32 timeout, Task & amp; task, Boolean asyncWrite) in System. data. sqlClient. sqlCommand. runExecuteReader (CommandBehavior describehavior, RunBehavior runBehavior, Boolean returnStream, String method) in System. data. sqlClient. sqlCommand. executeReader (CommandBehavior behavior, String method) in System. data. sqlClient. sqlCommand. executeDbDataReader (CommandBehavior behavior) in System. data. common. dbCommand. executeReader (CommandBehavior behavior) in System. data. entity. infrastructure. interception. dbCommandDispatcher. & amp; lt; Reader & amp; gt; B _ c (DbCommand t, DbCommandInterceptionContext '1 c) in System. data. entity. infrastructure. interception. internalDispatcher '1. dispatch [TTarget, TInterceptionContext, TResult]
(TTarget target, Func '3 operation, TInterceptionContext interceptionContext, Action '3 executing, Action '3 executed) in System. data. entity. infrastructure. interception. dbCommandDispatcher. reader (DbCommand command, DbCommandInterceptionContext interceptionContext) in System. data. entity. internal. interceptableDbCommand. executeDbDataReader (CommandBehavior behavior) in System. data. common. dbCommand. executeReader (CommandBehavior behavior) in System. data. entity. core. mapping. update. internal. dynamicUpdateCommand. execute (Dictionary '2 identifierValues, List '1 generatedValues) in System. data. entity. core. mapping. update. internal. updateTranslator. update () --- end of the internal exception stack trace --- in System. data. entity. core. mapping. update. internal. updateTranslator. update () in System. data. entity. core. entityClient. internal. entityAdapter. & amp; lt; Update & amp; gt; B _ 2 (UpdateTranslator ut) in System. data. entity. core. entityClient. internal. entityAdapter. update [T] (T noChangesResult, Func '2 updateFunction) in System. data. entity. core. entityClient. internal. entityAdapter. update () in System. data. entity. core. objects. objectContext. & amp; lt; SaveChangesToStore & amp; gt; B _ 35 () in System. data. entity. core. objects. objectContext. executeInTransaction [T] (Func '1 func, IDbExecutionStrategy executionStrategy,
Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) in System. Data. Entity. Core. Objects. ObjectContext. SaveChangesToStore (SaveOptions options, IDbExecutionStrategy executionStrategy,
Boolean startLocalTransaction) in System. data. entity. core. objects. objectContext. & amp; lt; & amp; gt; c _ DisplayClass2a. & amp; lt; SaveChangesInternal & amp; gt; B _ 27 () in System. data. entity. sqlServer. defaultSqlExecutionStrategy. execute [TResult] (Func '1 operation) in System. data. entity. core. objects. objectContext. saveChangesInternal (SaveOptions options, Boolean executeInExistingTransaction) in System. data. entity. core. objects. objectContext. saveChanges (SaveOptions options) in System. data. entity. internal. internalContext. saveChanges () </ExceptionString>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.