如何在 C# 中更改soap xml Web 服务?

Posted

技术标签:

【中文标题】如何在 C# 中更改soap xml Web 服务?【英文标题】:How I change soap xml web service in c#? 【发布时间】:2018-12-17 06:44:20 【问题描述】:

我使用了 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="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>

但我想改变这一点,我如何做到 c#?谢谢。

<s:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:a="http://ws.ddd.com/">
<a:List member="XYZ" wsNo="451">
<dateStart>20180708</dateStart>
<dateFinish>20180708</dateFinish>
<typeX >0</type>
</a:List> 
</s:Body>
</s:Envelope>

【问题讨论】:

我使用 VS.Net AddServiceRererences-> Reference.cs 将 wsdl 转换为 c# 代码。 【参考方案1】:

我终于找到了方法。 首先,我创建了 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)
        
        
    

然后我打开 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);

这是帮助链接。

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

【讨论】:

以上是关于如何在 C# 中更改soap xml Web 服务?的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义 SOAP Web 服务命名空间

C# |如何更改 XML 文档中的命名空间?

如何获取 WCF Web 服务请求的 XML SOAP 请求?

在 C# 中,如何使用大量精美的标记将 POCO 序列化为 XML?

如何在 Apache Camel 中更改 SOAP Web 服务端点 URL 和 WSDL URL

需要帮助从 C# 中的 Web 服务读取 xml 响应