在我的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
。
我该怎么做?
编辑:
结果&#34;抛出新的FaultException(new ValidationException());&#34;说明:
<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>
答案 0 :(得分:0)
由于我无法发表评论,我将此留在此处(如果不合适,请随时删除答案):
您是否尝试过抛出自定义FaultException
而不是返回null
?