C# WCF消息格式化程序未格式化错误消息
我有一个WCF服务,我正在更改肯定响应的前缀,但是在错误响应上,只有部分消息被更改C# WCF消息格式化程序未格式化错误消息,c#,.net,wcf,soap,ierrorhandler,C#,.net,Wcf,Soap,Ierrorhandler,我有一个WCF服务,我正在更改肯定响应的前缀,但是在错误响应上,只有部分消息被更改 <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>
<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>
我还需要去掉它的名称空间
我尝试过各种各样的事情,但都没有成功
下面是我尝试过的一种方法\方法的示例
protected override void OnWriteDetail(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Fault", "http://schemas.xmlsoap.org/soap/envelope/");
}
但是,这不是一个适合覆盖的方法
看过这里的一些文档后,我无法得到任何有用的东西
有关设置故障消息格式的任何和所有帮助都将非常有用您可以尝试以下解决方案:
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");
}
}
这是我的留言
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
[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) { }
}
我们将刚刚定义的行为添加到方法中
[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) { }
}
[MyMessage]
public Result GetUserData(string name)
{....