简体   繁体   English

WCF双工nettcp通道没有故障

[英]WCF duplex nettcp channel not faulting

Problem 问题

I have this strange problem. 我有这个奇怪的问题。 I am hosting a WCF server in a console app: Console.WriteLine("Press 'q' to quit."); 我在控制台应用程序中托管WCF服务器:Console.WriteLine(“按'q'退出。”);

            var serviceHost = new ServiceHost(typeof(MessageService));
            serviceHost.Open();
            while (Console.ReadKey().KeyChar != 'q')
            {
            }
            serviceHost.Close();

it exposes two endpoint for publish and subscribe (duplex binding) When I stop or exit the console app, I never receive channel faulted at the client end. 它公开了两个用于发布和订阅的端点(双工绑定),当我停止或退出控制台应用程序时,我永远不会在客户端收到错误的通道。 I would like client to be informed is server is down. 我想通知客户端服务器已关闭。 Any idea what is going wrong here? 知道这里出了什么问题吗?

All I want is either of the following event to be raised when console app goes down: 我只想在控制台应用程序关闭时引发以下任一事件:

    msgsvc.InnerDuplexChannel.Faulted += InnerDuplexChannelOnFaulted;
    msgsvc.InnerChannel.Faulted += InnerChannelOnFaulted;

From MSDN: The duplex model does not automatically detect when a service or client closes its channel. 从MSDN:双工模型不会自动检测服务或客户端何时关闭其通道。 So if a service unexpectedly terminates, by default the service will not be notified, or if a client unexpectedly terminates, the service will not be notified. 因此,如果服务意外终止,则默认情况下不会通知该服务,或者如果客户端意外终止,则不会通知该服务。 Clients and services can implement their own protocol to notify each other if they so choose. 客户端和服务可以选择使用自己的协议来相互通知。

AFAIK tcp channel is quite responsive to (persistant) connection problems, but you can use callback to notify clients before server becomes unavailable. AFAIK tcp通道可以很好地响应(持久)连接问题,但是您可以使用回调在服务器不可用之前通知客户端。 From client side you can use some dummy ping/poke method 'OnTimer' to get actual connection state/keep channel alive. 在客户端,您可以使用一些虚拟的ping / poke方法“ OnTimer”来获取实际的连接状态/保持活动状态。 It's good to recover client proxy (reconnect) at that point, I suppose. 我认为此时恢复客户端代理(重新连接)是一件好事。 If service provides metadata endpoint, you also can call svcutil for trial connection or retrieve metadata programmatically: 如果服务提供元数据终结点,则您还可以调用svcutil进行试用连接或以编程方式检索元数据:

  Uri mexAddress = new Uri("http://localhost:5000/myservice");
  var mexClient = new MetadataExchangeClient(mexAddress, MetadataExchangeClientMode.HttpGet);
  MetadataSet metadata = mexClient.GetMetadata();
  MetadataImporter importer = new WsdlImporter(metadata);
  ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

  ContractDescription description =
  ContractDescription.GetContract(typeof(IMyContract));
  bool contractSupported = endpoints.Any(endpoint =>
  endpoint.Contract.Namespace == description.Namespace &&
  endpoint.Contract.Name == description.Name);

or 要么

ServiceEndpointCollection endpoints = MetadataResolver.Resolve(typeof(IMyContract), mexAddress, MetadataExchangeClientMode.HttpGet)

if you're the client, and subscribe to all 3 events, IClientChannel's Closing, Closed, and Faulted, you should see at the very least the Closing and Closed events if you close the service console app. 如果您是客户端,并且预订了IClientChannel的Closing,Closed和Faulted这3个事件,则在关闭服务控制台应用程序时至少应该看到Closing和Closed事件。 I'm not sure why. 我不知道为什么。 if you're closing up correctly, you would think you'd see Faulted. 如果正确关闭的话,您会认为您会看到“故障”。 I'm using tcp.net full duplex channels and I see these events just fine. 我正在使用tcp.net全双工通道,我看到这些事件就很好了。 Keep in mind it's in 2017, so things might have changed since then, but I rely on these events all the time in my code. 请记住,那是在2017年,因此此后情况可能有所变化,但是我一直在代码中依赖这些事件。 For those people who says the duplex model doesn't see Closing of channels, I'm not understanding that. 对于那些说双工模型看不到通道关闭的人,我不太理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM