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

WCF 4.0 中的路由服务

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (4投票s)

2011 年 9 月 28 日

CPOL

2分钟阅读

viewsIcon

24498

路由器代表入站和出站终结点之间的逻辑连接

引言

MS.NET 4.0 技术代表了基于元数据驱动模型应用程序的基础技术。本文重点介绍 WCF 4 模型中的一个独特组件,用于逻辑连接,称为 路由服务。我们将尝试将其用法扩展到由运行时存储库中存储的元数据驱动的企业应用程序。路由服务背后的基本思想是基于消息内容路由消息。它也是在您的企业应用程序架构中设计负载均衡器的良好工具。我将介绍一个简单的“如何设计简单的路由服务”。

WCF 路由服务允许以下消息交换模式 (MEP) 和合同选项

  • OneWay (SessionMode.Required, SessionMode.Allowed, TrasactionFlowOption.Allowed)
  • Duplex (OneWay, SessionMode.Required, CallbackContract, TrasactionFlowOption.Allowed)
  • RequestResponse (SessionMode.Allowed, TrasactionFlowOption.Allowed)

除了标准的 MEP 之外,路由服务还具有用于多播消息和错误处理的内置模式。下图描述了完整的输入和输出流程。

路由过程非常简单,接收到入站端点消息后,会根据消息筛选器类型表示的优先级规则,将其转发到出站端点或多个端点。评估规则从最高优先级开始。路由的复杂性取决于入站和出站端点的数量、消息筛选器的类型、MEP 和优先级。

请注意,路由的入站和出站端点之间的 MEP 模式必须相同,例如:OneWay 消息只能路由到 OneWay 端点,因此,要将 Request/Response 消息路由到 OneWay 端点,必须调用服务中介来更改 MEP,然后再返回到路由器。

Using the Code

以下代码片段描述了如何配置路由器服务端点。

<service name="System.ServiceModel.Routing.RoutingService" 
	behaviorConfiguration="RoutingService_Behavior">
        <host>
           <baseAddresses>
              <add baseAddress="http://[your service address]/RoutingService/Router" />
           </baseAddresses>
        </host>

        <endpoint
           name="PatientManagement_Router"
           address="PatientManagement"
           binding="basicHttpBinding"
           contract="System.ServiceModel.Routing.IRequestReplyRouter"
        />
        <endpoint
           name="ClinicalsPatientManagement_Router"
           address="ClinicalsPatientManagement"
           binding="basicHttpBinding"
           contract="System.ServiceModel.Routing.IRequestReplyRouter"
        />
        <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"
        />
     </service> 

在这里,我们将配置将侦听路由器的入站调用的服务

<client>
     <clear/>
     <endpoint 
        name="PatientManagement_Client" 
        address="http://[your service address]/patientmanagementservice.svc" 
        binding="basicHttpBinding"
        contract="*"
     />
     <endpoint
        name="ClinicalsPatientManagement_Client"
        address="http://[your service address]/clinicalspatientmanagementservice.svc"
        binding="basicHttpBinding"
        contract="*"
     />
  </client> 

在下面的 config 部分中,我们首先将在 <filters> 部分中声明筛选器。 在这里,我们将定义筛选器类型和筛选器数据。

<filterTables> 部分中,我们将创建 filter 表,并在这里将上述创建的筛选器映射到实际服务(将处理这种类型的传入请求)。

<routing>
     <filters>
        <filter name="PatientManagement_Filter" filterType="EndpointName" 
			filterData="PatientManagement_Router" />
        <filter name="ClinicalsPatientManagement_Filter" 
	    filterType="EndpointName" filterData="ClinicalsPatientManagement_Router"/>
     </filters>

     <filterTables>
        <filterTable name="RoutingService_FilterTable">
           <add filterName="PatientManagement_Filter" 
			endpointName="PatientManagement_Client" />
           <add filterName="ClinicalsPatientManagement_Filter" 
			endpointName="ClinicalsPatientManagement_Client"/>
        </filterTable>
      </filterTables>
  </routing>

最后,我们将托管服务,在这里我使用一个基于控制台的示例应用程序来托管此服务。

using(var serviceHost = new ServiceHost
			(typeof(System.ServiceModel.Routing.RoutingService)))
{
    var endpointCounter = 0;
    Console.WriteLine("Routing Service configured, opening........");
    serviceHost.Open();
    Console.WriteLine("\r\nRouting Service is now running.....
    	and listening at following addresses");
    foreach (var endPoint in serviceHost.Description.Endpoints)
    {
        endpointCounter++;
        Console.WriteLine("\r\n" + endpointCounter + ".) 
        	" + endPoint.Address.ToString());
    }
    Console.WriteLine("\r\nPress <ENTER> to terminate router.");
    Console.ReadLine();
};

免责声明

本文档中的信息按“原样”提供,不含任何保证,也不授予任何权利。本文不代表我雇主的想法、意图、计划或策略。 纯属我个人意见。

本文档中提供的所有数据和信息仅供参考。我不对本网站上任何信息的准确性、完整性、时效性、适用性或有效性做出任何声明,并且不对因显示或使用这些信息而造成的任何错误、遗漏或延误或任何损失、伤害或损害承担责任。

© . All rights reserved.