WCF serializes exception to SOAP faults before reporting the exception information to the client.this is because exceptions are not allowed to be passed through a service channel.
SOAP faults are in xml format and are platform independent.
A typical SOAP fault contains FaultCode, FaultReason and Detail elements.
The detail element can be used to include any custom xml.
SOAP faults are formatted based on SOAP1.1 or SOAP1.2 specifications depending on the binding .
BasicHttpBinding uses SOAP1.1 whereas other bindings use SOAP1.2.
Following xml is the response SOAP xml if any unhandled exception occur in WCF service.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher" xmlns="">a:InternalServiceFault</faultcode>
<faultstring xml:lang="en-GB" xmlns="">The server was unable to process the request
due to an internal error. For more information about the error,
either turn on IncludeExceptionDetailInFaults
(either from ServiceBehaviorAttribute or from the <serviceDebug>configuration behavior)
on the server in order to send the exception information back to the client,
or turn on tracing as per the Microsoft .NET Framework 3.0 SDK
documentation and inspect the server trace logs.
</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
In above xml, actual reason is not serialize in SOAP fault. if you want to show actual reason allow includeExceptionDetailInFaults in config file.
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
Now the response xml will be as follows.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher" xmlns="">a:InternalServiceFault
</faultcode>
<faultstring xml:lang="en-GB" xmlns="">Attempted to divide by zero.
</faultstring>
<detail xmlns="">
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"></HelpLink>
<InnerException i:nil="true"></InnerException>
<Message>Attempted to divide by zero.</Message>
<StackTrace>
at HelloWCFService.Service1.Divide(Int32 val1, Int32 val2) in C:\Users\npdrsaini\documents\visual studio 2010\Projects\HelloService\HelloWCFService\Service1.svc.cs:line 33at SyncInvokeDivide(Object , Object[], Object[])at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[]inputs, Object[]&outputs)at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<Type>System.DivideByZeroException</Type>
</ExceptionDetail>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
Above message format is SOAP1.1 if you want to see SOAP1.2 some changes needed.
change the binding from basicHttpBinding to wsHttpBinding.
By default message Security is turned on for wsHttpBinding.Set the security mode for wsHttpBinding to None,so we could view the SOAP1.2 fault message.
<bindings>
<wsHttpBinding>
<binding>
<security mode="None"></security>
</binding>
</wsHttpBinding>
</bindings>
SOAP1.2 message format for same response will be as follows :
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action>
<a:RelatesTo>urn:uuid:47e00c8f-ca12-4e05-8321-46ff8fa69c79</a:RelatesTo>
<ActivityId CorrelationId="bba96711-ee01-4f2c-a5b2-f53af7975901" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">5e2bc13f-f61c-4ba9-8b1b-328db1823108</ActivityId>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Receiver</s:Value>
<s:Subcode>
<s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-GB">Attempted to divide by zero.</s:Text>
</s:Reason>
<s:Detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"></HelpLink>
<InnerException i:nil="true"></InnerException>
<Message>Attempted to divide by zero.</Message>
<StackTrace>
at HelloWCFService.Service1.Divide(Int32 val1, Int32 val2) in C:\Users\npdrsaini\documents\visual studio 2010\Projects\HelloService\HelloWCFService\Service1.svc.cs:line 33at SyncInvokeDivide(Object , Object[], Object[])at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[]inputs, Object[]&outputs)at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<Type>System.DivideByZeroException</Type>
</ExceptionDetail>
</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>
Code implementation :
[ServiceContract]
public interface IService1
{
[OperationContract]
float Divide(int val1,int val2);
}
public class Service1 : IService1
{
public float Divide(int val1, int val2)
{
return val1 / val2;
}
}
Client side code :
public partial class TestClient : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_getEmp_Click(object sender, EventArgs e)
{
Service1Client ocjClient = new Service1Client("WSHttpBinding_IService1");
float result = ocjClient.Divide(10, 0);
}
}
Related Post :
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF
Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault
SOAP faults are in xml format and are platform independent.
A typical SOAP fault contains FaultCode, FaultReason and Detail elements.
The detail element can be used to include any custom xml.
SOAP faults are formatted based on SOAP1.1 or SOAP1.2 specifications depending on the binding .
BasicHttpBinding uses SOAP1.1 whereas other bindings use SOAP1.2.
Following xml is the response SOAP xml if any unhandled exception occur in WCF service.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher" xmlns="">a:InternalServiceFault</faultcode>
<faultstring xml:lang="en-GB" xmlns="">The server was unable to process the request
due to an internal error. For more information about the error,
either turn on IncludeExceptionDetailInFaults
(either from ServiceBehaviorAttribute or from the <serviceDebug>configuration behavior)
on the server in order to send the exception information back to the client,
or turn on tracing as per the Microsoft .NET Framework 3.0 SDK
documentation and inspect the server trace logs.
</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
In above xml, actual reason is not serialize in SOAP fault. if you want to show actual reason allow includeExceptionDetailInFaults in config file.
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
Now the response xml will be as follows.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher" xmlns="">a:InternalServiceFault
</faultcode>
<faultstring xml:lang="en-GB" xmlns="">Attempted to divide by zero.
</faultstring>
<detail xmlns="">
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"></HelpLink>
<InnerException i:nil="true"></InnerException>
<Message>Attempted to divide by zero.</Message>
<StackTrace>
at HelloWCFService.Service1.Divide(Int32 val1, Int32 val2) in C:\Users\npdrsaini\documents\visual studio 2010\Projects\HelloService\HelloWCFService\Service1.svc.cs:line 33at SyncInvokeDivide(Object , Object[], Object[])at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[]inputs, Object[]&outputs)at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<Type>System.DivideByZeroException</Type>
</ExceptionDetail>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
Above message format is SOAP1.1 if you want to see SOAP1.2 some changes needed.
change the binding from basicHttpBinding to wsHttpBinding.
By default message Security is turned on for wsHttpBinding.Set the security mode for wsHttpBinding to None,so we could view the SOAP1.2 fault message.
<bindings>
<wsHttpBinding>
<binding>
<security mode="None"></security>
</binding>
</wsHttpBinding>
</bindings>
SOAP1.2 message format for same response will be as follows :
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action>
<a:RelatesTo>urn:uuid:47e00c8f-ca12-4e05-8321-46ff8fa69c79</a:RelatesTo>
<ActivityId CorrelationId="bba96711-ee01-4f2c-a5b2-f53af7975901" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">5e2bc13f-f61c-4ba9-8b1b-328db1823108</ActivityId>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Receiver</s:Value>
<s:Subcode>
<s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-GB">Attempted to divide by zero.</s:Text>
</s:Reason>
<s:Detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"></HelpLink>
<InnerException i:nil="true"></InnerException>
<Message>Attempted to divide by zero.</Message>
<StackTrace>
at HelloWCFService.Service1.Divide(Int32 val1, Int32 val2) in C:\Users\npdrsaini\documents\visual studio 2010\Projects\HelloService\HelloWCFService\Service1.svc.cs:line 33at SyncInvokeDivide(Object , Object[], Object[])at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[]inputs, Object[]&outputs)at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<Type>System.DivideByZeroException</Type>
</ExceptionDetail>
</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>
Code implementation :
[ServiceContract]
public interface IService1
{
[OperationContract]
float Divide(int val1,int val2);
}
public class Service1 : IService1
{
public float Divide(int val1, int val2)
{
return val1 / val2;
}
}
Client side code :
public partial class TestClient : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_getEmp_Click(object sender, EventArgs e)
{
Service1Client ocjClient = new Service1Client("WSHttpBinding_IService1");
float result = ocjClient.Divide(10, 0);
}
}
Related Post :
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF
Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault
No comments:
Post a Comment