Q如何在客户端获取原始异常(发生在服务器上)?
我正在使用自托管的 WCF 服务和 C# 4 并尝试设置正确的异常处理。
我有一个看起来像这样的客户
private ServiceResponse PerformRemoteAction(ServiceRequest request, ProcessOperations operation)
{
...
try
{
//remote call
switch (operation)
{
...
case ProcessOperations.VerifyAction: { response = client.VerifyAction(request); break; }
default: throw new NotImplementedException();
}
}
catch (Exception ex)
{
AppManager.LogException(ex);
}
return response;
}
及服务实施
public override ServiceResponse VerifyAction(ServiceRequest request)
{
RegisterRequest(request);
try
{
var chain = new VerificationChain();
Responce = chain.Run(request);
}
catch (Exception ex)
{
throw new FaultException<WcfException>(ExceptionFactory.Create(ex),
new FaultReason(ex.Message));
}
SetSuccessfulResponce();
return Responce;
}
服务 web.config 有
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
这是我原来的例外
这就是我在客户端上得到的
如果需要,我可以在客户端发布完整的详细信息,但是客户端上的异常没有对原始异常的任何引用。
更新
这是我定义的FaultContractAttribute接口
[ServiceContract]
public interface IServiceOperation : IOperation
{
...
[OperationContract(Action = "http://tempuri.org/ITestRunnerService/VerifyAction", ReplyAction = "http://tempuri.org/ITestRunnerService/VerifyAction")]
[FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/ITestRunnerService/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
ServiceResponse VerifyAction(ServiceRequest request);
}
这是我的WcfException类
[DataContract]
public class WcfException : Exception
{
[DataMember]
public string Title { get; set; }
[DataMember]
public new string Message { get; set; }
[DataMember]
public new string InnerException { get; set; }
[DataMember]
public new string StackTrace { get; set; }
}