tbe*_*nin 3 structuremap wcf dependency-injection wcf-client endpointbehavior
我在通过此处的方法" 集中式cookie管理 "中概述的方法进行WCF服务调用时管理共享的auth cookie :http://megakemp.com/2009/02/06/managing-shared-cookies-in- WCF /
我已经建立了一个自定义的IClientMessageInspector,IEndpointBehavior,BehaviorExtensionElement,的作品.我的端点行为添加了一个消息检查器,如下所示:
public class MyEndpointBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
// yuck.. Wish I had an instance of MyClientMessageInspector
// (which has the auth cookie already) so I could just inject that
// instance here instead of creating a new instance
clientRuntime.MessageInspectors.Add(new MyClientMessageInspector());
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都完美无缺,但是当您想要在多个客户端上共享cookie时,此解决方案就会崩溃.由于该ApplyDispatchBehavior()方法创建了一个新实例,因此任何其他客户端都不会获取该消息检查器实例,因此也不会获取该身份验证票证.
那么我想尝试创建一个自定义构造函数,我可以像这样注入实例:
MyEndpointBehavior(MyClientMessageInspector msgInspector) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,WCF需要无参数构造函数.通过互联网络除草,WCF有挂钩,允许依赖注入,创建IInstanceProvider,IServiceBehavior等等.但是我不认为这就是我正在寻找在这里.
任何人都可以帮助指导我正确的方向吗?
您只需要扩展概念,以便将cookie存储在消息检查器本身之外,以便消息检查器的所有实例共享同一存储.
只是为了开始,穷人的方式是使用静态字段而不是实例字段.显然,如果你有多个线程,你需要在更新字段时提供并发性.如果你将它推广到一个cookie容器概念,然后确保你与所有客户共享同一个容器,你可以从那里获得更好的体验.共享容器可以通过获取ChannelParameterCollection客户端通道并向其添加属性来完成,然后您的行为在检查消息并从中拉出cookie时查找该属性.这看起来有点像这样:
应用逻辑
// Hold onto a static cookie container
public static CookieContainer MyCookieContainer;
// When instantiating the client add the cookie container to the channel parameters
MyClient client = new MyClient();
client.InnerChannel.GetProperty<ChannelParameterCollection>().Add(MyCookieContainer);
Run Code Online (Sandbox Code Playgroud)
消息检查器逻辑
public void BeforeSendMessage(ref Message, IClientChannel clientChannel)
{
// Find the cookie container for the current channel
CookieContainer cookieContainer = clientChannel.GetProperty<ChannelParameterCollection>().Select(p => p as CookieContainer).Where(cc => cc != null).First();
// ... use the cookie container to set header on outgoing context ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3053 次 |
| 最近记录: |