Silverlight and WCF error handling: Server not found (2)

Source: Internet
Author: User

The previous article briefly introduced the configuration application.ProgramLet the client obtain the soap exception thrown by WCF, but the soap exception information only contains innerexception, message, and stacktrace. However, in actual conditions, we

You may need to provide more detailed information. In WCF, you can send two types of soap errors that are not declared and declared to the client.ArticleIn fact, an un-declared soap error is used.

Based on the previous article, this article demonstrates how to declare errors in service operations through faultcontractattribute provided by WCF:

1. Customize a custom type for the error contract

 
[Datacontract]

Public ClassDataaccessfault

 
{

[Datamember]

 
Public StringExceptionmessage {Get; set ;}

 
 

 
[Datamember]

Public StringExceptioncode {Get; set ;}

 
}

2. Modify the implementation of service operations

 
[Operationcontract]

[Faultcontract (Typeof(Dataaccessfault)]

Public VoidDowork ()

 
{

 
Throw NewFaultexception <dataaccessfault> (NewDataaccessfault ()

{

 
Predictionmessage ="An exception occurred",

 
Predictioncode ="Cs2011"

});

 
}

This type of exception is thrown in the operation, and the contract type is added at the same time.

3. After the client updates the reference, modify it.Code

 
Faultexception <dataaccessfault> faultexception = E. ErrorAsFaultexception <dataaccessfault>;

MessageBox. Show (faultexception. Detail. exceptioncode +":"+

Faultexception. Detail. exceptionmessage );

The final effect is as follows:

 

This article uses the error contract to pass a strong type of error message to the Silverlight client. In actual situations, you can choose as needed.

 

Download Code: wcfexceptionlesson1.rar

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.