WCF Study Notes (1)-specify fault behaviors for the service

Source: Internet
Author: User

This post demonstrates how to use faultcontractattribute to specify a soap error and combine it with a generic faultexception <custom class> to encapsulate custom error messages and send them back to the client. This post provides sample downloads for WCF + ASP. NET 3.5.

-------------------------------------------------
Example of this postCodeDownload point:
Http://files.cnblogs.com/WizardWu/090103.zip
(In this example, Visual Studio 2008 + SP1 is required and no database is required)
-------------------------------------------------

With vs 2008 + SP1, double-click wcfservicelibrary1.sln to open solution and press F5 to execute the example. The picture of 1 is displayed. When you enter a number in textbox and press button, the WCF Service uses Int. the parse method converts the input string to a number. When you enter a non-number, a transformation error occurs in WCF, then, a faultexception <tdetial> is returned in service1.cs.


Figure 1

Faultcontract attribute can declare an operation as an output error and specify the type used to encapsulate fault details (note that fault is not equal to exception ). In the iservice1.cs example downloaded in this post, we will use the getdata method in the interface and faultcontract attribute to declare these errors to be returned when a soap error is encountered, as shown below:

Iservice1.cs
Namespace Wcfservicelibrary1
{
[Datacontract (namespace =   " Http://wizardwu.cnblogs.com/ " )]
Public   Class Faultinfo
{
[Datamember ()]
Public   String Reason =   Null ; // Customize error messages to be returned
}

[Servicecontract]
Public InterfaceIservice1
{
[Operationcontract]
[Faultcontract (Typeof(Faultinfo)]
IntGetdata (StringValue );
}
}

 

In the code above, we also see a custom class faultinfo, which is used to provide relevant error information and is used in the service1 class that implements the iservice1 interface, encapsulate custom error information in the generic faultexception class, that is, faultexception <Faultinfo> To throw the information to the client's code-behind when a transformation error occurs.Program(Default. aspx. CS), as follows:

Service1.cs
Namespace Wcfservicelibrary1
{
Public   Class Service1: iservice1
{
Public   Int Getdata ( String Value)
{
Int Intreturn =   0 ;

Try
{
// Deliberately uses the old Int. parse () as the string transformation number, instead of the new Int. tryparse ()
Intreturn =   Int . Parse (value );
}
Catch
{
Faultinfo fi =   New Faultinfo (); // Instance from external class
Fi. Reason =   " The number you entered: "   + Value +   " <Br> invalid. enter a new one. " ; // Customize error messages to be returned

//Faultexception <tdetail> exception is used to intercept the soap of a specific contract in a WCF client application.
Throw NewFaultexception<Faultinfo>(FI );

// MS-help: // Ms. msdnqtr. v90.cht/fxref_system.servicemodel/html/bb03a521-d12b-606f-5f62-dca0c1fe23aa.htm
// Faultexception <tdetail> generic class
// In the client application, it is used to block soap errors specified in contract mode.
}

ReturnIntreturn;//The number thrown back after the transformation of WCF
}
}
}

 

Of course, you can also use the legacy non-generic faultexception class or faultexception <string> to directly assign custom error messages, as shown below:
Throw new faultexception <String> ("The number you entered is invalid ");

In addition, the custom getdata method, MEP (message exchange patterns) cannot be declared as one-way, as follows:
[Operationcontract (Isoneway = true)]
[Faultcontract (typeof (faultinfo)]
Int getdata (string value );

Otherwise, the client project will cause the "unable to update service reference" error when referencing this WCF Service. Therefore, we use the default value "Request/response" of MEP 」.

Reference the client ASP. Net project of this WCF Service. Its default. aspx. CS code is as follows:

Default. aspx. CS
Using System. servicemodel;
Using Wcfservicelibrary1;

Public Partial Class_ Default: system. Web. UI. Page
{
Protected VoidPage_load (ObjectSender, eventargs E)
{
}

Protected VoidButton#click (ObjectSender, eventargs E)
{
This. Transferwcf ();
}

Private   Void Transferwcf ()
{
Servicereference1.service1client prox =   New Servicereference1.service1client ();
Int I =   0 ;

Try
{
I = Prox. getdata (textbox1.text );
Response. Write (I );
}
Catch (Faultexception < Faultinfo > Fault)
{
Response. Write (fault. Detail. Reason );
}
Catch (Exception ex)
{
Response. Write (ex. tostring ());
}
}

}

 

The detail attribute in the code above is unique to the faultexception <tdetail> generic class and contains error details, but the old non-generic faultexception class does not.

After the solution is built, If you browse the WSDL, you will find the fault contract information in it.

-----------------------------------

Reference books:

[1] Microsoft. NET Framework 3.5-Windows Communication Foundation, MCTs Exam 70-503 Training Kit
Http://www.microsoft.com/learning/en/us/Books/12486.aspx
Http://www.amazon.com/MCTS-Self-Paced-Training-70-503-PRO-Certification/dp/0735625654

-----------------------------------

Reference file:

[2] msdn Library

Faultcontractattribute class:
Http://msdn.microsoft.com/zh-cn/library/system.servicemodel.faultcontractattribute.aspx

Faultexception <tdetail> generic class:
Http://msdn.microsoft.com/zh-cn/library/ms576199.aspx

Faultexception <tdetail> detail attributes:
Http://msdn.microsoft.com/zh-cn/library/ms575596.aspx

[3] WCF learning ---- my first WCF Program
Http://www.cnblogs.com/ASPNET2008/archive/2008/06/01/1211613.html

-----------------------------------

 

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.