OperationContext.Current的值不是此OperationContextScope安装的OperationContext值 [英] The value of OperationContext.Current is not the OperationContext value installed by this OperationContextScope

查看:73
本文介绍了OperationContext.Current的值不是此OperationContextScope安装的OperationContext值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WebApi控制器中,我试图调用 .asmx SOAP服务.

Within my WebApi controller, I am trying to call an .asmx SOAP Service.

我使用VS2015生成了SoapClient代理作为服务引用.

I used VS2015 to generate the SoapClient proxy as a Service Reference.

我的主要问题是我需要将Authorization标头设置为包含Bearer令牌.

My main problem is that I need to set the Authorization header to contain a Bearer token.

我以为我有一个解决方案,使用@SimonRC的答案

I thought I had a solution, using @SimonRC's answer here. My code looks like this:

using (OperationContextScope scope = new OperationContextScope(_client.InnerChannel))
{
    var httpRequestProperty = new HttpRequestMessageProperty();
    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer blahblahblah";
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

    string packetData = await _client.GetPacketAsync(packetNumber, packetDate.ToString("ddMMMyy"));

    response = new Packet()
    {
        packet = packetData
    };
    return response;
}

当我在VS2015中进行调试时,这很好用,但是当我将WebApi部署到Azure时,出现此错误:

This works great when I debug in VS2015, but when I deploy my WebApi to Azure I'm getting this error:

OperationContext.Current的值不是此OperationContextScope安装的OperationContext值.

我正在寻找(a)解决此错误的解决方案,或(b)将Authorization标头设置为包含Bearer令牌的另一种方法.

I'm looking for either (a) an solution to resolve this error, or (b) an alternate way to set the Authorization header to contain a Bearer token.

推荐答案

将对SOAP服务的调用更改为同步而不是异步即可解决该问题.

Changing the call to the SOAP service to be synchronous instead of async solved the problem.

基本上,我对此进行了更改:

Basically, I changed this:

string packetData = await _client.GetPacketAsync(packetNumber, packetDate.ToString("ddMMMyy"));

对此:

string packetData = _client.GetPacketAsync(packetNumber, packetDate.ToString("ddMMMyy"));

当然,相应地更改了整个链,以在任何地方删除异步Task.做到了.

And of course corresponding changes up the chain to remove async Task everywhere. That did it.

我想到从@ChrisMarisic的评论中删除异步

I got the idea to remove async from @ChrisMarisic's comment here that "I know this question is old, but there is pretty much no reason to use async inside a WCF service ever." So glad for comments like these!

这篇关于OperationContext.Current的值不是此OperationContextScope安装的OperationContext值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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