Documente Academic
Documente Profesional
Documente Cultură
Windows®
Communication
Foundation
Module 6: Handling Errors
• Relating .NET Framework Exceptions to
Service-Level Faults
• Using Faults in a Service
• Faults in Metadata
Relating Exceptions to Services
Exceptions are a common way to indicate errors
Service
Contract exception
errors handling
Service SOAP
Contract Fault SOAP
message
Structure of a SOAP Fault
<s:Envelope xmlns:s ="…/soap-envelope" xmlns:a ="…/addressing">
<s:Header>
<a:Action s:mustUnderstand ="1">
http://www.w3.org/2005/08/addressing/soap/fault
</a:Action>
<a:RelatesTo>
urn:uuid:dd129ffe-a8ff-4a70-ad6f-ad48085e94e8
</a:RelatesTo>
...
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value> s:Sender </s:Value>
</s:Code>
<s:Reason>
<s:Text xml:lang ="en-US"> ERROR INFO </s:Text>
</s:Reason>
<s:Detail>APPLICATION-SPECIFIC INFORMATION</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>
Faults in Metadata
[OperationContract]
[FaultContract(typeof(BankService.NoFunds))]
void Withdraw(string account, decimal amount);
Metadata
Generation
<wsdl:operation name="Withdraw">
<wsdl:input
wsaw:Action="http://tempuri.org/MyContract/Withdraw“
message="tns:MyContract_Withdraw_InputMessage"/>
<wsdl:output
wsaw:Action="http://tempuri.org/MyContract/WithdrawResponse“
message="tns:MyContract_Withdraw_OutputMessage"/>
<wsdl:fault
wsaw:Action="http://tempuri.org/MyContract/WithdrawNoFundsFault“
name=“NoFundsFault“
message="tns:MyContract_Withdraw_NoFundsFault_FaultMessage"/>
</wsdl:operation>
Lesson: Using Faults in a Service
• Designing Service Error Handling
Inform Inform
Handle
caller caller
Deciding What Information to Include in Faults
Different information
for different
constituencies
Client-Side
Support Team
Typed and Untyped Faults
[DataContract(Namespace="http://myuri.org/Simple")]
public class AccountOverdrawnFault
{ DataContract
private string accountNo; for use as a
typed fault
[DataMember]
public string AccountNo
{
get { return accountNo; }
set { accountNo = value; }
}
...
}
Using a Typed Fault
Typed
fault
Ongoing
Domain
communication
error
Untyped
fault
Expected Ongoing
technical error communication
No
action
Unhandled Faulted
Unexpected
exception channel
technical error
Applying Consistent Error Handling
First option for consistent error handling is a big outer try block
<behavior name="bankServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
...
</behavior>
• Communication Exceptions
CommunicationException
TimeoutException EndpointNotFound
ProtocolException
CommunicationObjectFaultedException
try
{
bankServiceProxy.Withdraw("ABC123", 2000.00);
...
}
catch (FaultException<AccountOverdrawnFault> ex);
{
AccountOverdrawnFault fault = ex.Detail;
try
{
bankServiceProxy.Withdraw("ABC123", 2000.00);
...
}
catch (FaultException ex);
{
Console.WriteLine("Service fault: {0}.", ex.Reason);
}
One-way operations
Duplex
Logon information
Virtual machine 6461A-LON-DEV-06
Password Pa$$w0rd
• Best Practices