C# WCF,IDispatchOperationSelector:当操作不存在时返回自定义故障异常

C# WCF,IDispatchOperationSelector:当操作不存在时返回自定义故障异常,c#,wcf,C#,Wcf,在我的WCF服务中,我使用了一个自定义的DispatchOperationSelector,如下所示: class DispatchByBodyElementOperationSelector : IDispatchOperationSelector { Dictionary<XmlQualifiedName, string> dispatchDictionary; public DispatchByBodyElementOperationSelector(Dict

在我的WCF服务中,我使用了一个自定义的
DispatchOperationSelector
,如下所示:

class DispatchByBodyElementOperationSelector : IDispatchOperationSelector
{
    Dictionary<XmlQualifiedName, string> dispatchDictionary;

    public DispatchByBodyElementOperationSelector(Dictionary<XmlQualifiedName, string> dispatchDictionary)
    {
        this.dispatchDictionary = dispatchDictionary;            
    }

    #region IDispatchOperationSelector Members

    private Message CreateMessageCopy(Message message, XmlDictionaryReader body)
    {
        Message copy = Message.CreateMessage(message.Version,message.Headers.Action,body);
        copy.Headers.CopyHeaderFrom(message,0);
        copy.Properties.CopyProperties(message.Properties);
        return copy;
    }

    public string SelectOperation(ref System.ServiceModel.Channels.Message message)
    {
        XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();

        XmlQualifiedName lookupQName = new XmlQualifiedName(bodyReader.LocalName, bodyReader.NamespaceURI);
        message = CreateMessageCopy(message,bodyReader);
        if (dispatchDictionary.ContainsKey(lookupQName))
        {
            return dispatchDictionary[lookupQName];
        }
        else
        {
            //Here : operation not found !
            //this doesn't work : throw new FaultException<ValidationException>(new ValidationException()); 

            return null;
        }
    }

    #endregion
}
预期结果:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://test.com/utility-1.0.xsd">
   <s:Header>
      <o:Security s:mustUnderstand="1" xmlns:o="http://test.com/secext-1.0.xsd">
         <u:Timestamp u:Id="_0">
            <u:Created>2015-09-15T07:49:55.610Z</u:Created>
            <u:Expires>2015-09-15T07:54:55.610Z</u:Expires>
         </u:Timestamp>
      </o:Security>
   </s:Header>
   <s:Body>
      <s:Fault>
         <faultcode>s:Client</faultcode>
         <faultstring xml:lang="fr-FR">The creator of this fault did not specify a Reason.</faultstring>
         <detail>
            <ValidationException xmlns="http://schemas.datacontract.org/2004/07/Demo.DomainModel.Demo11" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <reasonField i:nil="true"/>
            </ValidationException>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

2015-09-15T07:49:55.610Z
2015-09-15T07:54:55.610Z
s:客户
此错误的创建者未指定原因。

由于我无法发表评论,我将把这个留在这里(如果不合适,请随意删除答案):


您是否尝试过抛出一个自定义的
FaultException
而不是返回
null

,因为我显然无法发表评论,所以我将把它留在这里(如果不合适,请随意删除答案):


您是否尝试过只抛出一个自定义的
FaultException
而不是返回
null

这不是一个好的解决方案,因为如果我这样做,我会从WCF dispacher获得一个经典的FaultException。这不是我习惯的错误解释。在我的原始帖子中看到我的编辑。这不是一个好的解决方案,因为如果我这样做,我会从WCF dispacher获得一个经典的故障验证。这不是我习惯的错误解释。见我的编辑在我原来的职位。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://test.com/utility-1.0.xsd">
   <s:Header>
      <o:Security s:mustUnderstand="1" xmlns:o="http://test.com/secext-1.0.xsd">
         <u:Timestamp u:Id="_0">
            <u:Created>2015-09-15T07:49:55.610Z</u:Created>
            <u:Expires>2015-09-15T07:54:55.610Z</u:Expires>
         </u:Timestamp>
      </o:Security>
   </s:Header>
   <s:Body>
      <s:Fault>
         <faultcode>s:Client</faultcode>
         <faultstring xml:lang="fr-FR">The creator of this fault did not specify a Reason.</faultstring>
         <detail>
            <ValidationException xmlns="http://schemas.datacontract.org/2004/07/Demo.DomainModel.Demo11" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <reasonField i:nil="true"/>
            </ValidationException>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>