WCF Learning Journey-Exception handling based on Servicedebug (17)

Source: Internet
Author: User
Tags stack trace connectionstrings

WCF Learning Journey-WCF Traditional exception handling (16)

second, Servicedebug-based exception handling

From the previous example, you can see that the client caught the exception, which is a precondition for us to handle the exception. To facilitate effective debugging, WCF provides the Servicedebug Service Behavior. We can do this by setting <servicedebug includeexceptiondetailinfaults= "True" /> property is set to True, if the server throws an exception, WCF simply wraps the exception and places it in SOAP response to the server's visitor. Now, let's simply modify the configuration information for hosting, add a <system.diagnostics> node, Let the client write the received error message to the Wcf.svclog file as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <configsections>    <!--for more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -    < Sectionname= "EntityFramework"type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, version=6.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirepermission= "false" /> </configsections> <EntityFramework> <defaultconnectionfactorytype= "System.Data.Entity.Infrastructure.SqlConnectionFactory, entityframework" /> <providers> <providerInvariantName= "System.Data.SqlClient"type= "System.Data.Entity.SqlServer.SqlProviderServices, entityframework.sqlserver" /> </providers> </EntityFramework> <System.ServiceModel> <Diagnostics> <messageloggingLogentiremessage= "true"Logknownpii= "false"logmalformedmessages= "true"Logmessagesatservicelevel= "true"Logmessagesattransportlevel= "true" /> <endtoendtracingpropagateactivity= "true"activitytracing= "true"messageflowtracing= "true" /> </Diagnostics> <Behaviors> <servicebehaviors> <Behaviorname= "Metadatabehavior"> <Servicemetadatahttpgetenabled= "true"Httpgeturl= "Http://127.0.0.1:8888/BookService/metadata" /> <ServicedebugIncludeexceptiondetailinfaults= "True" /> </Behavior> </servicebehaviors> </Behaviors> <Services> <Servicebehaviorconfiguration= "Metadatabehavior"name= "SCF." Wcfservice.bookservice "> <EndpointAddress= "Http://127.0.0.1:8888/BookService"binding= "Wshttpbinding"Contract= "SCF." Contracts.ibookservice " /> </Service> </Services> </System.ServiceModel> <Startup> <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.5.2 " /> </Startup> <connectionStrings> <Addname= "Testentities"connectionString= "Data source=.\sqlexpress;initial catalog=test;integrated Security=sspi"
ProviderName= "System.Data.SqlClient" /> <Addname= "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> <Sourcename= "System.ServiceModel"Switchvalue= "Warning"propagateactivity= "true"> <Listeners> <Addname= "xml" /> </Listeners> </Source> </sources> <sharedlisteners> <Addname= "xml"type= "System.Diagnostics.XmlWriterTraceListener"initializedata= "D:\wcf.svclog" /> </sharedlisteners></System.Diagnostics> </Configuration>

Now run the program again and see the results now:

With the above settings, you can view the detailed error log information in the Wcf.svclog log file. According to the following log information, it is easy to locate the problem. This is, of course, suitable for development use.

For example:

<exceptionstring>System.Data.Entity.Core.UpdateException: An error occurred while updating the entry. For more information, see inner exception.
---&amp;Gt System.Data.SqlClient.SqlException: cannot be in the object with a unique index "Ix_books_name" dbo. Books "Inserts a repeating key in the row. The repeating key value is (when Harry Met Sally 2). 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;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 Cmdbehavior, Runbehavior Runbehavior,
Boolean Returnstream, Boolean async, Int32 timeout, Task&amp;amp; task, Boolean AsyncWrite, SqlDataReader ds,
Boolean describeparameterencryptionrequest) in System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior Cmdbehavior, Runbehavior Runbehavior,
Boolean Returnstream, String method, TaskCompletionSource ' 1 completion, Int32 timeout, Task&amp;amp; task, Boolean AsyncWrite) in System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior Cmdbehavior, 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 Sy Stem. 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) at the end of the internal exception stack trace in System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update ()------ 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>

WCF learning Journey-Exception handling based on Servicedebug (17)

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.