c# – wcf FaultException原因
发布时间:2020-12-16 01:59:19 所属栏目:百科 来源:网络整理
导读:对于以下异常,在异常窗口中未指定Reason( – 查看详细信息): System.ServiceModel.FaultException The creator of this fault did not specify a Reason. How can I change it to show the reason? (I need 1234 to be shown there) public class MysFaultE
对于以下异常,在异常窗口中未指定Reason( – >查看详细信息):
public class MysFaultException { public string Reason1 { get; set; } } MyFaultException connEx = new MyFaultException(); connEx.Reason1 = "1234"; throw new FaultException<OrdersFaultException>(connEx); 解决方法
如果您希望将所有例外转发给呼叫者,I3arnon answer是好的,如果您只想要通过一组有限的已知故障,则可以创建
Fault Contracts,让调用者知道可能会遇到一组特定的异常,因此客户可以准备好处理它们.这允许您传递潜在的预期异常,而无需转发软件可能引发的所有异常.
以下是MSDN中的一个简单示例,它显示了一个WCF服务捕获DivideByZeroException并将其转换为FaultException以传递给客户端. [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface ICalculator { //... (Snip) ... [OperationContract] [FaultContract(typeof(MathFault))] int Divide(int n1,int n2); } [DataContract(Namespace="http://Microsoft.ServiceModel.Samples")] public class MathFault { private string operation; private string problemType; [DataMember] public string Operation { get { return operation; } set { operation = value; } } [DataMember] public string ProblemType { get { return problemType; } set { problemType = value; } } } //Server side function public int Divide(int n1,int n2) { try { return n1 / n2; } catch (DivideByZeroException) { MathFault mf = new MathFault(); mf.operation = "division"; mf.problemType = "divide by zero"; throw new FaultException<MathFault>(mf); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |