C# 如何在调用WCF服务时触发事件(客户端)
我想在每次调用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
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 ());