从零开始学WCF(3)配置服务

配置服务概念

在设置和实现服务协定后,即可配置服务。在其中可以定义和自定义如何向客户端公开服务,包括指定可以找到服务的地址(IP)、服务用于发送和接收消息的传输(HTTP/TCP)和消息代码,以及服务需要的安全类型。

配置服务的类型:

1) 使用配置文件配置。(App.config)

2) 在代码中强制配置。(没有办法在后期部署的时候修改,不建议使用)

 

实际上,编写配置是WCF应用程序编程的主要部分。

 

使用配置文件配置WCF服务

1) 通过使用配置文件配置WCF服务,可提供在部署时而非设计时提供终结点和服务行为数据的灵活性。

2) 可使用.NET Framework配置技术对WCF服务进行配置。通常情况下,向承载WCF服务的Internet信息服务(IIS)网站的Web.config文件添加XML元素。通过这些元素,可以逐台计算机更改详细信息,例如终结点地址(用于与服务进行通信的实际地址)。此外,WCF包括几个系统提供的元素,可用于快速选择服务的最基本的功能。

3) WCF使用.NET Framwork的System.Configuration配置系统

      使用IIS来承载服务——使用Web.config文件;

      使用任何其他宿主环境——使用或App.config文件制定设置。

 

 

1. Service元素

每个服务都具有以下属性:

—— name 制定提供 协定的实现的类型。这事完全限定名称(命名空间和实现WCF接口类型名称)。

—— behaviorConfiguration 指定一个在behaviors元素中找到的behavior元素的名称。指定的行为控制操作,例如服务是否允许模拟。

<!--1.  Service name需要修改为我们所修改的类服务Behavior设置成标记的类,并且还要添加behaviorConfiguration(要与下面的behavior同名即可)-->
      <service name="Video2.WcfServiceLibrary2.DuplexService.CalculatorService" behaviorConfiguration="Video2.WcfServiceLibrary2.DuplexService.CalculatorServiceBehavior">


2. endpoint元素

每个终结点都需要以下属性表示的地址、绑定和协定:

—— address 指定服务的统一资源标示符(URI),它可以是一个绝对地址,或是一个相对于服务基地址给定的地址。如果设置为空字符串,则只是在创建服务的ServiceHost时,终结点在指定的基地址上可用。

—— binding 通常,指定一个类似WsHttpBinding的系统提供的绑定,但也可以指定一个用户定义的绑定。指定的绑定确定传输协议类型、安全和使用的编码,以及是否支持或启动可靠会话、事务或流。

—— bindingConfiguration  如果必须修改绑定的默认值,则可通过在bindings元素中配置相应的binding元素来执行此操作。此属性应赋予与用于更改默认值的binding元素的name属性相同的值。

—— contract  指定定义协定的接口。这是在由service元素的name属性指定的公共语言运行库(CLR)类型中实现的接口。

<endpoint address ="" binding="wsDualHttpBinding" contract="Video2.WcfServiceLibrary2.DuplexService.ICalculatorDuplex">


如果address为空,那么就会调用baseAddress地址来使用

<baseAddresses>
            <!--2.  修改Address WCF服务地址-->
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/Video2.WcfServiceLibrary2.DuplexService/CalculatorService/" />
          </baseAddresses>


3. binding元素

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.ServiceModel>
    
    <bindings>
      <basicHttpBinding>
        <!--关闭时间的设定-->
        <binding name="myBindingConfiguration1" closeTimeout="00:01:00"/>
        <binding name="myBindingConfiguration2" closeTimeout="00:02:00"/>
      </basicHttpBinding>
    </bindings>

    <services>
      <service name="myServiceType">
        <!--绑定配置为myBindingConfiguration1,在上面的basicHttpBinding里的name为myBindingConfiguration1-->
        <endpoint address="myAddress" binding="basicHttpBinding" bindingConfiguration="myBindingConfiguration1"/>
      </service>
    </services>
    
  </system.ServiceModel>
</configuration>


4. behaviors元素

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.ServiceModel>

    <behaviors>
      <endpointBehaviors>
        <behavior name="myBehavior">
          <!--如果endpoint使用了这个行为,会在回调的调试里把错误包含进去-->
          <callbackDebugincludeExceptionDetailInFaults="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="NewServiceType">
        <!--把myBehavior与endpoint的行为关联起来-->
        <endpoint address="myAddress" behaviorConfiguration="myBehavior" binding="basicHttpBinding"/>
      </service>
    </services>
    
  </system.ServiceModel>
</configuration>


绑定

绑定是用于指定连接到WCF服务终结点所必须的通信详细信息的对象。WCF服务中的每个终结点都要求正确指定绑定。

绑定最起码必须指定要使用的传输(如HTTP或TCP)。还可以通过绑定来设置其他特征,如安全和事务支持。

绑定中的信息可能非常基本,也可能非常复杂。最基本的绑定仅指定必须用于连接到终结点的传输协议(如HTTP)。一般来说,绑定包含的有关如何连接到终结点的信息属于以下类别中的一种:

——协定   确定要使用的安全机制:可靠消息传递功能或事物上下文流设置。

——编码   确定消息编码(例如,文本或二进制)。

——传输   去诶那个要使用的基础传输协议(例如,TCP或HTTP)。

绑定中的信息可能十分复杂,而且某些设置可能与其他设置不兼容。因此,WCF包含一组系统提供的绑定。这些绑定是为了满足大多数应用程序要求。下面的类表示系统绑定的绑定的一些是例:

——BasicHttpBinding:一个HTTP协议绑定,适合于连接到符合WS-I基本配置文件规范的Web服务(例如,基于ASP.NET Web服务的服务)。安全型及消息加密很差,编码格式是文本的。

——WSHttpBinding:一个可互操作的绑定,适用于连接到符合WS-*协议的终结点。包含安全、消息加密,比BasicHttpBinding安全。

——NetNamedPipeBinding:使用.NET Framework连接到同一计算机上的其他WCF终结点。性能很高。

——NetMsmqBinding:使用.NET Framework创建于其他WCF终结点的排队消息连接。

 

使用自己的绑定

如果系统提供的绑定都不具有服务应用程序所需的正确功能组合,则可以创建自己的绑定。有两种方法可以实现此目的。

可以使用CustomBinding对象从预先存在的绑定元素创建新的绑定。也可以通过从Binding绑定派生来创建完全由用户定义的绑定。

 

使用绑定

使用绑定需要执行两个基本步骤:

1) 选择或定义绑定。最简单的方法就是选择WCF包含的系统提供绑定中的一个,并且通过该绑定的默认设置来使用它。

2) 创建一个使用所选择或定义的绑定的终结点。

 

代码和配置

可以通过两种方式来定义绑定:通过代码或通过配置

这两种方法与使用的系统提供的绑定还是自定义绑定无关。通常,使用代码可以使在设计时对绑定的定义拥有完全的控制。

另一方面,使用配置则使系统管理员或WCF服务或客户端的用户可以更改绑定的参数,而不必重新编译服务应用程序。由于无法预测用于部署WCF应用程序的特定计算机的要求,因而通常需要这种灵活性。通过将绑定(和寻址)信息保持在代码外部,人们可以更改这些信息,而不必重新编译或重新部署应用程序。

请注意,代码中定义的绑定是在配置中指定的绑定之后创建的,这使得代码定义的绑定可以覆盖配置中定义的任何绑定。所以代码配置的优先权高于配合文件中的配置。

 

DEMO演示

使用配置文件配置在之前的Demo里就可以看到,这里不在举例子。

这里来介绍一下使用代码来配置,

新建一个控制台应用程序,引用WCF所需的System.ServiceModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Video3.ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用代码的方式来Host 下面的WCF服务

            //定义地址
            String baseAddress = "http://localhost/CalculatorService";
            //创建绑定方式为BasicHttpBinding用来提供WCF服务
            BasicHttpBinding binding1 = new BasicHttpBinding();
            binding1.CloseTimeout = new TimeSpan(0, 2, 0); //绑定关闭时间为2分钟

            //实例化WCF服务类
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
            {
                //添加WCF终结点
                host.AddServiceEndpoint(typeof(ICalculator), binding1, baseAddress);
                host.Open();  //启动WCF host
            }

            Console.Read();
        }
    }

    //创建WCF接口服务
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }

    //实现WCF接口服务
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }

        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }

        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }

        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }

}


 


终结点

与WCF服务的所有通信是通过该服务的终结点进行的。利用终结点,客户端可以访问WCF服务提供的功能。

终结点的结构:

—— 地址。 地址是唯一标示终结点并告知潜在客户服务的所在位置(例如http://localhost/Servce)。

——绑定。 绑定指定如何与终结点进行通信(例如wsHttpBinding)。

——服务协定。 服务协定概述了终结点向客户端公开的功能(也就是WCF服务接口类)。

指定服务的终结点有两种方法:

——通过代码的强制方式或通过配置的声明方式。

 

终结点地址的定义

终结点的地址有EndpointAddress类表示,该类包含一个表示服务地址的统一资源定位符(URI)。

大多数传输的地址URI包含四个部分。例如http://www.xxx.com:322/service.svc/secureEndpoint

这个URI具有以下四个部分:

 ——方案(传输协议): http:

——计算机(部署了WCF的服务器): www.xxx.com

——端口(可选): 322

——路径: /service.svc/secureEndpoint

 

一个配置文件里,可以配置多个Endpoint终结点:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviro">
        <host>
          <baseAddresses>
            <!--基地址-->
            <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
          </baseAddresses>
        </host>
        <!--地址为空,表示终结点在基地址上-->
        <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Sample.ICalculator"/>
        <!--地址为 基地址/test-->
        <endpoint address="/test" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
        <!--net tcp的协议,使用了完整的地址,所以基地址就不起作用了-->
        <endpoint address="net.tcp://localhost:9000/servicemodelsamples/service" binding="netTcpBinding" contract="Micorosoft.ServiceModel.Samples.ICalculator"/>
        <!--元数据地址-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

</configuration>


 

元数据终结点地址

WCF服务通过发布一个或多个元数据终结点来发布元数据(元数据为说明我们服务的各个接口、参数类型、返回值等的说明文件)。主要用于在开发调试的过程中使用,但在实际部署到服务器后,元数据终结点是没有什么用处的。

发布服务元数据之后,可以通过标准协议(如 WS-MetadataExchange(MEX)和HTTP/GET请求)来使用该元数据。

元数据终结点类似于其他服务终结点:他们都有一个地址、一个绑定和一个协定,并且他们都可通过配置或使用代码添加到服务主机。

若要启动发布元数据终结点,必须将ServiceMetadataBehavior服务行为添加到该服务。默认情况下,WCF服务不发布元数据终结点,所以必须将他们显式添加到服务才能为服务启用元数据发布。

元数据绑定有一下几种:

——mexHttpBinding: 对于HTTP发布。

——mexHttpsBinding: 对于HTTPS发布。

——mexNamedPipeBinding: 对于命名管道发布。

——mexTcpBinding: 对于TCP发布。

contract协定必须设定为:IMetadataExchage,这是系统默认的。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviro">
        <host>
          <baseAddresses>
            <!--基地址-->
            <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
          </baseAddresses>
        </host>
        <!--地址为空,表示终结点在基地址上-->
        <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Sample.ICalculator"/>
        <!--地址为 基地址/test-->
        <endpoint address="/test" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
        <!--net tcp的协议,使用了完整的地址,所以基地址就不起作用了-->
        <endpoint address="net.tcp://localhost:9000/servicemodelsamples/service" binding="netTcpBinding" contract="Micorosoft.ServiceModel.Samples.ICalculator"/>
        <!--元数据地址-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <!--必须建议一个行为,把元数据关联的行为进行指定-->
        <behavior name="SimpleServiceBehavior">
          <!--必须设定为true;policyVersion为发布的版本号-->
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy12"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值