问题描述
我有一个WCF服务,我在其中更改肯定响应的前缀,但是在错误响应中,仅更改了部分消息.
I have a WCF Service where I am changing the prefix of a positive response, however on a fault response only part of the message is being changed.
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<s:Fault
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-GB">Unable to satisfy web service request at this time.This may relate to the format or sequence of the requests, the status of the requested information or reflect a service issue.</faultstring>
</s:Fault>
</SOAP-ENV:Body>
SOAP-ENV是正确的语法,但是正如记录故障时所看到的那样,前缀为s:
.
SOAP-ENV is the correct syntax however as you can see when logging the fault the prefix is s:
.
正在进行工作的班级在这里
The class that is doing the work is here
public class ProposalMessage : Message
{
private readonly Message message;
public ProposalMessage(Message message)
{
this.message = message;
}
public override MessageHeaders Headers
{
get { return this.message.Headers; }
}
public override MessageProperties Properties
{
get { return this.message.Properties; }
}
public override MessageVersion Version
{
get { return this.message.Version; }
}
protected override void OnWriteStartBody(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Body", "http://schemas.xmlsoap.org/soap/envelope/");
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
this.message.WriteBodyContents(writer);
}
protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
writer.WriteStartElement("SOAP-ENV", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
}
}
,我也需要删除此名称空间.
and I also need to strip out the namespace of this too.
我尝试了各种各样的事情,但都没有奏效.
I have tried in the a variety of things, none of which have worked.
下面是我尝试过的方法之一的示例
below is an example of one of the methods \ things I have tried
protected override void OnWriteDetail(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Fault", "http://schemas.xmlsoap.org/soap/envelope/");
}
但是这不适合覆盖
在这里 https://docs.microsoft.com/zh-cn/dotnet/api/system.servicemodel.channels.messagefault.onwritedetail?view=netframework-4.7.2 我是无法获得任何工作.
Having looked at some of the documentation here https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channels.messagefault.onwritedetail?view=netframework-4.7.2 I am unable to get anything to work.
任何有关格式化故障消息的帮助都将非常有用
Any and all help on formatting the fault message will be greate
推荐答案
您可以尝试以下解决方案:
You can try the following solution:
public class CustomMessage : Message
{
private readonly Message message;
public CustomMessage(Message message)
{
this.message = message;
}
public override MessageHeaders Headers
{
get { return this.message.Headers; }
}
public override MessageProperties Properties
{
get { return this.message.Properties; }
}
public override MessageVersion Version
{
get { return this.message.Version; }
}
protected override void OnWriteStartBody(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Body", "http://schemas.xmlsoap.org/soap/envelope/");
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
this.message.WriteBodyContents(writer);
}
protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
writer.WriteStartElement("SOAP-ENV", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
}
}
这是CustomMessage.
This is CustomMessage.
public class MyCustomMessageFormatter : IDispatchMessageFormatter
{
private readonly IDispatchMessageFormatter formatter;
public MyCustomMessageFormatter(IDispatchMessageFormatter formatter)
{
this.formatter = formatter;
}
public void DeserializeRequest(Message message, object[] parameters)
{
this.formatter.DeserializeRequest(message, parameters);
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
var message = this.formatter.SerializeReply(messageVersion, parameters, result);
return new CustomMessage(message);
}
}
这是MyCustomMessageFormatter.
This is MyCustomMessageFormatter.
[AttributeUsage(AttributeTargets.Method)]
public class MyMessageAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) { }
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { }
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
var serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dispatchOperation.Formatter == null)
{
((IOperationBehavior)serializerBehavior).ApplyDispatchBehavior(operationDescription, dispatchOperation);
}
IDispatchMessageFormatter innerDispatchFormatter = dispatchOperation.Formatter;
dispatchOperation.Formatter = new MyCustomMessageFormatter(innerDispatchFormatter);
}
public void Validate(OperationDescription operationDescription) { }
}
这是MyMessageAttribute.我们将MyCustomMessageFormatter添加到行为中.
This is MyMessageAttribute.We add MyCustomMessageFormatter to the behavior.
[MyMessage]
public Result GetUserData(string name)
{....
我们将刚定义的行为添加到方法中.
We add the behavior we just defined to the method.
这篇关于WCF消息格式化程序未格式化故障消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!