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