Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在调用WCF服务时触发事件(客户端)_C#_Wcf_Service_Proxy_Channelfactory - Fatal编程技术网

C# 如何在调用WCF服务时触发事件(客户端)

C# 如何在调用WCF服务时触发事件(客户端),c#,wcf,service,proxy,channelfactory,C#,Wcf,Service,Proxy,Channelfactory,我想在每次调用WCF服务时触发一个事件 我尝试了以下方法: var factory = new ChannelFactory<TService>(binding, endPointAdress); factory.Credentials.UserName.UserName = username; factory.Credentials.UserName.Password = password; var proxy = factory.CreateChannel(); ((ICo

我想在每次调用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);
var-factory=新的ChannelFactory(绑定,端点地址);
factory.Credentials.UserName.UserName=用户名;
factory.Credentials.UserName.Password=密码;
var proxy=factory.CreateChannel();
((IContextChannel)this.Proxy).Opened+=新事件处理程序(FactoryOpeningEventHandler);
this.Factory.Opened+=新的EventHandler(FactoryOpeningEventHandler);

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

首先创建一个inspector类,该类实现
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;
    }
}
在你有了你的inspector类之后,你必须通过一个Behavior注册它

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
    }
}
您可以在以下位置了解有关扩展WCF的更多信息:

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