I downloaded a C# implementation example from NVMS to understand how to implement their web services. It uses SOAP requests to call different services. In the source code, there is an internal class implementing IEndpointBehavior and IClientMessageInspector, and in the BeforeSendRequest they cleared the SOAP headers but judging by the response I get, the final request still has headers. I tried both requests (with and without headers) that are printed in the console in SOAPUI and the headerless request works, while the other gets me the same message I get in the C# app itself.
Here is the class :
internal class CustomMessageInspector : IEndpointBehavior, IClientMessageInspector
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
/*public void BeforeSendReply(ref Message reply, object correlationState)
{
reply.Headers.Clear();
}*/
//add using directive Message = System.ServiceModel.Channels.Message;
public void AfterReceiveReply(ref Message reply, object correlationState)
{
// WORKAROUND for WCF-Exception "The MessageHeader is already understood"
// (Note: The message still gets validated)
reply.Headers.Clear();
Console.WriteLine("received Response:");
Console.WriteLine("{0}\r\n", reply);
}
/// <summary>
/// Shows the sent message with and without SOAP-Header
/// </summary>
/// <param name="request"></param>
/// <param name="channel"></param>
/// <returns></returns>
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// Fait la même chose que le clear, mais ne fonctione pas non plus...
/*request.Headers.ReplyTo = null;
request.Headers.Action = null;
request.Headers.MessageId = null;*/
Console.WriteLine("original Request:");
Console.WriteLine("{0}\r\n", request);
// Ne semble pas fonctionner, la requête est envoyée avec les headers...
request.Headers.Clear();
Console.WriteLine("without Header Request:");
Console.WriteLine("{0}\r\n", request);
return null;
}
}
The "request.Headers.Clear();" line should work here. The request argument is passed by reference, so it should clear the headers from the source object. But this is the result I get :
received Response:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope" />
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:MustUnderstand</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">MustUnderstand headers: [{http://www.w3.org/2005/08/addressing}To] are not understood.</soap:Text>
</soap:Reason>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Here are the 2 requests (with and without headers, as printed by the beforesendrequest method) :
original Request:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">ns:G110RequestMessage</a:Action>
<a:MessageID>urn:uuid:405c0e93-f39d-4d8b-bef8-72cf82f88203</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<G110Request xmlns="urn:wsdltypes.nmvs.eu:v3.0">
<Header xmlns="urn:types.nmvs.eu:v3.0">
<Auth>
<ClientLoginId>ABC</ClientLoginId>
<UserId>test123</UserId>
<Password>123456</Password>
</Auth>
<UserSoftware d5p1:name="Test Soft" d5p1:supplier="Comp Any" d5p1:version="V2" xmlns:d5p1="urn:types.nmvs.eu:v3.0" />
<Transaction>
<ClientTrxId>7775559966aaa</ClientTrxId>
<Language>eng</Language>
</Transaction>
</Header>
<Body xmlns="urn:types.nmvs.eu:v3.0">
<Product>
<ProductCode d6p1:scheme="GTIN" xmlns:d6p1="urn:types.nmvs.eu:v3.0">PK001C854A8EE536949</ProductCode>
<Batch>
<Id>TESTA1596337CF</Id>
<ExpDate>231130</ExpDate>
</Batch>
</Product>
<Pack d5p1:sn="PK001C854A8EE536949" xmlns:d5p1="urn:types.nmvs.eu:v3.0" />
</Body>
</G110Request>
</s:Body>
</s:Envelope>
without Header Request:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header />
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<G110Request xmlns="urn:wsdltypes.nmvs.eu:v3.0">
<Header xmlns="urn:types.nmvs.eu:v3.0">
<Auth>
<ClientLoginId>ABC</ClientLoginId>
<UserId>test123</UserId>
<Password>123456</Password>
</Auth>
<UserSoftware d5p1:name="Test Soft" d5p1:supplier="Comp Any" d5p1:version="V2" xmlns:d5p1="urn:types.nmvs.eu:v3.0" />
<Transaction>
<ClientTrxId>7775559966aaa</ClientTrxId>
<Language>eng</Language>
</Transaction>
</Header>
<Body xmlns="urn:types.nmvs.eu:v3.0">
<Product>
<ProductCode d6p1:scheme="GTIN" xmlns:d6p1="urn:types.nmvs.eu:v3.0">PK001C854A8EE536949</ProductCode>
<Batch>
<Id>TESTA1596337CF</Id>
<ExpDate>231130</ExpDate>
</Batch>
</Product>
<Pack d5p1:sn="PK001C854A8EE536949" xmlns:d5p1="urn:types.nmvs.eu:v3.0" />
</Body>
</G110Request>
</s:Body>
</s:Envelope>
I tried to change the mustUnderstand attribute but I can't find where to change it.