webservice调用上的FaultException错误

时间:2013-01-28 13:06:16

标签: c# wcf faultexception

我创建了一个从数据库返回某些类别的Web服务。如果我和客户一起测试WCF提供的一切都很完美。 我开始建立一个客户端。我在服务http://localhost/Transaction/transaction.svc中添加了服务引用。我创建了一个客户端webservice的新实例

  TransactionClient tc = new TransactionClient("BasicHttpEndpoint");
 Category[] availableCategories = tc.GetAllCategories();

我在第二行代码上得到Object reference not set to an instance of an object。端点名称是正确的。

知道错误的原因吗?

PS:如果您需要更多代码,请告诉我要发布的内容。 提前谢谢。

编辑:

  [OperationContract]
  List<Category> GetAllCategories();

  Implementation : 
  public List<Category> GetAllCategories()
   { return db.GetAllCategories()}

该服务正在使用WCFClient进行测试,因此我的其余代码必须是corect。

这是从数据库中获取项目的代码。我试着发布解决方案,但应用程序没有停止。

List<Category> response = new List<Category>();
            connect();

            SqlCommand cmd = new SqlCommand("select id_category, name from tbl_category", conn);
            try
            {
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Category new_category = new Category();
                    new_category.id_category = int.Parse(dr["id_category"].ToString());
                    new_category.name = dr["name"].ToString();
                    response.Add(new_category);
                }

            }
            catch (SqlException ex)
            {
                Console.Out.Write(ex.ToString());
            }
            finally
            {
                dr.Close();
                conn.Close();
            }

            return response;

1 个答案:

答案 0 :(得分:1)

FaultException是从WCF频道的另一侧传输的异常。意思是,该异常不是在你调用tc.GetAllCategories();的情况下发生的,而是在服务器端,在处理该方法时。

FaultException包装在服务器端发生的异常。从我们在您粘贴的内容中可以看到,它是NullReferenceException。要找到它出现的确切位置,请在GetAllCategories方法中设置断点并逐步执行,直至失败。由于这是一个WCF服务,处理方法调用中的异常不会使服务崩溃,而是包装异常并将其发送回客户端。

找到错误发生位置的另一种方法是调试服务,打开Debug - &gt; Visual Studio中的异常和“em>公共语言运行时异常旁边的Thrown列中的勾选复选框”。这告诉VS调试器在发生错误时停止执行,即使WCF会捕获异常。

相关问题