【问题标题】:Managing Credentials Within ChannelFactory Cache在 ChannelFactory 缓存中管理凭证
【发布时间】:2018-01-19 08:34:58
【问题描述】:

我已将旧库转换为 .NET Core 并实现了 ChannelFactory 缓存解决方案。它连接的服务需要将基本授权添加到每个请求的 HTTP 标头中。使用 ClientMesageInspector 将标头添加到每个请求。因为一旦创建第一个通道,ClientMessageInspector 是不可变的,所以 ChannelFactory 缓存将每个实例存储在字典中,并使用基于 ChannelFactory 端点、用户名和密码组合的键。此外,由于创建 ChannelFactory 的开销,我不会处理它们。我担心的是用户名和密码保存在字典键的内存中,以及添加到 ChannelFactory 的 ClientMesageInspector 中。有没有更好的解决方案?我已经在 dotnet/wcf GitHub page 上提出了这个问题,并希望向更多的观众开放。

提前致谢。

鲍勃。

【问题讨论】:

    标签: c# .net-core channelfactory


    【解决方案1】:

    dotnet/wcf 团队通过我的question 为我提供了很大帮助。 经过一番讨论,我最终使用 AsyncLocal 变量来存储用户名和密码。

    在包装类中,我们有两个通过构造函数设置的 AsyncLocal 变量。

    internal static AsyncLocal<string> Username = new AsyncLocal<string>();
    internal static AsyncLocal<string> Password = new AsyncLocal<string>();
    
    public WCFWrapper(string username, string password) : this()
    {
        Username.Value = username;
        Password.Value = password;
    }
    

    在 IClientMessageInspector BeforeSendRequest 方法中,我们只使用变量。

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
        httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] =  
            "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(WCFWrapper.Username.Value + ":" + WCFWrapper.Password.Value));
        request.Properties[HttpRequestMessageProperty.Name] = httpRequestProperty;
    
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 2011-11-02
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      相关资源
      最近更新 更多