我正在尝试在NamedPipe上传递Exception类型的对象(或其中一个子类)。
的ServiceContract:
[ServiceContract]
public interface IWCFCallback
{
[OperationContract]
void sendException(Exception e);
}
当我像这样使用它时它工作正常:
_pipeproxy.sendException(new Exception("bla bla 99"));
但是一旦我传递了一个子类:
_pipeproxy.sendException(new ArgumentException("fridgemaster 3000"));
我得到一个异常,说反序列化失败了。
我已经阅读过KnownTypes属性,但我无法弄清楚如何将它用于我自己未实现的类。
有人可以在这里给我一个提示吗?
答案 0 :(得分:2)
WCF的“最佳实践”之一是“不要序列化异常” 如果您的ServiceHost抛出异常,那么您应该使用FaultException 异常转移不安全的原因之一是,异常本身是可序列化的,但您可以从中派生,并保证您的自定义派生异常可以自行处理。
您可以将异常堆栈的数据合同对象作为字符串传递,并键入为枚举,作为解决方法。
答案 1 :(得分:1)
这可能不符合最佳做法,但您可以创建一个表示异常的DataContract - 如下所示:
/// <summary>
/// Represents errors that occur during application execution.
/// </summary>
[DataContract]
public class ExceptionInfo
{
/// <summary>
/// Gets the type of the exception.
/// </summary>
[DataMember]
public string ExceptionType
{
get;
set;
}
/// <summary>
/// Gets a message that describes the current exception.
/// </summary>
/// <returns>
/// The error message that explains the reason for the exception, or an empty string("").
/// </returns>
[DataMember]
public string Message
{
get;
set;
}
/// <summary>
/// Gets the <see cref="T:System.Exception"/> instance that caused the current exception.
/// </summary>
/// <returns>
/// An instance of Exception that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the constructor, or a null reference (Nothing in Visual Basic) if the inner exception value was not supplied to the constructor. This property is read-only.
/// </returns>
[DataMember]
public ExceptionInfo InnerException
{
get;
set;
}
/// <summary>
/// Gets a string representation of the immediate frames on the call stack.
/// </summary>
/// <returns>
/// A string that describes the immediate frames of the call stack.
/// </returns>
[DataMember]
public string StackTrace
{
get;
set;
}
/// <summary>
/// Gets or sets a link to the help file associated with this exception.
/// </summary>
/// <returns>
/// The Uniform Resource Name (URN) or Uniform Resource Locator (URL).
/// </returns>
[DataMember]
public string HelpLink
{
get;
set;
}
/// <summary>
/// Gets or sets the name of the application or the object that causes the error.
/// </summary>
[DataMember]
public string Source
{
get;
set;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionInfo"/> class.
/// </summary>
/// <param name="exception">The exception.</param>
/// <exception cref="ArgumentNullException">exception</exception>
public ExceptionInfo(Exception exception)
{
if(exception == null)
throw new ArgumentNullException("exception");
ExceptionType = exception.GetType().FullName;
HelpLink = exception.HelpLink;
Message = exception.Message;
Source = exception.Source;
StackTrace = exception.StackTrace;
if(exception.InnerException != null)
{
InnerException = new ExceptionInfo(exception.InnerException);
}
}
}
服务合同:
[ServiceContract]
public interface IWCFCallback
{
[OperationContract]
void sendException(ExceptionInfo e);
}
用法:
try
{
// .....
}
catch (Exception ex)
{
var info = new ExceptionInfo(ex);
// do something....
}