Search code examples
apiwcfasp.net-coresoapsession-cookies

ASP.NET Core making SOAP API request with WCF client how to add a Cookie header to the request?


So I am currently working on making SOAP API request to a service with WCF generated code "Client object", I am wondering how to set the Cookie header to the request?


Solution

  • In general, we add the custom HTTP header by using HttpRequestMessageProperty. Please refer to the below code.

    ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
    try
    {
        using (OperationContextScope ocs=new OperationContextScope(client.InnerChannel))
        {
            var requestProp = new HttpRequestMessageProperty();
            requestProp.Headers["myhttpheader"] = "Boom";
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestProp;
            var result = client.SayHelloAsync();
            Console.WriteLine(result.Result);
        }
    

    Result.
    enter image description here
    WebOperationContext is a convenience wrapper around the OperationContext. At present, it hasn’t been implemented yet in the Aspnet Core.
    https://github.com/dotnet/wcf/issues/2686
    Feel free to let me know if there is anything I can help with.