问如何在客户端获取原始异常(在服务器上发生)?
我正在使用自托管的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; }
}
答案 0 :(得分:4)
您是否尝试过捕获FaultException?
catch (FaultException<WcfException> ex)
{
AppManager.LogException(ex);
}
catch (FaultException unknownFault)
{
AppManager.LogException(unknownFault);
}
您需要在合同中加入FaultContractAttribute才能实现此目的。
答案 1 :(得分:3)
什么是WcfException
,是否可序列化?
在任何情况下,我建议您按照this MSDN article中的说明在服务器上启用跟踪 - 这可以帮助您诊断问题。
答案 2 :(得分:2)
如果您想获得有关原始异常的信息,则可以使用:
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
答案 3 :(得分:1)
感谢您的所有答案。我的问题出在服务界面,即 FaultContractAttribute 的 Action 属性中,它指向错误的URL。
[ServiceContract]
public interface IServiceOperation : IOperation
{
...
[OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
[FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/IServiceOperation/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
ServiceResponse VerifyAction(ServiceRequest request);
}
我通过删除所有其他属性(自动确定)并仅保留WcfFault的类型来解决它
[ServiceContract]
public interface IServiceOperation : IOperation
{
...
[OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
[FaultContractAttribute(typeof(WcfException))]
ServiceResponse VerifyAction(ServiceRequest request);
}