我想知道最佳实践方法是在服务结构中抛出/处理Actors和客户端之间的异常。
我注意到,例如,当从Actor抛出异常时,客户端收到了一组嵌套的异常。外部异常的类型为System.AggregateException,仅表示"发生了一个或多个错误"。但是,如果深入查看实际的内部异常,您会发现从Actor抛出的异常无法序列化。
Test method PoCSystem.Test.CommandHandlerTest.CommandHandler_When_ExpectExceptionThrown threw exception:
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. --->
System.AggregateException: One or more errors occurred. ---> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]:
Type 'PoCActor.Interfaces.ConcurrencyException' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
如果您随后使用DataContract和DataMember属性标记Exception,则会报告它与序列化不兼容。
服务结构中错误处理的最佳方法是什么?
答案 0 :(得分:2)
考虑使用老式.net可序列化。来自"例外"内置于visual studio的代码片段:
[Serializable]
public class MyException : Exception
{
public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception inner) : base(message, inner) { }
protected MyException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}