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

发布于 2025-01-14 19:06:21 字数 1512 浏览 4 评论 0原文

本周,在没有任何代码更改的情况下,我们的旧版 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;
        }

您知道它为什么失败吗?

This week without any code change, our legacy WCF web service started to throw "The value of OperationContext.Current is not the OperationContext value installed by this OperationContextScope" error on our API management system.

We have checked installed .net version (.net 4.5.1) and other installations. All seems OK.
When we have tried to debug on local computer, we can not reproduce same error.

My related code part is below:

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

Do you have any idea about why it is failing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文