0

I'm trying to call a service from a wsdl imported Screenshot

Here is an example of the code and I don't know how/where I have to pass the securityToken as header.

ProjectName.Service.ServiceClient serviceClient = new ProjectName.Service.ServiceClient();
int result = serviceClient.getDataFromDS(inputDTO); // <- Here i got the error because it doesn't find the SecurityToken in the header.

from Postman the call(POST) works, because I can put the header here: Screenshot

It also works using the (HttpWebRequest)WebRequest but I have to use the import wsdl as a client service. Here is an example of working call:

ProjectName.Service.LoginServiceClient loginServiceClient = new ProjectName.Service.LoginServiceClient();
loginServiceClient.login(loginDTO);

But here is so easy because it accepts only a xml input(inputDTO) and not a value in the header.

Thanks in advance.

Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
MrD
  • 1
  • 1
  • Does this answer your question? [How to add a custom HTTP header to every WCF call?](https://stackoverflow.com/questions/964433/how-to-add-a-custom-http-header-to-every-wcf-call) – Oliver Weichhold Oct 20 '22 at 15:56

1 Answers1

0

As far as I know, if you want to pass values to the header, usually use IDispatchMessageInspector on the server-side, and use IClientMessageInspector on the client-side.

It might look something like this:

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
    HttpRequestMessageProperty httpRequestMessage;
    object httpRequestMessageObject;
    if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
    {
        httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
        if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER]))
        {
            httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent;
        }
    }
    else
    {
        httpRequestMessage = new HttpRequestMessageProperty();
        httpRequestMessage.Headers.Add(USER_AGENT_HTTP_HEADER, this.m_userAgent);
        request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
    }
    return null;
}

Hope it helps.

Jiayao
  • 510
  • 3
  • 7