1

i try to consume a SOA Service. I generate a Service Reference from the wsdl and then I instantiate a client object with my binding configuration, it is a basicHttpBinding.

Then i implement a custom behavior and a message inspector and there I add my custom header properties like shown below...

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        request.Properties.Add("CONTENT-TYPE", "text/xml;charset=UTF-8");
        request.Properties.Add("PropertyOne", "One");
        request.Properties.Add("PropertyTwo", "Two");

        return null;
    }

Then, when I try to consume the service, I get always the Error message

(502) Bad Gateway.

With fiddler I look at the raw http data send to the service, the custom properties aren't in the header.

2 Answers 2

4

To add a custom HTTP header to a message, you need to add them to the HttpRequestMessageProperty instance of the message property bag:

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
    HttpRequestMessageProperty prop;
    if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
    {
        prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
    }
    else
    {
        prop = new HttpRequestMessageProperty();
        request.Properties.Add(HttpRequestMessageProperty.Name, prop);
    }

    prop.Headers["Content-Type"] = "text/xml; charset=UTF-8";
    prop.Headers["PropertyOne"] = "One";
    prop.Headers["PropertyTwo"] = "Two";

    return null;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I wanted to do something similar too and had luck with WeboperationContext

WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Accepted; WebOperationContext.Current.OutgoingResponse.Headers.Add("HeaderName", "HeaderValue");

and it worked like a charm

1 Comment

Wrapping the code will be preferred by many people. Making the code and hit Ctrl + K will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.