Home > Blockchain >  Setting HTML content-type for WCF message
Setting HTML content-type for WCF message

Time:09-17

I'm using an external SOAP service, the european National Medicines Verification Systems, over which I have no control. My application uses a WCF client automatically generated from the supplied WSDL. The client uses System.ServiceModel.Channels.Message messages.

The SOAP service uses the same message structure, an "I7RequestType", for 2 messages, the G482LoadTermsAndConditions and the G487LoadDataPrivacyPolicies, and does not "understand" any Action header.

This is my request message, with the ignored Action header :

<?xml version="1.0" encoding="UTF-8"?>
<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="0">ns:G482RequestMessage</a:Action>
   </s:Header>
   <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <I7RequestType xmlns="urn:wsdltypes.nmvs.eu:v4.0">
         <Header xmlns="urn:types.nmvs.eu:v4.0">
            <Auth>
               <ClientLoginId>xxxxx</ClientLoginId>
               <UserId>xxxxx</UserId>
               <Password>xxxxx</Password>
            </Auth>
            <UserSoftware xmlns:d5p1="urn:types.nmvs.eu:v4.0" d5p1:name="xxxxx" d5p1:supplier="xxxxx" d5p1:version="2.0" />
            <Transaction>
               <ClientTrxId>xxxxxxxxxxxxxxx</ClientTrxId>
               <Language>fra</Language>
            </Transaction>
         </Header>
      </I7RequestType>
   </s:Body>
</s:Envelope>

The Header Action is ignored by the server, which returns a fault if mustUnderstand is set to 1.

Apparently I should be using the HTML content-type to permit the server to differentiate the two messages. This is the example I have been given:

    Content-Type : application/soap xml;charset=utf-8;method=SUPPORT-I7RequestType;action="urn:G482LoadTermsAndConditions"; 

My custom MessageInspector can manipulate the message content in BeforeSendRequest(), but I cannot find any way of adding/modifying the encapsulating HTTP message's headers.

Does anybody know any way of modifying the HTTP headers of an outbound System.ServiceModel.Channels.Message ?

Thanks in advance.

CodePudding user response:

After much searching the problem seems to be due to the generated WCF client not respecting the WSDL and the SOAP 1.2 specification.

This answer WCF Service with wsHttpBinding - Manipulating HTTP request headers gave me all that I needed to create a workaround.

This is the hard-coded proof-of-concept code, based very heavily on Dejan Janjušević's reply, that I added in the function BeforeSendRequest() of my CustomMessageInspector (implements IEndpointBehavior and IClientMessageInspector).

        // Making sure we have a HttpRequestMessageProperty
        HttpRequestMessageProperty httpRequestMessageProperty;
        if( request.Properties.ContainsKey( HttpRequestMessageProperty.Name ) )
        {
            httpRequestMessageProperty = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            if( httpRequestMessageProperty == null )
            {
                httpRequestMessageProperty = new HttpRequestMessageProperty();
                request.Properties.Add( HttpRequestMessageProperty.Name, httpRequestMessageProperty );
            }
        }
        else
        {
            httpRequestMessageProperty = new HttpRequestMessageProperty();
            request.Properties.Add( HttpRequestMessageProperty.Name, httpRequestMessageProperty );
        }

        Console.WriteLine("original Request:");
        Console.WriteLine("{0}\r\n", request);
        request.Headers.Clear();

        httpRequestMessageProperty.Headers.Add( "Content-Type", "application/soap xml;charset=utf-8;method=SUPPORT-I7RequestType;action=\"urn:G482LoadTermsAndConditions\"" );
  • Related