0

I need to add a Request Header to a WCF Request when using ConfigurationChannelFactory.CreateChannel.

I have already tried using OperationContextScope.

I have a function which is as shown below:

    public O Execute<O>(Func<T, O> action, string configFilePath, string endpoint, StringDictionary headers)
    {
        bool closed = false;
        T channel = default(T);
        O output = default(O);

        try
        {
            channel = this.GetChannel(configFilePath, endpoint);

            if (headers != null && headers.Count > 0)
            {
                (channel as IClientChannel).Open();
                using (new OperationContextScope(channel as IClientChannel))
                {
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    foreach (DictionaryEntry header in headers)
                    {
                        requestMessage.Headers[header.Key.ToString()] = header.Value.ToString();
                    }

                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                    output = action(channel);
                }
                (channel as IClientChannel).Close();
            }
            else
            {
                (channel as IClientChannel).Open();
                output = action(channel);
                (channel as IClientChannel).Close();
            }

            closed = true;
        }
        finally
        {
            if (!closed && channel != null)
            {
                (channel as IClientChannel).Abort();
            }
        }

        return output;
    }

    private T GetChannel(string configFilePath, string endpoint)
    {
        //Get the ChannelFactoryObject
        ConfigurationChannelFactory<T> wcfClientFactory = null;
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
        wcfClientFactory = new ConfigurationChannelFactory<T>(endpoint, ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None), null); 
        return wcfClientFactory.CreateChannel();
    }

Configuration file entry:

&lt;security mode="Transport"&gt;
   &lt;transport clientCredentialType="None" proxyCredentialType="None" realm="" /&gt;;clientCredentialType="Windows" negotiateServiceCredential="true" /&gt;
&lt;/security&gt;

The above function is called from another .cs file, as shown below, passing Func<T,O> as an argument:

Execute&lt;MyService.InformationResponse[]&gt;=&gt;IMyService.GetInformation(Request), ConfigPath, myServiceEndPoint, headers);

I am getting 400, BadRequest as the Service is expecting "Authorization" in the Request header, which it is not able to find.

jkdev
  • 11,360
  • 15
  • 54
  • 77

1 Answers1

0

We could use the WebOperationContext class to alter and add HTTP header, please refer to the below code segments.

  IService service = factory.CreateChannel();
            using (OperationContextScope scope = new OperationContextScope((IContextChannel)service))
            {
                WebOperationContext.Current.OutgoingRequest.ContentType = "application/json; charset=utf-8";
                WebOperationContext.Current.OutgoingRequest.Headers.Add("Authorization", "bearer xxxxxxxx");
                service.GetData();
            }

Result.
enter image description here
For details,
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.web.weboperationcontext?redirectedfrom=MSDN&view=netframework-4.8
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22