【发布时间】:2011-08-08 16:51:26
【问题描述】:
我有一个关于如何将自定义异常作为 FaultException 发送的问题。当我使用像 ArgumentException 这样的系统异常时它可以工作,但是如果我将它更改为我的自定义异常“TestException”它会失败。当我尝试添加服务引用时,我无法获取服务引用的配置。
作品:
[OperationContract]
[FaultContract(typeof(ArgumentException))]
[TransportChannel TestMethod ();
public Void TestMethod()
{
throw new FaultException<ArgumentException>(new ArgumentException("test"), new FaultReason("test"));
}
不起作用:
[OperationContract]
[FaultContract(typeof(TestException))]
[TransportChannel TestMethod ();
public Void TestMethod()
{
throw new FaultException<TestException>(new TestException("test"), new FaultReason("test"));
}
我的“TestException”如下所示:
[Serializable()]
public class TestException: Exception
{
public TestException () : base() { }
public TestException (string message) : base(message) { }
public TestException (string message, Exception innerException) : base(message, innerException) { }
public TestException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
我想我必须在自定义对象上添加一个 DataContract,但我不明白为什么它不能像现在这样工作,因为 ArgumentException 有效。有人可以启发我吗?
感谢您的帮助:)
【问题讨论】:
-
奇数。我遇到了类似的问题,并且无需在我的自定义异常中添加
[DataContract]属性即可使其正常工作。我只是错过了采用SerializationInfo和SerializationContext的构造函数。添加该构造函数对我有用。 -
@JeffBridgman 你的意思可能是
Serialization.StreamingContext,而不是SerializationContext -
@Teejay,正确。我引用了this constructor,但输入了错误的内容。
标签: wcf exception exception-handling custom-exceptions faultexception