【发布时间】:2011-09-03 16:12:42
【问题描述】:
在通过此方法进行 WCF 服务调用时,我正在管理共享身份验证 cookie,该方法在位于此处的标题“集中 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)
{
}
}
这一切都完美地运行,但是当您想要在多个客户端上共享 cookie 时,此解决方案就会失效。因为ApplyDispatchBehavior() 方法创建了一个新实例,所以任何其他客户端都不会获得该消息检查器实例,因此也不会获得身份验证票。
然后我想尝试创建一个自定义构造函数,我可以像这样注入实例:
MyEndpointBehavior(MyClientMessageInspector msgInspector) { ... }
但是,WCF 需要无参数构造函数。通过互联网进行筛选,WCF 具有允许依赖注入、创建IInstanceProvider、IServiceBehavior 等的挂钩。但我认为这不是我要在这里寻找的。p>
谁能帮我指引正确的方向?
【问题讨论】:
标签: wcf dependency-injection structuremap wcf-client endpointbehavior