Csharp/C#教程:我如何在c#中更改soap xml web服务?分享


我如何在c#中更改soap xml web服务?

我使用了webservice preferences cs代码。 我有soap xml web服务请求。

   20180708 20180708 0    

但我想改变这个,我怎么做c#? 谢谢。

   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", "https://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", "https://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(); 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 = new EnvelopeNamespaceMessageFormatter(innerClientFormatter) { EnvelopeNamespaces = EnvelopeNamespaces }; } public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) { } public void Validate(OperationDescription operationDescription) { } } 

然后我打开了proxy.cs类,并在方法中添加了EnvelopeNamespaces属性。

 [System.ServiceModel.ServiceContractAttribute(Namespace = "https://ws.xyz.com/", ConfigurationName = "WebServiceImpl")] //[ServiceContract(Namespace = "")] public interface WebServiceImpl { // CODEGEN: [System.ServiceModel.OperationContractAttribute(Action = "https://ws.xyz.com/WebServiceImpl/methodRequest", ReplyAction = "https://ws.xyz.com/WebServiceImpl/methodResponse")] [System.ServiceModel.XmlSerializerFormatAttribute()] [EnvelopeNamespaces(EnvelopeNamespaces = new string[] { "soapenv:https://schemas.xmlsoap.org/soap/envelope/", "soapenc:https://schemas.xmlsoap.org/soap/encoding/", "xsd:https://www.w3.org/2001/XMLSchema", "xsi:https://www.w3.org/2001/XMLSchema-instance", "ws:https://ws.xyz.com/" })] methodResponse method(methodRequest request); [System.ServiceModel.OperationContractAttribute(Action = "https://ws.xyz.com/WebServiceImpl/methodRequest", ReplyAction = "https://ws.xyz.com/WebServiceImpl/methodResponse")] System.Threading.Tasks.Task methodAsync(methodRequest request); 

这是帮助链接。

https://weblog.west-wind.com/posts/2016/Apr/02/Custom-Message-Formatting-in-WCF-to-add-all-Namespaces-to-the-SOAP-Envelope#ClientMessageFormatter

上述就是C#学习教程:我如何在c#中更改soap xml web服务?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/997840.html

(0)
上一篇 2021年12月26日
下一篇 2021年12月26日

精彩推荐