WCF入门-客户端实现(2)

这个博客真的难。第一种方法前面已经介绍了,直接第二种方法:

二、创建代理,引用服务接口所在的类库和服务实现类的类库

 static void Main(string[] args)
        {
            //基于地址和绑定对象创建一个ChannelFactory<ICalculator> 通过代码
            using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/calculatorService"))
            {
                //创建服务代理对象
                ICalculator proxy = channelFactory.CreateChannel();
                //调用计算器方法
                Console.WriteLine("x+y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
                Console.WriteLine("x-y={2} when  x={0} and y={1}", 1, 2, proxy.Subtract(1, 2));
                Console.WriteLine("x*y={2} when  x={0} and y={1}", 1, 2, proxy.Multiply(1, 2));
                Console.WriteLine("x/y={2} when  x={0} and y={1}", 1, 2, proxy.Divide(1, 2));
                Console.ReadLine();
            }

        }

三、服务代理的内容通过配置实现(个人偏向这种方法):

static void Main(string[] args)
        {
            using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorService"))
            {
                //创建服务代理对象
                ICalculator proxy = channelFactory.CreateChannel();
                //调用计算器方法
                Console.WriteLine("x+y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
                Console.WriteLine("x-y={2} when  x={0} and y={1}", 1, 2, proxy.Subtract(1, 2));
                Console.WriteLine("x*y={2} when  x={0} and y={1}", 1, 2, proxy.Multiply(1, 2));
                Console.WriteLine("x/y={2} when  x={0} and y={1}", 1, 2, proxy.Divide(1, 2));
                Console.ReadLine();
            }
        }

客户端的配置文件,自己创建一个配置文件,app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <client>
      <endpoint name="calculatorService" address="http://127.0.0.1:3721/CalculatorService" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/>
    </client>
  </system.serviceModel>
</configuration>

其实总的来说,是两种,一种是添加服务引用,另一种是引用类库,然后使用代理。第三种方法只是用配置去实现客户端的方法罢了。仔细看添加引用服务的时候,其实他也生成了一个配置文件,哈哈。如下图:跟自己创建的区别不是特别的大:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_CalculatorService" />
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://127.0.0.1:3721/calculatorservice" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_CalculatorService" contract="Service.CalculatorService"
                name="WSHttpBinding_CalculatorService">
                <identity>
                    <userPrincipalName value="DESKTOP-F4TGBAT\Administrator" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

90e家

有C币的小伙伴可以贡献一点,哈

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值