检查服务器是否可以在.NET中访问的最佳方法?

时间:2021-09-26 22:10:29

I have created a timeclock application in C# that connects to a web service on our server in order to clock employees in/out. The application resides in the system tray and clocks users out if they shut down/suspend their machines or if they are idle for more than three hours to which it clocks them out at the time of last activity.

我在C#中创建了一个时钟应用程序,它连接到我们服务器上的Web服务,以便为员工提供进/出时钟。应用程序驻留在系统托盘中,如果用户关闭/挂起他们的计算机,或者如果他们在上次活动时闲置超过三个小时,则会将用户计时。

My issue arises that when a user brings his machine back up from a sleep state (which fires the SystemEvents.PowerModeChanged event), the application attempts to clock the employee back in but the network connection isn't fully initialized at that time and the web-service call times out.

我的问题出现了,当用户将他的机器从休眠状态(触发SystemEvents.PowerModeChanged事件)恢复时,应用程序尝试重新启动员工,但网络连接在那时并未完全初始化 - 服务呼叫超时。

An obvious solution, albeit it a hack, would be to put a delay on the clock in but this wouldn't necessarily fix the problem across the board. What I am looking to do is a sort of "relentless" clock in where it will wait until it can see the server until it actually attempts to clock in.

一个明显的解决方案,虽然它是一个黑客,将是延迟时钟,但这不一定能解决所有问题。我期待做的是一种“无情”的时钟,它会一直等到它能看到服务器,直到它真正尝试进入。

What is the best method to determine if a connection to a web service can be made?

确定是否可以建立与Web服务的连接的最佳方法是什么?

5 个解决方案

#1


3  

The best way is going to be to actually try to make the connection and catch the errors. You can ping the machine, but that will only tell you if the machine is running and on the network, which doesn't necessarily reflect on whether the webservice is running and available.

最好的方法是实际尝试建立连接并捕获错误。您可以ping机器,但这只会告诉您机器是否在运行并且在网络上,这不一定反映Web服务是否正在运行且可用。

#2


0  

When handling the event, put your connection code into a method that will loop through until success, catching errors and retrying.

处理事件时,将连接代码放入一个循环的方法,直到成功,捕获错误并重试。

Even a delay wouldn't be perfect as depending on the individual systems and other applications running it can take varying times for the network connection to be re-established.

即使延迟也不是完美的,因为取决于各个系统,运行它的其他应用程序可能需要不同的时间来重新建立网络连接。

#3


0  

Implement a queue where you post messages and have a thread periodically try to flush the in-memory queue to the web service.

实现发布消息的队列,并让线程定期尝试将内存中的队列刷新到Web服务。

#4


0  

if the problem is latency in re-establishing the network service, Ping is the solution; it's like ringing the doorbell to see if anyone is home

如果问题是重新建立网络服务的延迟,Ping就是解决方案;这就像敲响门铃,看看是否有人回家

if ping succeeds, then try calling the web service, catching exceptions appropriately (I think both SocketException and SoapException can occur depending on readiness/responsiveness)

如果ping成功,那么尝试调用Web服务,适当地捕获异常(我认为SocketException和SoapException都可以发生,具体取决于准备/响应)

#5


0  

Ping can be disabled although the web service port is open. I wouldn't use this method...

尽管Web服务端口已打开,但可以禁用Ping。我不会用这种方法......

#1


3  

The best way is going to be to actually try to make the connection and catch the errors. You can ping the machine, but that will only tell you if the machine is running and on the network, which doesn't necessarily reflect on whether the webservice is running and available.

最好的方法是实际尝试建立连接并捕获错误。您可以ping机器,但这只会告诉您机器是否在运行并且在网络上,这不一定反映Web服务是否正在运行且可用。

#2


0  

When handling the event, put your connection code into a method that will loop through until success, catching errors and retrying.

处理事件时,将连接代码放入一个循环的方法,直到成功,捕获错误并重试。

Even a delay wouldn't be perfect as depending on the individual systems and other applications running it can take varying times for the network connection to be re-established.

即使延迟也不是完美的,因为取决于各个系统,运行它的其他应用程序可能需要不同的时间来重新建立网络连接。

#3


0  

Implement a queue where you post messages and have a thread periodically try to flush the in-memory queue to the web service.

实现发布消息的队列,并让线程定期尝试将内存中的队列刷新到Web服务。

#4


0  

if the problem is latency in re-establishing the network service, Ping is the solution; it's like ringing the doorbell to see if anyone is home

如果问题是重新建立网络服务的延迟,Ping就是解决方案;这就像敲响门铃,看看是否有人回家

if ping succeeds, then try calling the web service, catching exceptions appropriately (I think both SocketException and SoapException can occur depending on readiness/responsiveness)

如果ping成功,那么尝试调用Web服务,适当地捕获异常(我认为SocketException和SoapException都可以发生,具体取决于准备/响应)

#5


0  

Ping can be disabled although the web service port is open. I wouldn't use this method...

尽管Web服务端口已打开,但可以禁用Ping。我不会用这种方法......