如何在C#中向肥皂信封添加名称空间 [英] How can I add a namespace to soap envelope in c#

查看:127
本文介绍了如何在C#中向肥皂信封添加名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的肥皂信封中添加一个命名空间设置. 我想在IClientMessageInspector的BeforeSendRequest中更改它,或者您有一个更优雅的方法.

I want to add a namespace setting to my soap envelope. I would like to change it in the BeforeSendRequest in an IClientMessageInspector or you have a more elegant way.

例如

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
 <s:Header>
  <wsa:To xmlns="http://www.w3.org/2005/08/addressing">ws://xxx/V1</wsa:To>
  ...
 </s:Header>
 <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  ...
 </s:Body>
</s:Envelope>

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
...

请帮助我!

谢谢!

推荐答案

根据您的描述,我认为您可以使用WCF消息检查器.在客户端发送消息之前.我们可以自定义消息正文.
https://docs.microsoft.com/zh-我们/dotnet/framework/wcf/samples/message-inspectors
我做了一个演示,希望它对您有用.
服务器端.

According to your description, I think you could use WCF Message Inspector. Before the client send the message. We could customize the message body.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/message-inspectors
I have made a demo, wish it is useful to you.
Server end.

class Program
    {

        static void Main(string[] args)
        {
            Uri uri = new Uri("http://localhost:1500");
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.TransferMode = TransferMode.Buffered;
            binding.Security.Mode = BasicHttpSecurityMode.None;
            ServiceHost sh = new ServiceHost(typeof(Calculator),uri);
            sh.AddServiceEndpoint(typeof(ICalculator), binding, "");
            ServiceMetadataBehavior smb;
            smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
            {
                smb = new ServiceMetadataBehavior();
                //smb.HttpGetEnabled = true;
                sh.Description.Behaviors.Add(smb);
            }
            Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
            sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "MEX");


            sh.Open();
            Console.Write("Service is ready....");
            Console.ReadLine();
            sh.Close();
        }
    }
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract,WebGet]
        double Add(double a, double b);

    }

    public class Calculator : ICalculator
    {
        public double Add(double a, double b)
        {
            return a + b;
        }

}

客户.

class Program
    {
        static void Main(string[] args)
        {
            ServiceReference2.CalculatorClient client = new ServiceReference2.CalculatorClient();
            try
            {
                var result = client.Add(34, 20);
                Console.WriteLine(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
    public class ClientMessageLogger : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            string result = $"server reply message:\n{reply}\n";
            Console.WriteLine(result);
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {

            XmlDocument doc = new XmlDocument();
            MemoryStream ms = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(ms);
            request.WriteMessage(writer);
            writer.Flush();
            ms.Position = 0;
            doc.Load(ms);

            ChangeMessage(doc);

            ms.SetLength(0);
            writer = XmlWriter.Create(ms);

            doc.WriteTo(writer);
            writer.Flush();
            ms.Position = 0;
            XmlReader reader = XmlReader.Create(ms);
            request = System.ServiceModel.Channels.Message.CreateMessage(reader, int.MaxValue, request.Version);
            string result = $"client ready to send message:\n{request}\n";
            Console.WriteLine(result);
            return null;
        }
        void ChangeMessage(XmlDocument doc)
        {
            XmlElement element = (XmlElement)doc.GetElementsByTagName("s:Envelope").Item(0);
            if (element != null)
            {
                element.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
                element.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            }
        }
    }
    public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
    {
        public Type TargetContract => typeof(ICalculator);

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            return;
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
            return;
        }
}  

别忘了将CustContract行为应用于服务界面.

Don’t forget apply the CustContractbehavior to the service interface.

    [CustContractBehavior]
public interface ICalculator {

结果.

这篇关于如何在C#中向肥皂信封添加名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆