zoukankan      html  css  js  c++  java
  • wcf服务编程(一)

    步骤一:定义契约

    [ServiceContract]     //定义服务契约   需要引用System.ServiceModel
        public interface ICalculator
        {
            [OperationContract]   //定义消息契约
            double Add(double x, double y);
            [OperationContract]
            double Subtract(double x, double y);
            [OperationContract]
            double Multiply(double x, double y);
            [OperationContract]
            double Divide(double x, double y);
        }

    步骤二:创建服务

    public class CalculatorService:ICalculator   //继承服务契约
        {
            public double Add(double x, double y)
            {
                return x + y;
            }
            public double Subtract(double x, double y)
            {
                return x - y;
            }
            public double Multiply(double x, double y)
            {
                return x * y;
            }
            public double Divide(double x, double y)
            {
                return x / y;
            }
        }

    步骤三:服务寄宿(通过控制台寄宿)

    static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) // 承载服务
                {
                    host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://localhost:3721/CalculatorService");  // 增加节点到承载服务,参数是:契约、绑定、地址
                    if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                    {
                        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();     //定义服务行为
                        behavior.HttpGetEnabled = true;             //是否发布服务元数据,方便使用GET-HTTP进行检索
                        behavior.HttpGetUrl = new Uri("http://localhost:3721/CalculatorService/metadata");  //获取或设置元数据的发布地址
                        host.Description.Behaviors.Add(behavior);   //添加服务行为到承载服务
                    }
                    host.Opened += delegate
                    {
                        Console.WriteLine("服务已经开启,按任意键终止");
                    };
                    host.Open();
                    Console.Read();
                }
            }

    步骤四:服务端通过配置文件配置服务

    xml:

    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="metadataBehavior">
              <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3722/CalculatorService/metadata"/>
            </behavior>
          </serviceBehaviors>      
        </behaviors>
        
        <services>
          <service name="Service.CalculatorService" behaviorConfiguration="metadataBehavior">
            <endpoint address="http://127.0.0.1:3722/CalculatorService" binding="wsHttpBinding"
                bindingConfiguration="" name="Test" contract="Service.Interface.ICalculator" />
          </service>
        </services>
      </system.serviceModel>
    </configuration>

    c#

    static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) // 承载服务
                {              
                    host.Opened += delegate
                    {
                        Console.WriteLine("服务已经开启,按任意键终止");
                    };
                    host.Open();
                    Console.Read();
                }
            }

    步骤五:客户端调用 (不通过代理类的方式)

    static void Main(string[] args)
            {
                //客户端调用,不通过代理类,通过ChannelFactory直接创建服务代理对象
                //条件:需要引用契约动态库
                using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3722/CalculatorService"))
                {
                    ICalculator calcuator = channelFactory.CreateChannel();  //创建服务代理对象
                    Console.WriteLine("5+5={0}", calcuator.Add(5, 5));
                    Console.WriteLine("5-5={0}", calcuator.Subtract(5, 5));
                    Console.WriteLine("5*5={0}", calcuator.Multiply(5, 5));
                    Console.WriteLine("5/5={0}", calcuator.Divide(5, 5));
                }
                Console.Read();
            }
  • 相关阅读:
    -Dmaven.test.skip=true 和 -DskipTests
    mysql 生成指定范围随机数
    Delphi 6 保存窗体设置
    Sql server left join,right join和inner join的比较
    Mysql配置文件my.ini详解
    网络游戏MMORPG服务器架构
    彻底删除SVN版本库中部分文件或文件夹
    高负载高并发应用 参考索引 地址
    Linux计划任务入门详解
    Linux下SVN的三种备份方式
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/3860394.html
Copyright © 2011-2022 走看看