• 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();
                }
    
            }

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

    代码下载:点击下载

  • 相关阅读:
    HTML入门(一)
    WEB攻击手段及防御第2篇-SQL注入
    公司来了个新同事不会用 Lombok,还说我代码有问题!
    最流行的 RESTful API 要怎么设计?
    Spring Boot & Restful API 构建实战!
    分布式事务不理解?一次给你讲清楚!
    带着问题学 Kubernetes 架构!
    Linux 与 Unix 到底有啥区别和联系?
    Java虚拟机最多支持多少个线程?
    常用的 Git 命令,给你准备好了!
  • 原文地址:https://www.cnblogs.com/tianboblog/p/5677546.html
Copyright © 2020-2023  润新知