【问题标题】:How to catch exception if a webservice or a window service is down?如果 web 服务或窗口服务关闭,如何捕获异常?
【发布时间】:2017-03-20 10:32:26
【问题描述】:

我知道这很愚蠢,但我想知道在 web 服务关闭的情况下使用哪个异常类。我将 web 服务的 url 地址更改为其他内容,以便将异常抛出为 “没有端点列为...”

一旦我遇到了上面的记录,它应该被一个适当的异常捕获。

下面是我用于超时异常的代码。 N 现在我想处理一个没有列出端点的异常。

try
        {
            var result = proxy.Geocode(ADDRESSVERIFICATIONAPISOURCE);
            if (result != null)
            {
                // Display result of geocoding result.
                retAddresses.AddRange(
                    result.Select(
                        item =>
                        new AddressContent
                        {
                            Address = item.GeocodedAddress.Address,
                            City = item.GeocodedAddress.City,
                            State = item.GeocodedAddress.State,
                            Zip = item.GeocodedAddress.Zip,
                            Status = item.Status,
                            StatusFlag = item.StatusFlag,
                            SourceId = item.GeocodedAddress.SourceId
                        }));
            }
        }
        catch (TimeoutException ex)
        {
            retAddresses.AddRange(
                   addresses.Select(
                       item =>
                       new AddressContent
                       {
                           Status = null,
                           StatusFlag = item.StatusFlag,
                           SourceId = item.SourceId
                       }));
        }            
        catch
        {
            retAddresses.AddRange(
                    addresses.Select(
                        item =>
                        new AddressContent
                        {
                            Status = "N",
                            StatusFlag = item.StatusFlag,
                            SourceId = item.SourceId
                        }));
        }
//Here I want to handle exception if service is down 

感谢任何帮助。

谢谢,

【问题讨论】:

  • 如果 Web 服务关闭,您会捕获什么异常?如果您不确定,那么为什么不尝试使 Web 服务脱机并运行您的代码,然后查看您捕获的异常。

标签: c# .net web-services


【解决方案1】:

我得到了答案。我使用 EndpointNotFoundException 并且它有效。谢谢大家的帮助。

谢谢,

【讨论】:

  • 我会考虑捕获CommunicationException,这比 EndpointNotFoundException 更通用。
猜你喜欢
  • 1970-01-01
  • 2017-07-03
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多