gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:10:47 25 4
gpt4 key购买 nike

我想在每次调用 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 没有事件可以做我想做的事,所以我想有一个解决方法。

最佳答案

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

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 了解有关扩展 WCF 的更多信息

关于c# - 调用 WCF 服务时如何触发事件(客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com