【问题标题】:WCF service throws exception but client never receives itWCF 服务抛出异常,但客户端从未收到它
【发布时间】:2013-11-26 11:54:45
【问题描述】:

在我的 wcf 服务中,我向服务客户端公开了方法。此方法使用 FaultContract 属性进行修饰,因此我可以返回并捕获自定义异常。

在我拥有的 IBookService 中

 [FaultContract(typeof(BookServiceAuthorizeFault))]
 [OperationContract]
 IEnumerable<string> GetAllBookTitlesFromSpecificGenre(string genre);

自定义类型的错误被包裹在里面

[DataContract]
public sealed class BookServiceAuthorizeFault
{
   private string _Message;
   public BookServiceAuthorizeFault(string message)
   {
      _Message = message;        
   }

   [DataMember]
   public string Message 
   {
      get { return _Message;  }
      set { _Message = value; }
   }
 }

实现此接口的 Insude Service 我故意抛出异常

public IEnumerable<string> GetAllBookTitlesFromSpecificGenre(string genre)
{
   throw new FaultException<BookServiceAuthorizeFault>(new BookServiceAuthorizeFault("Unauthorize access"));    
}

最后在客户端我想捕捉这个异常

catch (FaultException<BookServiceAuthorizeFault> bsaf)
{
     MessageBox.Show(bsaf.Message);
}

但是我这个 catch 块永远不会被命中,我在服务端遇到了未处理的用户异常 在public IEnumerable&lt;string&gt; GetAllBookTitlesFromSpecificGenre(string genre)

我在这里做错了什么?

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    我想说,如果您在 Visual Studio 中更改调试设置,您的解决方案应该可以正常工作。

    转到Tools -> Options -> Debugging -> General 并取消选中“Enable the exception assistant”。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多