I have a client code implementation to consume a service with IEndpointBehavior to track request and response data.
一切工作正常,直到我使用 OperationContextScope 实现不记名令牌。
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer " + accessToken;
var context = new OperationContext(client.InnerChannel);
context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var operationContext = new OperationContextScope(context);
BeforeSendRequest、AfterReceiveReply 停止调用,因为我实现了基于令牌的身份验证,并且当我删除用于向标头添加令牌的 OperationContextScope 代码时,它正在工作。
我需要帮助来理解如何一起使用两者(使用 OperationContextScope 和 IEndpointBehavior 进行消息拦截器插入令牌)。
根据您的描述,我进行了测试,成功将OperationContextScope和IEndpointBehavior一起使用。您可能将OperationContextScope的代码放在IEndpointBehavior的代码前面,这会导致IEndpointBehavior的代码失败。
Service1Client service1Client = new Service1Client();
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer";
var context = new OperationContext(service1Client.InnerChannel);
context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var operationContext = new OperationContextScope(context);
service1Client.Endpoint.Behaviors.Add(new Interceptor());
service1Client.GetUserData("Test");
上面的代码结构就会导致这个问题。
正确的代码结构应该是这样的:
Service1Client service1Client = new Service1Client();
service1Client.Endpoint.Behaviors.Add(new Interceptor());
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer";
var context = new OperationContext(service1Client.InnerChannel);
context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var operationContext = new OperationContextScope(context);
service1Client.GetUserData("Test");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)