Передача исключений и подклассов через NamedPipe
Я пытаюсь передать объекты типа Exception (или один из его подклассов) через NamedPipe.
Контракт на обслуживание:
[ServiceContract]
public interface IWCFCallback
{
[OperationContract]
void sendException(Exception e);
}
он работает нормально, когда я использую его так:
_pipeproxy.sendException(new Exception("bla bla 99"));
но как только я прохожу подкласс:
_pipeproxy.sendException(new ArgumentException("fridgemaster 3000"));
Я получил исключение, заявив, что десериализация не удалась.
Я уже читал об атрибуте KnownTypes, но я не могу понять, как использовать его для классов, не реализованных мной самостоятельно.
Может кто-нибудь дать мне подсказку здесь?
2 ответа
Один из "Лучших практик" над WCF - не сериализировать исключение.
Если ваш ServiceHost выдает исключение, то вы должны использовать FaultException.
Одна из причин, по которой исключение нельзя безопасно передать, заключается в том, что само исключение является сериализуемым, но вы можете извлечь его из него и гарантировать, что ваше пользовательское производное исключение будет сериализуемым.
Вы можете передать объект контракта данных со стеком исключений в виде строки и ввести как enum, как обходной путь.
Это может не соответствовать рекомендациям, но вы можете создать 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....
}