这个重新抛出的例外有什么问题吗?

时间:2016-03-26 16:09:34

标签: c# .net wcf exception-handling

我正在使用wcf Service将数据插入到数据库中,而我的服务崩溃则表示异常未处理。我正在尝试将异常从一个WCF服务方法传递给其他WCF方法,并从那里向客户端抛出异常。

这是我的代码:

WCF服务的数据插入方法:向DB插入数据的方法

public int insertStatements(SqlCommand cmd)
{           
  Try
  { 
   //insert data to the db

  }
 catch(SqlException ex)
 {
   if (ex.Number == 2627) // if unique key constraint error
    {
     throw new FaultException( "error reason", new FaultCode("Error Code: "); 
    }
    else
    {
      throw new FaultException("DB error: ", new FaultCode("Error Code: " +);
    }
}
 catch (FaultException ex)
 {
     throw new FaultException("Unknown Error",new FaultCode("Unknown Error"));
 }
 }

WCF插入位置方法,即服务公开方法(公共)

public int insertLocation (string name)
{
try
 {
     // this method communicates with client
    dataconnection.insertStatements(cmd);  

 }    
  catch
  {
     throw; // Here i'm getting error
  }
}

在我的客户端:winform应用程序

 try
 {
    AreaDataServicesClient DataObject = new AreaDataServicesClient("BasicHttpBinding_IAreaDataServices");
     int rowsAffected = DataObject.InsertProvince(ProvinceObject.AreaName, ProvinceObject.AreaKm);
      return rowsAffected;
  }
  catch(FaultException ex)
  {
     messagebox.show("erro occured");
  }

这是我得到的错误: “EMSDataServices.dll中发生了'System.ServiceModel.FaultException'类型的异常,但未在用户代码中处理”

为什么service方法不会将异常传递给客户端。

3 个答案:

答案 0 :(得分:1)

我找到了。代码没有任何问题。我正在调试模式下运行服务,并且在异常设置中“如果用户代码未处理则在此处中断”被检查,因此它停止了服务以执行进一步的操作,并且客户端由于服务没有响应而崩溃。

我取消选中它并按预期工作。

答案 1 :(得分:0)

在insertStatements中@Mohsin,catch(FaultException ex)是没用的。它永远不会捕获你的例外。因为你在方法之外抛出异常。您应该在重新抛出的某个地方处理异常。您可以在insertLocation方法中执行此操作。请参阅波纹管修改后的代码。

public int insertLocation(string name)
        {
            try
            {
                dataconnection.insertStatements(cmd);
            }
            catch (FaultException ex)
            {
                // you should handle the exception here. Do not retrow here too. Otherwise you may need to handle somewhere else again.
            }
        }
        public int insertStatements(SqlCommand cmd)
        {
            try
            {
                //insert data to the db

            }
            catch (SqlException ex)
            {
                if (ex.Number == 2627) // if unique key constraint error
                {
                    throw new FaultException("error reason", new FaultCode("Error Code: ");
                }
                else
                {
                    throw new FaultException("DB error: ", new FaultCode("Error Code: ");
                }
            }
        }

答案 2 :(得分:0)

标记为///Here i'm getting the error的行是throw语句。这正是它应该做的事情。它已经捕获了已被捕获的异常(因为它在try中),并使其再次未被处理,就像它从未被捕获一样。

如果您想记录或检查异常然后让它冒泡,那么您可以做些什么。在这种情况下,它与删除整个try / catch完全相同。

此:

public int insertLocation (string name)
{
    try
    {
        dataconnection.insertStatements(cmd);  
    }    
    catch
    {
        throw; // Here i'm getting error
    }
}

与此相同:

public int insertLocation (string name)
{
    dataconnection.insertStatements(cmd);  
}
相关问题