如何在WCF服务使用IDispatchMessageInspector? [英] How to use IDispatchMessageInspector in a WCF Service?
问题描述
我想使用<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector.aspx">IDispatchMessageInspector在WCF服务实现访问自定义标题值。
I am trying to use IDispatchMessageInspector in a WCF service implementation to access custom header values.
是这样的:
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类。
I've implemented my own IDispatchMessageInspector class.
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。信息和
System.ServiceModel。 IClientChannel
System.ServiceModel.IClientChannel
为 AfterReceiveRequest 所谓的服务实现?的
编辑:
很多文章像<一个href="http://msmvps.com/blogs/paulomorgado/archive/2007/04/27/wcf-building-an-http-user-agent-message-inspector.aspx">this 之一或<一href="http://blogs.msdn.com/b/zelmalki/archive/2008/12/29/creating-a-wcf-idispatchmessageinspector.aspx">this 之一,就如何实现自己的 ServiceBehavior
的例子。所以,你的服务实现是这样的:
Many articles like this one or this one, give examples on how to implement your own ServiceBehavior
. So your service implementation looks like this:
[MyCustomBehavior]
public class MyService : IMyService
{
public List<string> GetNames()
{
// Can you use 'MyCustomBehavior' here to access the header properties?
}
}
所以用这个,我可以访问 MyCustomBehavior
不知何故服务操作方法中访问自定义头值?
So with this, can I access MyCustomBehavior
somehow within the service operation method to access custom header values?
推荐答案
您必须配置
<extensions>
<behaviorExtensions>
<add
name="serviceInterceptors"
type="CustomHeaderInspector , MyDLL, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
然后扩展会在你的WCF栈处理。该服务本身所具有的 serviceInterceptors
没有概念,你没有做这样的事情在你的第一个code座。在WCF堆栈将注入你督察。
Then the extension will be handled in your WCF stack. The service itself has no notion of the serviceInterceptors
and you do not have to do something like in your first code block. The WCF stack will inject you Inspector.
<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector.aspx"相对=nofollow> MSDN:system.servicemodel.dispatcher.idispatchmessageinspector
这篇关于如何在WCF服务使用IDispatchMessageInspector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!