views:

1473

answers:

3

Hi, I'm trying to use a third party web service (so I do not have access to the web service code). In Visual Studio 2008 I created a new web site project (ASP and c#), and added the web reference (not web service! so I guess it is not a WCF service... correct?).

The problem is that from the documentation of the web service I know that each soap request has to be sent with the following envelope and header, can you please tell me how to add this in my Soap requests? All the solutions I found required modifying the web service source or proxy, I can't do that because I don't have access to the web service source and the web service proxy in the client in Visual studio 2008 comes in a read-only temp file!

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
<soap:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="1">
<wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
<wsse:Username>[email protected]</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;Ima5tatto&lt;/wsse:Password&gt;
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body xmlns:ns2="http://neighbourhood.statistics.gov.uk/nde/v1-0/discoverystructs"&gt;
<ns2:AreaAtLevelElement>
<AreaIdWithLevelType>
<AreaId>276704</AreaId>
<LevelTypeId>12</LevelTypeId>
</AreaIdWithLevelType>
</ns2:AreaAtLevelElement>
</soap:Body>
</soap:Envelope>
+3  A: 

You can statically add headers to the messages in the configuration file using the headers element in the endpoint element. Each child element of the headers element will be copied as is in the header of your message.

Philippe
A: 

Hi, I'd like to re-open the issue, 'cause the answer is about WCF client, and not simple web service client. I have more or less the same situation of uince81, but I created the proxy classes myself, using visualstudio 2008 tools. Anyway my particular services only work with web reference and not with service reference, so I haven't the configuration file for the service.

How can I achieve the same result in this configuration?

themarcuz
@themarcuz: read the [FAQ](http://stackoverflow.com/faq). This is not a discussion forum. Ask your own question - we don't "reply to the thread" here.
John Saunders
A: 

I'm struggling with this same issue and thus far have written a message inspector to be able to get to the SOAP headers, although I'm not sure how to get the wsse:security stuff in there without having to do it manually. I would like to be able to use the WS-Security schema (as well as SAML schemas) to build the wsse:security stuff...

For what it's worth my message inspector code is below, if i solve this I'll post it on this thread.

Here's where I add the behavior to the client:

client.Endpoint.Behaviors.Add(new CustomBehavior());
msgOutput = client.ProvideAndRegisterDocumentSetXDR(msgInput);

And here is the message inspector and custom behavior:

public class CustomMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
{
    public void AfterReceiveReply(ref WCF.Message reply, object correclationState)
    {
    }

    public Object BeforeSendRequest(ref WCF.Message request, IClientChannel channel)
    {
        MessageHeaders headers = new MessageHeaders(MessageVersion.Soap11WSAddressing10);
        MessageHeader header = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "");
        request.Headers.Add(header);
        return null;
    }
}


public class CustomBehavior : System.ServiceModel.Description.IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRunTime)
        {
            CustomMessageInspector inspector = new CustomMessageInspector();
            clientRunTime.MessageInspectors.Add(inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
Tone
@Tone: read the [FAQ](http://stackoverflow.com/faq). This is not a discussion forum. Ask your own question - we don't "reply to the thread" here. Also, -1 for not noticing he's using a "web reference".
John Saunders