如何在c#中更改soap xml web服务?
我使用了webservice首选项cs代码。 我有soap xml web服务请求如何在c#中更改soap xml web服务?,c#,xml,web-services,soap,soap-client,C#,Xml,Web Services,Soap,Soap Client,我使用了webservice首选项cs代码。 我有soap xml web服务请求 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <List xmlns
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<List xmlns="http://ws.ddd.com/" wsNo="451" member="XYZ">
<dateStart xmlns="">20180708</dateStart>
<dateFinish xmlns="">20180708</dateFinish>
<typeX xmlns="">0</type>
</List>
</s:Body>
</s:Envelope>
20180708
20180708
0
但我想改变这一点,我是如何做到的?谢谢
20180708
20180708
0
我终于找到了方法。
首先,我创建了EnvelopeNamespaceMessage类
EnvelopeNamespaceMessage.cs
public class EnvelopeNamespaceMessage : Message
{
private readonly Message message;
public string[] EnvelopeNamespaces { get; set; }
public EnvelopeNamespaceMessage(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("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
if (EnvelopeNamespaces != null)
{
foreach (string ns in EnvelopeNamespaces)
{
var tokens = ns.Split(new char[] { ':' }, 2);
writer.WriteAttributeString("xmlns", tokens[0], null, tokens[1]);
}
}
}
}
public class EnvelopeNamespaceMessageFormatter : IClientMessageFormatter
{
private readonly IClientMessageFormatter formatter;
public string[] EnvelopeNamespaces { get; set; }
public EnvelopeNamespaceMessageFormatter(IClientMessageFormatter formatter)
{
this.formatter = formatter;
}
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
var message = this.formatter.SerializeRequest(messageVersion, parameters);
return new EnvelopeNamespaceMessage(message) { EnvelopeNamespaces = EnvelopeNamespaces };
}
public object DeserializeReply(Message message, object[] parameters)
{
return this.formatter.DeserializeReply(message, parameters);
}
}
[AttributeUsage(AttributeTargets.Method)]
public class EnvelopeNamespacesAttribute : Attribute, IOperationBehavior
{
public string[] EnvelopeNamespaces { get; set; }
public void AddBindingParameters(OperationDescription operationDescription,
BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
//var serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
IOperationBehavior serializerBehavior = operationDescription.Behaviors.Find<XmlSerializerOperationBehavior>();
if (serializerBehavior == null)
serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (clientOperation.Formatter == null)
serializerBehavior.ApplyClientBehavior(operationDescription, clientOperation);
IClientMessageFormatter innerClientFormatter = clientOperation.Formatter;
clientOperation.Formatter = new EnvelopeNamespaceMessageFormatter(innerClientFormatter)
{
EnvelopeNamespaces = EnvelopeNamespaces
};
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
}
public void Validate(OperationDescription operationDescription)
{
}
}
公共类EnvelopeNamespaceMessage:Message
{
私有只读消息;
公共字符串[]信封面{get;set;}
公共信封消息(消息消息消息)
{
this.message=消息;
}
公共覆盖消息头
{
获取{返回this.message.Headers;}
}
公共重写消息属性
{
获取{返回this.message.Properties;}
}
公共覆盖消息版本
{
获取{返回this.message.Version;}
}
受保护的重写无效OnWriteStartBody(XmlDictionaryWriter编写器)
{
writer.writeStarElement(“Body”http://schemas.xmlsoap.org/soap/envelope/");
}
受保护的重写无效OnWriteByContents(XmlDictionaryWriter编写器)
{
this.message.WriteBodyContents(writer);
}
受保护的重写无效OnWriteStartInvelope(XmlDictionaryWriter编写器)
{
writer.writeStarteElement(“soapenv”、“信封”、”http://schemas.xmlsoap.org/soap/envelope/");
if(EnvelopeNamespaces!=null)
{
foreach(信封面中的字符串ns)
{
var tokens=ns.Split(新字符[]{':'},2);
WriteAttributeString(“xmlns”,标记[0],null,标记[1]);
}
}
}
}
公共类EnvelopeNamespaceMessageFormatter:IClientMessageFormatter
{
专用只读IClientMessageFormatter格式化程序;
公共字符串[]信封面{get;set;}
公共信封消息格式化程序(IClientMessageFormatter格式化程序)
{
this.formatter=格式化程序;
}
公共消息序列化请求(MessageVersion MessageVersion,对象[]参数)
{
var message=this.formatter.SerializeRequest(messageVersion,参数);
返回新的EnvelopeNamespaceMessage(message){EnvelopeNamespaces=EnvelopeNamespaces};
}
公共对象反序列化reply(消息,对象[]参数)
{
返回this.formatter.DeserializeReply(消息、参数);
}
}
[AttributeUsage(AttributeTargets.Method)]
公共类EnvelopeNamespacesAttribute:属性,IOperationBehavior
{
公共字符串[]信封面{get;set;}
public void AddBindingParameters(OperationDescription OperationDescription,
BindingParameterCollection(绑定参数)
{
}
public void ApplyClientBehavior(操作说明操作说明,客户端操作客户端操作)
{
//var serializerBehavior=operationDescription.Behaviors.Find();
IOperationBehavior serializerBehavior=operationDescription.Behaviors.Find();
if(serializerBehavior==null)
serializerBehavior=operationDescription.Behaviors.Find();
if(clientOperation.Formatter==null)
serializerBehavior.ApplyClientBehavior(operationDescription,clientOperation);
IClientMessageFormatter innerClientFormatter=clientOperation.Formatter;
clientOperation.Formatter=新的EnvelopeNamespaceMessageFormatter(innerClientFormatter)
{
EnvelopeNamespaces=EnvelopeNamespaces
};
}
public void ApplyDispatchBehavior(操作说明操作说明,调度操作调度操作)
{
}
公共无效验证(OperationDescription OperationDescription)
{
}
}
然后我打开proxy.cs类并添加了EnvelopeNamespaces属性
at方法。
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://ws.xyz.com/", ConfigurationName = "WebServiceImpl")]
//[ServiceContract(Namespace = "")]
public interface WebServiceImpl
{
// CODEGEN:
[System.ServiceModel.OperationContractAttribute(Action = "http://ws.xyz.com/WebServiceImpl/methodRequest", ReplyAction = "http://ws.xyz.com/WebServiceImpl/methodResponse")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
[EnvelopeNamespaces(EnvelopeNamespaces = new string[] {
"soapenv:http://schemas.xmlsoap.org/soap/envelope/",
"soapenc:http://schemas.xmlsoap.org/soap/encoding/",
"xsd:http://www.w3.org/2001/XMLSchema",
"xsi:http://www.w3.org/2001/XMLSchema-instance",
"ws:http://ws.xyz.com/"
})]
methodResponse method(methodRequest request);
[System.ServiceModel.OperationContractAttribute(Action = "http://ws.xyz.com/WebServiceImpl/methodRequest", ReplyAction = "http://ws.xyz.com/WebServiceImpl/methodResponse")]
System.Threading.Tasks.Task<methodResponse> methodAsync(methodRequest request);
[System.ServiceModel.ServiceContractAttribute(命名空间=”http://ws.xyz.com/,ConfigurationName=“WebServiceImpl”)]
//[ServiceContract(名称空间=”)]
公共接口WebServiceImpl
{
//CODEGEN:
[System.ServiceModel.OperationContractAttribute(操作=”http://ws.xyz.com/WebServiceImpl/methodRequest,ReplyAction=http://ws.xyz.com/WebServiceImpl/methodResponse")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
[EnvelopeNamespaces(EnvelopeNamespaces=新字符串[]]{
“soapenv:http://schemas.xmlsoap.org/soap/envelope/",
“soapenc:http://schemas.xmlsoap.org/soap/encoding/",
“xsd:http://www.w3.org/2001/XMLSchema",
“xsi:http://www.w3.org/2001/XMLSchema-instance",
“ws:http://ws.xyz.com/"
})]
方法响应方法(方法请求);
[System.ServiceModel.OperationContractAttribute(操作=”http://ws.xyz.com/WebServiceImpl/methodRequest,ReplyAction=http://ws.xyz.com/WebServiceImpl/methodResponse")]
System.Threading.Tasks.TaskMethodAsync(methodRequest请求);
这是帮助链接
我终于找到了做这件事的方法。 首先,我创建了EnvelopeNamespaceMessage类 EnvelopeNamespaceMessage.cs
public class EnvelopeNamespaceMessage : Message
{
private readonly Message message;
public string[] EnvelopeNamespaces { get; set; }
public EnvelopeNamespaceMessage(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("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
if (EnvelopeNamespaces != null)
{
foreach (string ns in EnvelopeNamespaces)
{
var tokens = ns.Split(new char[] { ':' }, 2);
writer.WriteAttributeString("xmlns", tokens[0], null, tokens[1]);
}
}
}
}
public class EnvelopeNamespaceMessageFormatter : IClientMessageFormatter
{
private readonly IClientMessageFormatter formatter;
public string[] EnvelopeNamespaces { get; set; }
public EnvelopeNamespaceMessageFormatter(IClientMessageFormatter formatter)
{
this.formatter = formatter;
}
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
var message = this.formatter.SerializeRequest(messageVersion, parameters);
return new EnvelopeNamespaceMessage(message) { EnvelopeNamespaces = EnvelopeNamespaces };
}
public object DeserializeReply(Message message, object[] parameters)
{
return this.formatter.DeserializeReply(message, parameters);
}
}
[AttributeUsage(AttributeTargets.Method)]
public class EnvelopeNamespacesAttribute : Attribute, IOperationBehavior
{
public string[] EnvelopeNamespaces { get; set; }
public void AddBindingParameters(OperationDescription operationDescription,
BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
//var serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
IOperationBehavior serializerBehavior = operationDescription.Behaviors.Find<XmlSerializerOperationBehavior>();
if (serializerBehavior == null)
serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (clientOperation.Formatter == null)
serializerBehavior.ApplyClientBehavior(operationDescription, clientOperation);
IClientMessageFormatter innerClientFormatter = clientOperation.Formatter;
clientOperation.Formatter = new EnvelopeNamespaceMessageFormatter(innerClientFormatter)
{
EnvelopeNamespaces = EnvelopeNamespaces
};
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
}
public void Validate(OperationDescription operationDescription)
{
}
}
公共类EnvelopeNamespaceMessage:Message
{
私有只读消息;
公共字符串[]信封面{get;set;}
公共信封消息(消息消息消息)
{
this.message=消息;
}
公共覆盖消息头
{
获取{返回this.message.Headers;}
}
公共覆盖MessagePro