使用 ConfigurationChannelFactory.CreateChannel 时向 WCF 添加请求标头 [英] Add Request header to WCF when using ConfigurationChannelFactory.CreateChannel

查看:32
本文介绍了使用 ConfigurationChannelFactory.CreateChannel 时向 WCF 添加请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 ConfigurationChannelFactory.CreateChannel 时,我需要向 WCF 请求添加请求标头.

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

我已经尝试过使用 OperationContextScope.

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();
    }

配置文件入口:

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

上面的函数是从另一个.cs文件调用的,如下图,传入Func作为参数:

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);

我收到了 400,BadRequest,因为服务在请求标头中期待授权",但它无法找到.

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

推荐答案

我们可以使用 WebOperationContext 类来修改和添加 HTTP 头,请参考下面的代码段.

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();
            }

结果.

详情请
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.web.weboperationcontext?redirectedfrom=MSDN&view=netframework-4.8
如果有什么我可以帮忙的,请随时告诉我.

Result.

For details,
https://docs.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.

这篇关于使用 ConfigurationChannelFactory.CreateChannel 时向 WCF 添加请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆