代码之家  ›  专栏  ›  技术社区  ›  Ibrahim Schi

为什么WCF OperationContext.Current的值不是此OperationContextScope错误安装的OperationContext值而没有任何代码更改?

  •  1
  • Ibrahim Schi  · 技术社区  · 4 年前

    本周,在没有任何代码更改的情况下,我们的传统WCF web服务开始在API管理系统上抛出“OperationContext.Current的值不是此OperationContextScope安装的OperationContext值”错误。

    我们已经检查了已安装的.net版本(.net 4.5.1)和其他安装。一切似乎都很好。 当我们尝试在本地计算机上调试时,我们无法重现相同的错误。

    我的相关代码部分如下:

    internal static T InvokeService<T>(this CalculatorClient client, Func<CalculatorClient, T> method)
            {
                if (client.ClientCredentials.UserName.UserName == null)
                {
                    client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["APIMANAGEMENT_User"];
                    client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["APIMANAGEMENT_Pwd"];
                }
    
                T result;
    
                using (new OperationContextScope(client.InnerChannel))
                {
                    string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(ConfigurationManager.AppSettings["APIMANAGEMENT_User"] + ":" + ConfigurationManager.AppSettings["APIMANAGEMENT_Pwd"]));
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["Authorization"] = auth;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                    result = method.Invoke(client);
                }
    
                return result;
            }

    你知道它为什么会失败吗?

    0 回复  |  直到 4 年前
    推荐文章