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

        }

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

代码下载:点击下载

 

转载于:https://www.cnblogs.com/tianboblog/p/5677546.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值