我正在尝试在 WCF 服务实现中使用IDispatchMessageInspector来访问自定义标头值。
就像是:
public class MyService : IMyService
{
public List<string> GetNames()
{
var headerInspector = new CustomHeaderInspector();
// Where do request & client channel come from?
var values = headerInspector.AfterReceiveRequest(ref request, clientChannel, OperationContext.Current.InstanceContext);
}
}
我已经实现了自己的 IDispatchMessageInspector 类。
public class CustomHeaderInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
var userName = prop.Headers["Username"];
return userName;
}
}
我如何通过
System.ServiceModel.Channels。留言和
系统.服务模型。客户端频道
从服务实现调用AfterReceiveRequest ?
编辑:
许多文章都喜欢this one or this one,举例说明如何实现你自己的ServiceBehavior
。所以你的服务实现看起来像这样:
[MyCustomBehavior]
public class MyService : IMyService
{
public List<string> GetNames()
{
// Can you use 'MyCustomBehavior' here to access the header properties?
}
}
那么有了这个,我可以MyCustomBehavior
在服务操作方法中以某种方式访问来访问自定义标头值吗?