Common WCF logic and Code 1. Error Handling and Configuration

Source: Internet
Author: User

Many of these things have been written .....

Here, we only need to make a backup. We can find it when using it --#

Directly copyCodeYou can run it.

Note that it can only run in WCF 4.0 .....

The following code is C # used to define error handling.

View code

     Public   Class Errorhandler: ierrorhandler
{
# If Debug
Public Static Concurrentdictionary <datetime, exception> predictionlist = New Concurrentdictionary <datetime, exception> ();
# Endif

Private Static Webbodyformatmessageproperty bodyformat = New Webbodyformatmessageproperty (webcontentformat. XML );
Private Static Httpresponsemessageproperty responsemessage = New Httpresponsemessageproperty () {statuscode = system. net. httpstatuscode. internalservererror };
Public Bool Handleerror (Exception error)
{
Return True ;
}

Public Void Providefault (Exception error, messageversion version, Ref Message fault)
{
# If Debug
Exceptionlist [datetime. Now] = error; // Just debug, don't care time conflict
# Endif
Logger. Current. writeentry (error );

VaR exception = Error As Operationexception; // Custom exception type
If (Exception! = Null )
{
// To modify the returned content, note the following:
Fault = message. createmessage (version, "" , New Operationresult (convert. toint32 (exception. errorcode), exception. tostring ()), New Datacontractjsonserializer ( Typeof (Operationresult )));
}
Else
{
Fault = message. createmessage (version, "" , New Operationresult (- 1 , Error. tostring ()), New Datacontractjsonserializer ( Typeof (Operationresult )));
}
// To modify the return type, note the following:
Fault. properties. Add (webbodyformatmessageproperty. Name, bodyformat ); // Use XML format
Fault. properties. Add (httpresponsemessageproperty. Name, responsemessage ); // Error Code
}
}

Public Class Errorhandlerextension: behaviorextensionelement, iservicebehavior
{
Public Override Type behaviortype
{
Get { Return GetType ();}
}

Protected Override Object Createbehavior ()
{
Return This ;
}

Private Ierrorhandler getinstance ()
{
Return New Errorhandler ();
}

Void Iservicebehavior. addbindingparameters (servicedescription, servicehostbase, collection <serviceendpoint> endpoints, bindingparametercollection bindingparameters)
{
}

Void Iservicebehavior. applydispatchbehavior (servicedescription, servicehostbase)
{
Ierrorhandler errorhandlerinstance = getinstance ();
Foreach (Channeldispatcher dispatcher In Servicehostbase. channeldispatchers)
{
Dispatcher. errorhandlers. Add (errorhandlerinstance );
}
}

Void Iservicebehavior. Validate (servicedescription, servicehostbase)
{

}
}

/// <Summary>
/// Just wrap errorcode and errormessage
/// </Summary>
Public Class Operationexception: system. applicationexception
{
Public Errorcode { Get ; Set ;}
Public Operationexception (errorcode)
{
This . Errorcode = errorcode;
}
Public Operationexception (errorcode, String Message)
: Base (Message)
{
This . Errorcode = errorcode;
}
}

The configuration in Web. config is as follows:

     <  Extensions  > 
< Behaviorextensions >
< Add Name = "Errorhandler" Type = "{Namespace}. validationbehaviorsection, {DLL name }" />
</ Behaviorextensions >
</ Extensions >
< Behaviors >
< Servicebehaviors >
< Behavior >
< Errorhandler />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >

You must note that providefault is used to handle exceptions (no result is returned before this method is executed)

the handleerror method does not prevent messages from being returned. It is better to write logs.

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.