如何根据Name参数选择FaultContractAtribute

时间:2016-06-06 14:53:17

标签: c# wcf faultcontract

我必须实现生成两个不同SystemFaults的接口

Interface:
[ServiceContract]
public interface IErrorProvider
{
[OperationContract(Action = "getError")]
[FaultContractAttribute(typeof(string), Action = "getError", Name = "Error1Fault")]
[FaultContractAttribute(typeof(string), Action = "getError", Name = "Error2Fault")]
string getError(int iArg);
}

并实施:

Code:
public class ErrorProvider : IErrorProvider
{
  public getError(int iArg)
  {
    if(iArg == 1)
      throw new FaultException<string>("Error1Fault","you asked err 1");
    if(iArg == 2)
      throw new FaultException<string>("Error2Fault","you asked err 2");
    return "No error";
  }
}

现在,当我使用

调用此服务时
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Body>
    <getError xmlns="http://tempuri.org/">
        <iArg>1</iArg>
    </getError>
  </Body>
</Envelope>

我得到回复:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <s:Fault>
        <faultcode>s:Client</faultcode>
        <faultstring xml:lang="fi-FI">you asked err 1</faultstring>
        <detail>
            <Error2Fault xmlns="http://tempuri.org/">Error1Fault</Error2Fault>
        </detail>
    </s:Fault>
  </s:Body>
</s:Envelope>

清楚地表明使用了第一次投掷。我需要得到的是:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <s:Fault>
        <faultcode>s:Client</faultcode>
        <faultstring xml:lang="fi-FI">you asked err 1</faultstring>
        <detail>
            <Error1Fault xmlns="http://tempuri.org/">Error1Fault</Error1Fault>
        </detail>
    </s:Fault>
  </s:Body>
</s:Envelope> 

如何获得?而且我没有机会改变另一方,所以不知何故我需要解决这个问题,真的是新手问题,但我找不到真正的解决方案。所有文件告诉我抛出自制对象,但我无法理解它是如何帮助的,因为这些只是错误字符串。

0 个答案:

没有答案