zoukankan      html  css  js  c++  java
  • 三种调用WCF服务的代码

     wsHttpBinding,Massage UserName认证

      static void Main(string[] args)
            {
                //使用服务引用方式生成的Client调用服务,使用自动生成的配置文件 WSHttpBinding_IService
                using (ServiceClient client = new ServiceClient())
                {
                    client.ClientCredentials.UserName.UserName = "name";
                    client.ClientCredentials.UserName.Password = "123";
                    string result = client.Hello("SomeOne");
                    Console.WriteLine(result);
                }
    
                //使用配置方件 WSHttpBinding_IService_1 创建通道调用服务
                using (ChannelFactory<TAePService_Contracts.IService> ChannelFactory = new ChannelFactory<TAePService_Contracts.IService>("WSHttpBinding_IService_1"))
                {
              var loginCredentials = ChannelFactory.Endpoint.Behaviors.Find<ClientCredentials>();
                    loginCredentials.UserName.UserName = "name";
                    loginCredentials.UserName.Password = "123";
                    var proxy = ChannelFactory.CreateChannel();
                    using (proxy as IDisposable)
                    {
                        var result = proxy.Hello("SomeOne");
                        Console.WriteLine(result);
                    }
                }
    
                //纯代码方式创建通道调用服务
                WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message);
                binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
                binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
                EndpointAddress address = new EndpointAddress(new Uri("http://192.168.126.129:12122/"), EndpointIdentity.CreateDnsIdentity("ServiceCA"));
                using (ChannelFactory<Service_Contracts.IService> channelFactory = new ChannelFactory<Service_Contracts.IService>(binding, address))
                {
                    ClientCredentials loginCredentials = channelFactory.Endpoint.Behaviors.Find<ClientCredentials>();
                    loginCredentials.UserName.UserName = "name";
                    loginCredentials.UserName.Password = "123";
                    loginCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; //不认证服务证书           
                    Service_Contracts.IService proxy = channelFactory.CreateChannel();
                    proxy = channelFactory.CreateChannel();
                    using (proxy as IDisposable)
                    {
                        var result = proxy.Hello("SomeOne");
                        Console.WriteLine(result);
                    }
                };
    
                Console.ReadKey();
            }
  • 相关阅读:
    samtools使用过程中出现的问题
    转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系
    韦恩图的画法
    python的计算保留小数
    awk的输出格式控制:print 和printf
    awk遇到windows 的^M
    从引物序列出发查找pcr产物的内容和在基因组上的位置
    八.Windows内核保护机制--页保护3--PDE PTE属性
    九.Windows内核保护机制--TSS
    七.Windows内核保护机制--陷阱门
  • 原文地址:https://www.cnblogs.com/TianPing/p/10089739.html
Copyright © 2011-2022 走看看