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();
            }
  • 相关阅读:
    MySQL——MySQL用户与数据库的关系
    Kafka——Kakfa的设计思想
    Hibernate建表——将MySQL的JSON列映射到Java字段(Jpa/Hibernate——Java)
    Spring Boot——Apache Commons包作用说明
    word转html工具
    html导出word
    html导出pdf
    获取dubbo-admin.war(自己maven编译)
    ajax提交表单中文呈现乱码解决
    JVM优化
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/3860394.html
Copyright © 2011-2022 走看看