简体   繁体   English

如何使客户端自动检测WCF服务是否关闭或连接断开

[英]How to make client automatically detect if WCF service is down or lost connection

I've looked at a bunch of threads like Detect if wcf service is activated but these solutions require the client to proactively detect if the WCF service is running. 我看过很多线程,例如“ 检测是否激活了wcf服务”,但是这些解决方案要求客户端主动检测WCF服务是否正在运行。 But what if I am in the middle of a transaction and the WCF service goes down or the connection is lost for some reason? 但是,如果我正处于事务处理中间并且WCF服务中断或由于某种原因连接断开了怎么办? In my testing there is no exception thrown; 在我的测试中,没有抛出异常。 either nothing happens at all or that twirly circle thing just keeps going round and round. 要么什么也没有发生,要么旋转的东西不断旋转。 I want the client to detect if the service/connection is lost and gracefully tell the user it's down. 我希望客户端检测服务/连接是否丢失,并优雅地告诉用户它已关闭。 I have timeouts set in my code: 我在代码中设置了超时:

NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            binding.OpenTimeout = TimeSpan.FromSeconds(15);
            binding.SendTimeout = TimeSpan.FromSeconds(3000);
            binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
            this._engineChannel = new DuplexChannelFactory<IEngineApi>(this, binding, new EndpointAddress("net.pipe://localhost/Engine"));

But if I am in the middle of a transaction nothing actually happens; 但是,如果我正在进行交易,那么实际上什么也不会发生。 these timeouts don't seem to affect anything. 这些超时似乎没有任何影响。

You can use one of the two approaches: 您可以使用以下两种方法之一:

1 1

The two things I do are a telnet check to make sure the WCF process has the socket open. 我要做的两件事是进行telnet检查,以确保WCF进程已打开套接字。

telnet host 8080 The second thing I do is always add an IsAlive method to my WCF contract so that there is a simple method to call to check that the service host is operating correctly. telnet主机8080我要做的第二件事是始终向我的WCF合同中添加IsAlive方法,以便可以调用一个简单的方法来检查服务主机是否正常运行。

public bool IsAlive() { return true; 公共布尔IsAlive(){返回true; } }

Source: Pinging WCF Services 来源: Ping WCF服务

2 2

Use the Discovery/Announcement feature introduced in WCF 4.0 使用WCF 4.0中引入的发现/公告功能

Discovery depends on the User Datagram Protocol (UDP). 发现 取决于用户数据报协议(UDP)。 UDP is a connectionless protocol, and there is no direct connection required between the client and server. UDP是无连接协议,客户端与服务器之间不需要直接连接。 The client usages UDP to broadcast finding requests for any endpoint supporting a specified contract type. 客户端使用UDP广播对支持指定合同类型的任何端点的查找请求。 The discovery endpoints that support this contract will receive the request. 支持该合同的发现端点将收到请求。 The implementation of the discovery endpoint responds back to the client with the address of the service endpoints. 发现端点的实现将使用服务端点的地址响应客户端。 Once the client determines the services, it invokes the service to set up call. 一旦客户端确定了服务,它将调用服务以建立呼叫。

Simple usage example: http://www.codeproject.com/Articles/469549/WCF-Discovery 简单用法示例: http : //www.codeproject.com/Articles/469549/WCF-Discovery

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

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