WCF client gets service-side exception [Custom Exception]

Source: Internet
Author: User

Introduction

After constant groping, inquiring/debugging, finally learned about the WCF client and server exception between the processing mechanism, here to record their results, for recording and sharing to the needs of the partners.

First thanks. " NET technology Group "in the group of the main" Xuan "the great help, if necessary, we also welcome to join." NET Technology Group (group number: 199281001), discuss the exchange together. NET technology (ASP. MVC4, MVC5/C#/WPF/WCF), I hope you have more technical achievements, no nonsense to say, directly began to share.

On the WCF server side of the native throw exception, I do not say, I believe that when you find this blog post, have tried many methods, one of the simplest to throw the original exception directly in [Web. config] configuration [red Word section] False to True to catch the thrown exception

Report unexpected exceptions

When you develop a WCF service, it is useful for debugging on the client program to translate all the exceptions that occur on the server, including expected and expected, into a SOAP faults message to the client.

Set the WCF service profile to True when debugging, and set to False when it is officially online

<behaviors>      <serviceBehaviors>        <behavior>          false --          < Servicemetadata httpgetenabled="true" httpsgetenabled="true" />          truefalse to avoid leaking exception information--          <servicedebug Includeexceptiondetailinfaults= "false"/>        </behavior>      </serviceBehaviors>    </behaviors>

Capturing Custom exception Handling

First, attach the custom design exception class responsecode[exception code]customexception[custom Exception]

/// <summary>    ///Exception handling Code/// </summary>     Public enumResponsecode {/// <summary>        ///Success 8200/// </summary>Success =8200,        /// <summary>        ///user does not exist 8300/// </summary>Usernotexist =8300,        /// <summary>        ///Invalid user 8301/// </summary>Userinvalid =8301,        /// <summary>        ///Password Error 8302/// </summary>Passworderror =8302,        /// <summary>        ///Token Invalid 8303/// </summary>Tokeninvalid =8303,        /// <summary>        ///non-user bound number 8304/// </summary>Notbingmobile =8304,        /// <summary>        ///Raw Password input error 8305/// </summary>Oldpassworderror =8305,        /// <summary>        ///user name already exists/// </summary>Loginnameexist =8321,        /// <summary>        ///user nickname already exists/// </summary>Nicknameexist =8322,        /// <summary>        ///Resource not found/// </summary>NotFound =8404,        /// <summary>        ///Service Internal Error 8500/// </summary>Serverinternalerror =8500,        /// <summary>        ///WCF Service Internal error/// </summary>Wcfserviceerror =8501,        /// <summary>        ///Missing Parameters/// </summary>Missparam =8600,        /// <summary>        ///Invalid parameter value/// </summary>Paramvalueinvalid =8700,        /// <summary>        ///illegal parameter (value)/// </summary>Illegalparam =8701,        /// <summary>        ///interface returned object is empty/// </summary>Resdataisempty =8800,        /// <summary>        ///data already exists, universal/// </summary>Dataexist =8900    }
Responsecode
/// <summary>    ///Logical Exception/// </summary>     Public classcustomexception:exception { Publiccustomexception (responsecode code) { This. Code =Code; }         PublicCustomexception (Responsecode Code,stringmsg) {             This. Code =Code;  This. MSG =msg; }        Private string_msg;  PublicResponsecode Code {Get;Private Set; }  Public stringMSG {Get{return_msg;} Set{_msg = value??""; } }    }
customexception

When we need the client to get the thrown exception to the WCF server, use the faultexception class

The WCF Class Library provides the FaultException class under the System.ServiceModel namespace. If the WCF service throws a FaultException object, the WCF runtime generates a SOAP fault message and passes it back to the client program.

When handling custom exceptions, it is not necessary to change the above <servicedebug includeexceptiondetailinfaults= "false" to true in/>

Service side throws exception

Client Catch exception

(Responsecode) (Enum.parse (typeof (Responsecode), ex. Code.name)//This is just a return of the captured exception information to the value of the enumerated type object, without the enumeration can be used directly with ex. Code.name

Run results

WCF client gets service-side exception [Custom Exception]

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.