locked
How to suppress header in SOAP response from C# web service RRS feed

  • Question

  • User1311133436 posted

    We're working with a client who uses Java to call our .NET web service.  We've received their request and responded successfully, but they're saying they can't process the response because of the SOAP headers. What they want us to do is to send an empty header instead.  How do I control that?

    Current response:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
    <ActivityId CorrelationId="ec996ca1-9999-9999-9999-50fce66eb706" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId>
    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <u:Timestamp u:Id="_0">
    <u:Created>2018-02-12T17:16:24.545Z</u:Created>
    <u:Expires>2018-02-12T17:21:24.545Z</u:Expires>
    </u:Timestamp>
    </o:Security>
    </s:Header>
    <s:Body>
    <MyResponse xmlns="http://xmlns.oracle.com/integration/b2b" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Status>Success</Status>
    <ErrorMessage i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></ErrorMessage>
    </MyResponse>
    </s:Body>
    </s:Envelope>

    Intended response:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header></s:Header>
    <s:Body>
    <MyResponse xmlns="http://xmlns.oracle.com/integration/b2b" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Status>Success</Status>
    <ErrorMessage i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></ErrorMessage>
    </MyResponse>
    </s:Body>
    </s:Envelope>

    Monday, February 12, 2018 6:00 PM

Answers

  • User1168443798 posted

    Hi loopyou,

    For your requirement, you could try Message Inspectors or Message Encoder to modify the request.

    # WCF Extensibility – Message Inspectors

    https://blogs.msdn.microsoft.com/carlosfigueira/2011/04/18/wcf-extensibility-message-inspectors/

    # WCF Extensibility – Message Encoders

    https://blogs.msdn.microsoft.com/carlosfigueira/2011/11/08/wcf-extensibility-message-encoders/

    Best Regards,

    Edward

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 13, 2018 3:05 AM
  • User1311133436 posted

    Thanks, an inspector got me halfway, and I figured out the rest.  Here's the inspector code for anyone who's interested:

        public class RemoveResponseHeadersInspector : IDispatchMessageInspector
        {
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                return null;  //result gets passed to BeforeSendReply as correlationState--nothing needed
            }
    
            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                reply.Headers.Clear();  //don't return any headers in response
            }
        }
    
        //entry point of manipulating the response--adds custom inspector
        public class CustomBehaviorAttribute : Attribute, IServiceBehavior
        {
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
                //nothing needed
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                //add message inspector to endpoints so we can manipulate response header
                foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        ed.DispatchRuntime.MessageInspectors.Add(new RemoveResponseHeadersInspector());
                    }
                }
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                //nothing needed
            }
        }
    
        [CustomBehavior] //attach new inspector behavior to service
        public class MyService : IMyService
        {
            //...service code here
        }
    
    

    The additional piece was that because my service is secured, there was still a security header that got added automatically to the response.  You can turn that off in the config with the enableUnsecuredResponse attribute in your security binding:

      <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="NewBinding1">
              <security authenticationMode="UserNameOverTransport" 
    messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
    enableUnsecuredResponse="true" /> </binding> </customBinding> </bindings> </system.serviceModel>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 15, 2018 2:44 PM

All replies

  • User1168443798 posted

    Hi loopyou,

    For your requirement, you could try Message Inspectors or Message Encoder to modify the request.

    # WCF Extensibility – Message Inspectors

    https://blogs.msdn.microsoft.com/carlosfigueira/2011/04/18/wcf-extensibility-message-inspectors/

    # WCF Extensibility – Message Encoders

    https://blogs.msdn.microsoft.com/carlosfigueira/2011/11/08/wcf-extensibility-message-encoders/

    Best Regards,

    Edward

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 13, 2018 3:05 AM
  • User1311133436 posted

    Thanks, an inspector got me halfway, and I figured out the rest.  Here's the inspector code for anyone who's interested:

        public class RemoveResponseHeadersInspector : IDispatchMessageInspector
        {
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                return null;  //result gets passed to BeforeSendReply as correlationState--nothing needed
            }
    
            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                reply.Headers.Clear();  //don't return any headers in response
            }
        }
    
        //entry point of manipulating the response--adds custom inspector
        public class CustomBehaviorAttribute : Attribute, IServiceBehavior
        {
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
                //nothing needed
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                //add message inspector to endpoints so we can manipulate response header
                foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        ed.DispatchRuntime.MessageInspectors.Add(new RemoveResponseHeadersInspector());
                    }
                }
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                //nothing needed
            }
        }
    
        [CustomBehavior] //attach new inspector behavior to service
        public class MyService : IMyService
        {
            //...service code here
        }
    
    

    The additional piece was that because my service is secured, there was still a security header that got added automatically to the response.  You can turn that off in the config with the enableUnsecuredResponse attribute in your security binding:

      <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="NewBinding1">
              <security authenticationMode="UserNameOverTransport" 
    messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
    enableUnsecuredResponse="true" /> </binding> </customBinding> </bindings> </system.serviceModel>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 15, 2018 2:44 PM