- I know how to use [FaultException] and throw Customized [DataContract] in WCF
- I know how to return soap message with Customized Headers and Body Tags using [MessageContract]
However, I'm going to ask: How to throw a [FaultException] in WCF with customized Headers and Body? Because when I use [FaultException] to throw [MessageContract], it always wraps my headers and body tags into body tag.
This is not what I want. Most of the SOAP clients do not understand WCF [FaultException].
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-CH" />
<detail>
<CustomizedError xmlns="http://schemas.datacontract.org/2004/07/PlayWcfFault" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<BodyTag1>Hello BodyTag1</BodyTag1>
<HeaderTag1>Hello HeaderTag1</HeaderTag1>
</CustomizedError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
[MessageContract]
public class CustomizedError
{
[MessageHeader] public string HeaderTag1;
[MessageBodyMember] public string BodyTag1;
}
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(CustomizedError))]
CustomizedError GetData();
}
public class Service1 : IService1
{
public CustomizedError GetData()
{
CustomizedError fault = new CustomizedError
{
HeaderTag1 = "Hello HeaderTag1",
BodyTag1 = "Hello BodyTag1",
};
throw new FaultException<CustomizedError>(fault, "");
// return fault;
}
}
The soap message I want is
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<HeaderTag1>Hello HeaderTag1</HeaderTag1>
</s:Header >
<s:Body>
<s:Fault>
<BodyTag1>Hello BodyTag1</BodyTag1>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
I am confused that why you define the soap message structure in that format. But as far as I know, the IErrorHandler interface is able to custom the fault message when capturing the communication error. Here is an example used to capture the errors during communication.
Hadler Error EndPoint in my host - WCF - Behaviour
We are capable of adding the custom message header in the ProvideFault method. Please consider the below code.
Result.

Feel free to let me know if there is anything I can help with.
Updated.
In order to create ActivityID field, we should enable tracing and message logging on the server-side.
Link.

About ActivityID field
Configure tracing and message logging
When the invocation on the client-side passes a parameter 0. We capture the http request by using fiddler.