当我调用 WCF 服务时如何触发事件(客户端)

发布于 2024-10-26 10:50:36 字数 554 浏览 3 评论 0原文

我想在每次调用 WCF 服务时触发一个事件。

我尝试了以下操作:

var factory = new ChannelFactory<TService>(binding, endPointAdress);

factory.Credentials.UserName.UserName = username;
factory.Credentials.UserName.Password = password;

var proxy = factory.CreateChannel();

((IContextChannel)this.Proxy).Opened += new EventHandler(FactoryOpeningEventHandler);
this.Factory.Opened += new EventHandler(FactoryOpeningEventHandler);

上述问题是仅在打开代理时调用该事件,但我想在通过该代理进行调用时触发该事件,而不仅仅是在打开代理时触发该事件。我知道 IContextChannel 没有事件可以执行我想要的操作,因此我想要一个解决方法。

I'd like to fire an event every time I call a WCF service.

I've tried the following:

var factory = new ChannelFactory<TService>(binding, endPointAdress);

factory.Credentials.UserName.UserName = username;
factory.Credentials.UserName.Password = password;

var proxy = factory.CreateChannel();

((IContextChannel)this.Proxy).Opened += new EventHandler(FactoryOpeningEventHandler);
this.Factory.Opened += new EventHandler(FactoryOpeningEventHandler);

The problem with the above is that the event is only called when the proxy is opened, but I want to fire the event when a call is made trough this proxy, not only when it opens. I know there is no event for the IContextChannel that can do what I want, so I would like to have a workaround.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-11-02 10:50:36

首先创建一个实现 IDispatchMessageInspector(发送时)和 IClientMessageInspector(接收时)接口的检查器类。

public class SendReceiveInspector : IDispatchMessageInspector, IClientMessageInspector
{

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        // TODO: Fire your event here if needed
        return null;
    }
    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        // TODO: Fire your event here if needed
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        // TODO: Fire your event here if needed
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        // TODO: Fire your event here if needed
        return null;
    }
}

获得检查员课程后,您必须通过行为注册它。

public class SendReceiveBehavior : IEndpointBehavior, IServiceBehavior
{
    void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new SendReceiveInspector());
    }

    void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new SendReceiveInspector());
    }

    void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        // Leave empty
    }

    void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
    {
        // Leave empty
    }

    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host)
    {
        foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers)
        {
            foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
            {
                eDispatcher.DispatchRuntime.MessageInspectors.Add(new SendReceiveInspector());
            }
        }
    }

    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        // Leave empty
    }

    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        // Leave empty
    }
}

最后,您必须将该行为注册到您的服务描述中:

host.Description.Behaviors.Add(new SendReceiveBehavior ());
foreach (ServiceEndpoint se in host.Description.Endpoints)
    se.Behaviors.Add(new SendReceiveBehavior ());

您可以在 http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

You start by creating an inspector class that implement both IDispatchMessageInspector (when sending) and IClientMessageInspector (when receiving) interfaces.

public class SendReceiveInspector : IDispatchMessageInspector, IClientMessageInspector
{

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        // TODO: Fire your event here if needed
        return null;
    }
    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        // TODO: Fire your event here if needed
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        // TODO: Fire your event here if needed
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        // TODO: Fire your event here if needed
        return null;
    }
}

After you have your inspector class, you have to register it through a behaviour.

public class SendReceiveBehavior : IEndpointBehavior, IServiceBehavior
{
    void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new SendReceiveInspector());
    }

    void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new SendReceiveInspector());
    }

    void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        // Leave empty
    }

    void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
    {
        // Leave empty
    }

    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host)
    {
        foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers)
        {
            foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
            {
                eDispatcher.DispatchRuntime.MessageInspectors.Add(new SendReceiveInspector());
            }
        }
    }

    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        // Leave empty
    }

    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        // Leave empty
    }
}

Finally, you have to register that behaviour to your service description:

host.Description.Behaviors.Add(new SendReceiveBehavior ());
foreach (ServiceEndpoint se in host.Description.Endpoints)
    se.Behaviors.Add(new SendReceiveBehavior ());

You can learn more about extending WCF at http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文