65.9K
CodeProject 正在变化。 阅读更多。
Home

WCF 4.0 配置更新

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (11投票s)

2012年9月30日

CPOL

5分钟阅读

viewsIcon

45530

downloadIcon

477

本文介绍 WCF 4.0 的配置模型更新。

引言

Windows Communication Foundation (WCF) 是一个用于构建面向服务应用程序的框架。它允许开发人员通过配置文件或代码来配置服务。尽管可以通过代码定义配置设置,但部署后为了便于管理,将这些设置放在配置文件中会更方便。但是,当配置文件变得过大时,就会带来麻烦。通常 WCF 配置架构非常复杂,并为用户提供了许多难以找到的特性。WCF 4.0 带来了新的特性“简化配置”,通过支持默认终结点、绑定和行为配置来消除这种麻烦。在本文中,我将介绍 WCF 4.0 配置模型的更新。我将逐步解释以下特性:

默认终结点

WCF 3.x 至少需要一个终结点,否则会引发异常。在 WCF 4.0 中,服务默认带有终结点,即运行时会自动添加一个或多个默认终结点,使得服务无需任何配置即可使用。运行时会根据每个服务契约的实现以及基地址的组合来添加默认终结点。例如,如果服务实现单个契约,并且主机配置了单个基地址,运行时将添加一个默认终结点。如果我们为主机配置了两个基地址,并且服务实现单个契约,它将添加两个默认终结点。例如,请看下面的代码。

服务契约

[ServiceContract]
public interface IMessageService
{
   [OperationContract]
   string GetMessage(string submittedMsg);
} 

实现

public class MessageService:IMessageService
{
   public string GetMessage(string submittedMsg)
   {
      if(!string.IsNullOrEmpty(submittedMsg))
      {
          return submittedMsg;
      }
         return string.Empty;
    }
}   

主机配置

using(var host=new ServiceHost(typeof(MessageService),
                new Uri("net.tcp:///netTCP"),
                new Uri("https:///http")))
            {             
                host.Open();
                host.Description.Endpoints.ToList().ForEach((endPoint)=> 
				Console.WriteLine(endPoint.ListenUri));
                Console.WriteLine("Press Enter to Exit");
                Console.Read();
            }   

在上面的代码片段中,我们看到服务实现了一个契约。并且主机配置了两个基地址,一个用于 TCP,另一个用于 HTTP。在这种情况下,运行时会自动添加两个默认终结点。

WCF 根据传输协议方案和内置 WCF 绑定之间的默认协议映射来决定为特定基地址使用哪种绑定。默认协议映射位于 .NET 4 的 machine.config.comments 文件中,如下所示:

 <system.serviceModel>
  <protocolMapping>
    <add scheme="http" binding="basicHttpBinding" bindingConfiguration="" />
    <add scheme="net.tcp" binding="netTcpBinding" bindingConfiguration=""/>
    <add scheme="net.pipe" binding="netNamedPipeBinding" bindingConfiguration=""/>
    <add scheme="net.msmq" binding="netMsmqBinding" bindingConfiguration=""/>
  </protocolMapping>
  ... 

我们可以通过将此节添加到 machine.config 并为每个协议方案更改映射来在计算机级别覆盖这些映射。或者,如果我们只想在应用程序范围内覆盖它,可以在我们的应用程序/Web 配置文件中覆盖此节。在我们的演示项目中,我们将 Http 映射到 wsHttpBinding

<protocolMapping>
   <remove scheme="http"/>
   <add scheme="http" binding="wsHttpBinding"/>
</protocolMapping>  

一旦 WCF 通过协议映射表确定了要使用的绑定,它就会在配置默认终结点时使用默认绑定配置。

默认绑定

我们可以通过简单地定义一个未命名的绑定来定义默认绑定。此绑定将对任何未指定特定绑定的内容生效。我们可以在不同的范围(如 machine.config)中定义它。例如,如果我们在演示应用程序中添加以下 app.config 文件,默认的 HTTP 终结点将采用此默认配置,从而启用 MTOM。

<bindings>
   <wsHttpBinding>
     <binding  messageEncoding="Mtom"/>      
   </wsHttpBinding>
</bindings>  

默认行为

在 WCF 3.x 中,我们必须定义命名的行为配置,通过“behaviorConfiguration”属性显式将其应用于服务和终结点。与默认绑定类似,我们也可以通过简单地定义一个未命名的行为来定义默认行为,这可以缩短当我们需要在所有服务或终结点之间共享标准默认行为配置时的工作量。以下代码片段为不带显式行为配置的服务启用服务元数据。

<serviceBehaviors>
  <behavior>
     <serviceMetadata httpGetEnabled="true"/>
  </behavior>
</serviceBehaviors>  

标准终结点

标准终结点允许用户通过使用单个属性来描述地址、绑定和契约的组合以及与其关联的附加属性,从而简化终结点定义。WCF 4.0 还附带了多个预配置的标准终结点,它们通过直接重用而无需更改即可涵盖一些最常见的场景。例如,对于 MEX 终结点,我们总是需要为服务契约指定 IMetadataExchange,并且很可能选择 HTTP。因此,WCF 提供了一个名为“mexEndpoint”的元数据交换的标准终结点定义,易于使用。我们可以通过 kind 属性按名称引用标准终结点。

<services>
      <service name="MessageService">
         <endpoint kind="mexEndpoint" address="/mex"/>
      </service>
</services>  

无文件激活

尽管 .svc 文件可以方便地公开 WCF 服务,但使用 WCF 4.0,我们不再需要物理 .svc 文件来激活服务。相反,我们可以在 config 中定义一个虚拟映射。下面的示例展示了如何配置一个激活终结点。

<services>
   <service name="FileLessActivationDemo.NoSvc">
      <endpoint kind="webHttpEndpoint" 
      contract="FileLessActivationDemo.IMessageService"/>
      <endpoint kind="mexEndpoint" address="/mex"/>
   </service>
</services>           

现在可以使用“NoSvc.svc”的相对路径(相对于 Web 应用程序的基地址)来激活 NoSvc

使用解决方案

要查看演示应用程序的输出,您需要先运行 Rashim.RND.WCF.ConfigurationModel.Server,然后运行 Rashim.RND.WCF.ConfigurationModel.Client。在 Rashim.RND.WCF.ConfigurationModel.Serverapp.config 文件中,您会看到一些我上面已经描述过的新内容。配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <protocolMapping>
      <remove scheme="http"/>
      <add scheme="http" binding="wsHttpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding  messageEncoding="Mtom"/>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="MessageService">
        <endpoint kind="mexEndpoint" address="/mex"/>
      </service>
    </services>
  </system.serviceModel>
</configuration> 

要查看无文件激活的输出,请运行项目 FileLessActivationDemo,您将看到一个目录列表,其中没有 NoSvc.svc 文件。但是,如果您在 addressbar 中输入类似 https://...../NoSvc.svc 的内容,您将看到 NoSvc.svc 被激活了。此应用程序的配置文件如下所示:

 <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="True">
      <serviceActivations>
        <add service="FileLessActivationDemo.NoSvc" relativeAddress="NoSvc.svc"/>
      </serviceActivations>
    </serviceHostingEnvironment>
    <protocolMapping>
      <remove scheme="http"/>
      <add scheme="http" binding="wsHttpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="FileLessActivationDemo.NoSvc">
        <endpoint kind="webHttpEndpoint" contract="FileLessActivationDemo.IMessageService"/>
        <endpoint kind="mexEndpoint" address="/mex"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration> 

参考文献

就这些

暂时就到这里。希望本文能帮助您了解 WCF 4.0 配置模型的更新。

© . All rights reserved.