• 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();
            }
  • 相关阅读:
    oracle 分配表权限给用户的写法
    任务的一种写法:
    解决oracle 32位64位的问题
    设计模式学习
    Nginx 相关介绍
    (2) html 语义化
    (1)HTML5的常用新特性你必须知道
    less初学手记
    如何修改chrome记住密码后自动填充表单的黄色背景 ?
    HTML的水平居中和垂直居中解决方案
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/3860394.html
Copyright © 2020-2023  润新知