zoukankan      html  css  js  c++  java
  • wcf系列(一)--- 寄宿方式

     

    一、自我寄宿(self-hosting)

      1、wcf采用基于终结点(Endpoint)的通信手段;终结点由:地址(Address)+绑定(Binding)+契约(Contract)组成;  Enpoint=ABC.

      2、终结点的添加和服务行为的定义

        (1)通过代码的方式

          

    static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
                {
                    host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:3721/calculatorservice");
                    if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                    {
                        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                        behavior.HttpGetEnabled = true;
                        behavior.HttpGetUrl = new Uri("http://127.0.0.1:3721/calculatorservice/metadata");
                        host.Description.Behaviors.Add(behavior);
                    }
                    host.Opened += delegate
                    {
                        Console.WriteLine("服务已经启动,按任意键终止!");
                    };
                    host.Open();
                    Console.Read();
                }
            }

        (2)wcf服务配置编辑器

            工具-->wcf服务配置编辑器,配置完成会自动生成配置文件内容; 

          

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
        <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="CalculatorService.CalculatorService"
                    name="WSHttpBinding_CalculatorService">
                    <identity>
                        <userPrincipalName value="" />
                    </identity>
                </endpoint>
            </client>
        </system.serviceModel>
    </configuration>

          调用:

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

          也可以使用ChannelFactory<ICalculator>创建代理:

     static void Main(string[] args)
            {
                using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(),
                    "http://127.0.0.1:3721/calculatorservice"))
                {
                    ICalculator proxy = channelFactory.CreateChannel();
                    Console.WriteLine(proxy.Multiply(1, 2));
                    Console.ReadKey();
                }
            }

    二、iis寄宿

      1、新建空的web应用程序

      2、新建Service.svc文件:新建项目-->Visual C#-->直接在名称框输入:Service.svc保存即可

      3、打开Service.svc,内容修改为:<%@ServiceHost Service="Service.CalculatorService" %>作用就是映射到服务

      4、定义的接口拷贝到当前应用程序下,修改web.config;由于服务端采用不加密的方式传输,所以客户端也相应的配置如此

    <system.serviceModel>
        <bindings>
          <wsHttpBinding>
            <binding name="bindingConfiguration1">
              <security mode="None">
                <transport clientCredentialType="None"/>
                <message clientCredentialType="None"/>
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="metadataBehaviors" >
              <!-- 将下列元素添加到服务行为配置中。 -->
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <services>
          <!-- 注意: 服务名称必须与服务实现的配置名称相匹配。 -->
          <service name="Service.CalculatorService" behaviorConfiguration="metadataBehaviors" >
            <!-- 添加下列终结点。 -->
            <!-- 注意: 服务必须有一个 http 基址以便添加此终结点。 -->
            <endpoint contract="ServiceI.Interface.ICalculator" binding="wsHttpBinding" bindingConfiguration="bindingConfiguration1"/>
          </service>
        </services>
    
      </system.serviceModel>

      5、发布之后,生成相关dll和Service.svc文件,在iis中像发布网站一样寄宿服务;在浏览器输入http:*****/Service.sv就可以访问了

      static void Main(string[] args)
            {
                WSHttpBinding WShb = new WSHttpBinding();//使用协议与服务端相同
                WShb.Security.Mode = SecurityMode.None; //安全级别
                EndpointAddress epo = new EndpointAddress("http://*********/Service.svc");//指定WCF服务地址
    
    
                using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(WShb, epo))
                {
    
                    ICalculator proxy = channelFactory.CreateChannel();
    
                    Console.WriteLine(proxy.Multiply(1, 2));
                    Console.ReadKey();
                }
    
            }

    以上是使用代码方式创建服务通道,也可以直接引用服务调用;

    代码下载:点击下载

  • 相关阅读:
    poj 3528 (三维几何求凸包+凸包表面积)
    dijkstra模板(好像是斐波那契额堆优化,但我为什么看起来像优先队列优化,和spfa一样)
    最大空凸包模板
    ICPC 2017–2018, NEERC, Northern Subregional Contest St Petersburg, November 4, 2017 I题
    hdu 5248 序列变换
    hdu 2063(二分图模板测试)
    组合数
    85. Maximal Rectangle 由1拼出的最大矩形
    750. Number Of Corner Rectangles四周是点的矩形个数
    801. Minimum Swaps To Make Sequences Increasing 为使两个数组严格递增,所需要的最小交换次数
  • 原文地址:https://www.cnblogs.com/tianboblog/p/5677546.html
Copyright © 2011-2022 走看看