The value of OperationContext.Current is not the OperationContext value installed by this OperationContextScope

牧云@^-^@ 提交于 2019-12-11 06:03:18

问题


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

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

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

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

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

The value of OperationContext.Current is not the OperationContext value installed by this OperationContextScope.

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.


回答1:


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

To this:

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

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

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!



来源:https://stackoverflow.com/questions/44192260/the-value-of-operationcontext-current-is-not-the-operationcontext-value-installe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!