N
N
Nikolay Alekseev2021-04-19 16:14:52
SOAP
Nikolay Alekseev, 2021-04-19 16:14:52

How to set custom headers for every SOAP(WCF) request?

Connected to the project WSLD'ku through the Project-> Connect the reference to service.
Objects were generated, everything is fine. It’s not great that the SOAP service I’m accessing does not support standard authorization through an indication of the Creditials object, but asks to request the Login method, returns a token, and I must register this token as a header for each request that I will send subsequently.

I would appreciate any hint on how to do this!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Alekseev, 2021-04-24
@VariusRain

He asked, he answered ...
And so, the mechanism is as follows:

  1. You need to create a class that implements the IClientMessageInspector interface
  2. You need to create a class that implements the IEndpointBehavior interface
  3. You need to make them friends

In the message inspector, the title is set as follows:
public object BeforeSendRequest( ref Message request, IClientChannel channel ) {
      MessageHeader header = MessageHeader.CreateHeader( "authenticate", "NS", "value" );
      request.Headers.Add( header );
  
      Console.WriteLine( "BeforeSendRequest" );
      return null;
    }

The inspector is then attached to the behavior:
public void ApplyClientBehavior( ServiceEndpoint endpoint, ClientRuntime clientRuntime ) {
      clientRuntime.ClientMessageInspectors.Add( new ClientMessageInspector() );
    }

And finally, the behavior class needs to be bound to the client
client = new ApiSoapPortClient();
client.Endpoint.EndpointBehaviors.Add( new CustomBehavior() );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question