您对此方法的看法:
故障助手:
[Serializable]
public class WcfHwServiceFault
{
private readonly Guid _guid;
private readonly byte[] _data;
private WcfHwServiceFault(Guid guid, byte[] data)
{
_guid = guid;
_data = data;
}
public static WcfHwServiceFault Create(Exception ex)
{
var formatter = new SoapFormatter(null, new StreamingContext(StreamingContextStates.CrossMachine));
var ms = new MemoryStream();
formatter.Serialize(ms, ex);
return new WcfHwServiceFault(ex.GetType().GUID, ms.GetBuffer());
}
public Exception Get()
{
var formatter = new SoapFormatter(null, new StreamingContext(StreamingContextStates.CrossMachine));
var ms = new MemoryStream(_data);
return (Exception)formatter.Deserialize(ms);
}
}
服务器端用法:
try
{
...
}
catch (Exception ex)
{
throw new FaultException<WcfHwServiceFault>(WcfHwServiceFault.Create(ex));
}
客户端使用:
try
{
Channel.DoSomeMethod(...);
}
catch (FaultException<WcfHwServiceFault> ex)
{
throw ex.Detail.Get();
}
catch (CommunicationException ex)
{
throw new ApplicationException("Communication error.", ex);
}
答案 0 :(得分:0)
有趣的想法。它可以节省您使用100个个别例外来装饰您的服务。
但为什么不将异常作为WcfHwServiceFault的属性?