zoukankan      html  css  js  c++  java
  • WCF步步为营(三):使用配置文件改变使用服务的方式

    1. 打开上节的解决方案,为JackWangServiceClient工程添加一个App.config文件

    image

    2. 修改App.config的文件如下

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <system.serviceModel>

        <client>     

            <endpoint  name="MyEndPoint" binding="basicHttpBinding" 

              contract="JackWangServiceClient.ICalc" address="http://localhost:9000/Add" />         

        </client

      </system.serviceModel>

    </configuration>

    3. 修改Program.cs文件,绿色是注释掉的部分,由于使用配置文件,我们需要使用ChannelFactory<T>的实例。

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.ServiceModel;

    namespace JackWangServiceClient

    {

        [ServiceContract]

        public interface ICalc

        {

            [OperationContract]

            long Add(int a, int b);

        }

        class Program

        {

            static void Main(string[] args)

            {

                //ICalc proxy = ChannelFactory<ICalc>.CreateChannel(new BasicHttpBinding(),

                //    new EndpointAddress("http://localhost:9000/Add"));

                ChannelFactory<ICalc> channelFactory = new ChannelFactory<ICalc>("MyEndPoint");

     

                ICalc proxy = channelFactory.CreateChannel();

                long result = proxy.Add(50, 60);

                Console.Out.WriteLine("result from server is:" + result);

                Console.ReadLine();

            }

        }

    }

    这样,当服务器修改配置方式时,我们不用改变代码,直接修改配置文件即可。

    扫码关注公众号,了解更多管理,见识,育儿等内容

    作者: 王德水
    出处:http://www.cnblogs.com/cnblogsfans
    版权:本文版权归作者所有,转载需经作者同意。

  • 相关阅读:
    android高级UI之Paint Xfermode
    android高级UI之Paint滤镜
    常见文献管理软件
    linux下10款markdown软件
    markdown页面内跳转
    Ubuntu18.04配制阿里巴巴的源
    python中TAB补全
    word中手动添加endnote的加载项
    MarkDown添加图片的三种方式
    word前页与后页页码断开
  • 原文地址:https://www.cnblogs.com/cnblogsfans/p/1234582.html
Copyright © 2011-2022 走看看