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();
            }
  • 相关阅读:
    洛谷-P5729 【深基5.例7】工艺品制作
    洛谷-P5728 【深基5.例5】旗鼓相当的对手
    洛谷-P5727 【深基5.例3】冰雹猜想
    洛谷-P1720 月落乌啼算钱
    洛谷-P4956 [COCI2017-2018#6] Davor
    洛谷-P1075 质因数分解
    洛谷-P1420 最长连号
    洛谷-P1307 数字反转
    回调地址
    OAuth 2.0
  • 原文地址:https://www.cnblogs.com/TianPing/p/10089739.html
Copyright © 2011-2022 走看看