问题
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