Nuthan Gowda
Nuthan Gowda

Reputation: 265

How to pass Header for the WCF SOAP Service

I'm trying to consume a WCF SOAP Service through C# console Application.

I have 2 services in context here, one for getting the Authentication Token by passing username and password 2nd for getting the required data, where this service expects the Token which is generated earlier to be passed as a Header with the name "Token"

This works fine when I call the service using the SOAP UI, where we have an option to send the Header name and value

but I'm not able to find a way to do the same programatically below is the sample code what I have tried

SecurityService.IInteropSecurityService authenticationClient = new SecurityService.InteropSecurityServiceClient();

string token = client.GetAuthenticationToken("cfadmin", "305f2a0ebb646a2ab32689d5b9c01532");


EntityService.InteropEntityServiceClient entityClient = new EntityService.InteropEntityServiceClient();

 using (new OperationContextScope(entityClient.InnerChannel))
            {
                HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
                httpRequestProperty.Headers["Token"] = token;

                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

            }
entityClient.GetEntity("Test name");

When i try this, it will get me

An unhandled exception of type 'System.ServiceModel.Security.SecurityAccessDeniedException' occurred in mscorlib.dll Additional information: Access is denied.

Upvotes: 0

Views: 649

Answers (1)

Abraham Qian
Abraham Qian

Reputation: 7522

The scope of the OperationContxtScope is only available within Using statement, namely, the service request with the specific soap header is valid in the Using statement. the other call is restored outside of the Using statement, without a specifical header.
We can use the IClientMessageInspector interface if we want to permanently add message headers to every requests.
https://putridparrot.com/blog/adding-data-to-wcf-message-headers-client-side
Please refer to my previous reply.
Adding an outgoing message headers in WCF can't be retrieved in incoming message headers
Feel free to let me know if there is anything I can help with.

Upvotes: 1

Related Questions