在服务器上我抛出这样的异常。
catch(SqlException exception)
{
if (exception.Message.Contains("Custom error from stored proc"))
{
//Exception to be thrown when authentication fails.
throw new FaultException<MyServiceFault>(new MyServiceFault { MessageText = exception.Message });
}
}
在客户端我捕获了异常
catch(FaultException<MyServiceFault> faultException)
{
}
这是我的 MyServiceFault
[DataContract]
public class MyServiceFault
{
[DataMember]
public string MessageText { get; set; }
[DataMember]
public Guid Id { get; set; }
}
问题是在客户端上,它不会进入 MyServiceFault catch 块,而是进入通信异常 catch 块并抛出此错误
System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. ---> System.Net.WebException
我还装饰了我的服务方法[FaultContract(typeof(MyServiceFault))]
在我的服务实现的界面中。
在我的 web.config servicebehaviour 标签中包含<serviceDebug includeExceptionDetailInFaults="true" />
知道我哪里出错了。
这个问题发生在Windows 7上。是否有与之相关的原因?
重要更新
根据回答者的说法,服务器上任何未处理的异常都可能导致客户端抛出通信异常,并且它可能与您在服务器上抛出的自定义错误异常无关。因此,解决方案是在服务器上记录错误并查找导致此行为的错误。这是我发现并实现的一个非常有用的开源日志记录功能,在您的项目进入生产环境后它甚至会更有用。非常感谢各位的回答。
在 WCF 中记录异常的更好方法 https://stackoverflow.com/questions/2997076/elmah-exception-logging-without-having-httpcontext