抛出FaultException< T>从WCF服务导致'这个错误的创建者没有指定原因'。 [英] Throwing a FaultException<T> from a WCF services causes 'The creator of this fault did not specify a Reason.'

查看:208
本文介绍了抛出FaultException< T>从WCF服务导致'这个错误的创建者没有指定原因'。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当抛出 FaultException< CustomFault> 如下所示:

throw new FaultException<CustomFault>(new CustomFault("Custom fault message"));

我得到以下内容:此故障的创建者没有指定原因。
根据此MSDN文章,不需要一个使用 FaultReason

I get the following: "The creator of this fault did not specify a Reason." Now according to this MSDN article one isn't required to use the FaultReason

我有以下服务合同:

[OperationContract]
[FaultContract(typeof(CustomFault))]
CustomType[] SomeMethod(int someParameter);

[DataContract]
public class CustomFault
{
    private string report;

    public CustomFault(string message)
    {
        this.report = message;
    }

    [DataMember]
    public string Message
    {
        get { return this.report; }
        set { this.report = value; }
    }
}

有关MSDN文章的评论必须使用其他 FaultReason ,但在其他几个地方,我看到有人认为您不应该使用 FaultReason 除非必要。

There is a comment on the MSDN article that suggest it is mandatory to use the other FaultReason, but in several other places I have seen people arguing that you shouldn't use the FaultReason unless necessary.

所以我的问题如下:是否真的强制使用 FaultReason ,如果没有 - 如何防止在尝试抛出 FaultException ?

So my question is as follows; is it really mandatory to use the FaultReason and if not - how can i prevent the exception raised when trying to throw a FaultException?

编辑

通过从本文我得到同样的行为。也许是由于.NET中的更新引起的,文档/样本未被更新。

By running the sample project from this article I get the excact same behavior. Maybe it's caused by an update in .NET an the documentation/samples not being updated.

推荐答案

故障原因是。

The fault reason is required while the details are optional.

WCF使用 FaultException.Reason 属性填充< faultstring> SOAP故障的元素,这是 SOAP所要求的规范

WCF uses the value of the FaultException.Reason property to populate the <faultstring> element of the SOAP Fault, which is required by the SOAP specification:


faultstring

faultstring元素旨在提供一个人类可读的故障解释,不适用于算法
处理。 它必须存在于SOAP Fault元素中,并且
应至少提供一些解释
错误的性质的信息。

faultstring
The faultstring element is intended to provide a human readable explanation of the fault and is not intended for algorithmic processing. [...] It MUST be present in a SOAP Fault element and SHOULD provide at least some information explaining the nature of the fault.

因此,您在必须提供错误的原因来自WCF服务的/en-us/library/ms576192.aspxrel =noreferrer> FaultException

Hence, you must provide a reason for the fault when throwing a FaultException from a WCF service.

这篇关于抛出FaultException&lt; T&gt;从WCF服务导致'这个错误的创建者没有指定原因'。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆