Webservice 对象失败处理

Webservice object fail handling

我在 C# 中创建对象时遇到有关 try catch 的问题。

我的问题出现在应该创建对象但定义该对象的网络服务不可用或已被修改时。 这就是我希望我的程序能够处理的问题。

当我尝试这样做时:

try
{
    var Customer = new Customer();
}
catch
{
      //handling the exception.
}

稍后在我的程序中我需要那个特定的对象,但是由于 try catch 该对象并非在所有情况下都可用(当然,当 try 失败时,该对象不会被创建)。

没有 if(customer != null)

if (insertCustomer(lead, LS_TS, out Customer) == true)
{
    insert = true;
}

with if(customer != null)

if(customer != null)
{
    if (insertCustomer(lead, LS_TS, out Customer) == true)
    {
        insert = true;
    }
}

无论如何,编译器都会说:“名称'Customer'在当前上下文中不存在

如何解决这个问题?我需要一个程序一直 运行,当检查一个未创建的对象时,我需要退出该方法,稍后再试。

提前致谢

你可以这样做:

Customer customer = null;

try
{
    customer = new Customer();
}
catch
{
    // handling the exception.
}

并且每当您需要使用对象 customer 时,您都应该这样做

if(customer != null)
{
    // do stuff
}