【发布时间】:2015-09-15 16:05:56
【问题描述】:
在我的 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
}
如果请求的操作不存在,我想返回自定义 FaultException(而不是返回 null 并在我的代码中出现 ArgumentNullException 异常)。
我该怎么做?
编辑:
结果为“throw new FaultException(new ValidationException());”说明:
<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:48:23.199Z</u:Created>
<u:Expires>2015-09-15T07:53:23.199Z</u:Expires>
</u:Timestamp>
</o:Security>
</s:Header>
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
<faultstring xml:lang="fr-FR">The creator of this fault did not specify a Reason.</faultstring>
<detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"/>
<InnerException i:nil="true"/>
<Message>The creator of this fault did not specify a Reason.</Message>
<StackTrace>at Demo.Demo11WcfService.DispatchByBodyElementOperationSelector.SelectOperation(Message& message) in d:\Activite\TRACA\DEV\Main\DemoSolution2\DemoSolution2\Demo.Demo11WcfService\DispatchByBodyOperationSelector.cs:line 47
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.CustomDemuxer.GetOperation(Message& request)
at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)</StackTrace>
<Type>System.ServiceModel.FaultException`1[Demo.DomainModel.Demo11.ValidationException]</Type>
</ExceptionDetail>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
预期结果:
<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>
【问题讨论】: