如何使用.NET检查Web服务是否可用

时间:2021-11-20 16:54:35

If a .NET application needs to call a web service is there a good solution for checking if the web service is currently up and available, outside of calling a specific web method?

如果.NET应用程序需要调用Web服务,那么在调用特定Web方法之外,是否有一个很好的解决方案来检查Web服务当前是否已启动且可用?

On my web services I usually include some sort of status method for testing, but this becomes an issue when using a third party web service.

在我的Web服务上,我通常会包含某种状态测试方法,但这在使用第三方Web服务时会成为一个问题。

3 个解决方案

#1


Another solution is just to do a simple HTTP get on the URL of the service. And look at the response code (i.e. 404 etc). The cool thing about this is that you don't have to post any data or call any methods.

另一个解决方案就是在服务的URL上做一个简单的HTTP get。并查看响应代码(即404等)。关于这一点很酷的是你不必发布任何数据或调用任何方法。

#2


Nope.

One thing you might do is create a ServiceIsUp method that does nothing, only returning true. If your server was hung for whatever reason, your request would timeout.

你可能做的一件事是创建一个什么都不做的ServiceIsUp方法,只返回true。如果您的服务器因任何原因挂起,您的请求将会超时。

#3


You would need to call a method regardless, otherwise you wouldn't know if the web service itself was running. I.e. The server could be running but the web service stopped.

您无论如何都需要调用方法,否则您将不知道Web服务本身是否正在运行。即服务器可能正在运行但Web服务已停止。

We do this for some of our web services and it exposes neat functionality. We can have it return true/false and a failed call or false means its down. This lets you "return true;" always or conditionally return false if you choose to, such as blocking specific clients or turning the service off without actually stopping the web service, etc.

我们为一些Web服务执行此操作,并且它提供了简洁的功能。我们可以让它返回true / false并且失败的调用或false表示它失败。这让你“回归真实”;如果您选择,则始终或有条件地返回false,例如阻止特定客户端或关闭服务而不实际停止Web服务等。

#1


Another solution is just to do a simple HTTP get on the URL of the service. And look at the response code (i.e. 404 etc). The cool thing about this is that you don't have to post any data or call any methods.

另一个解决方案就是在服务的URL上做一个简单的HTTP get。并查看响应代码(即404等)。关于这一点很酷的是你不必发布任何数据或调用任何方法。

#2


Nope.

One thing you might do is create a ServiceIsUp method that does nothing, only returning true. If your server was hung for whatever reason, your request would timeout.

你可能做的一件事是创建一个什么都不做的ServiceIsUp方法,只返回true。如果您的服务器因任何原因挂起,您的请求将会超时。

#3


You would need to call a method regardless, otherwise you wouldn't know if the web service itself was running. I.e. The server could be running but the web service stopped.

您无论如何都需要调用方法,否则您将不知道Web服务本身是否正在运行。即服务器可能正在运行但Web服务已停止。

We do this for some of our web services and it exposes neat functionality. We can have it return true/false and a failed call or false means its down. This lets you "return true;" always or conditionally return false if you choose to, such as blocking specific clients or turning the service off without actually stopping the web service, etc.

我们为一些Web服务执行此操作,并且它提供了简洁的功能。我们可以让它返回true / false并且失败的调用或false表示它失败。这让你“回归真实”;如果您选择,则始终或有条件地返回false,例如阻止特定客户端或关闭服务而不实际停止Web服务等。