2011年7月28日 星期四

WCF教學(9) - 使用Routing與備援清單

這個練習將設計一個RsHost做Routing,聽在8081 port,收到ConClient請求後,將請求導向接聽8080 port的MyWCFService
  • File->New Website->WCF Service->MyWCFService
  • MyWCFService, 設port使用8080
image
  • File->Add->new Project->加入Console Application做為Routing Service Host,名為RsHost
  • RsHost,加入參考System.ServiceModel, System.ServiceModel.Routing.dll
  • RsHost,加入app.config檔,並加入以下內容
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="System.ServiceModel.Routing.RoutingService"
      behaviorConfiguration="routingData">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081/routing/"/>
          </baseAddresses>
        </host>
        <endpoint address="requestReply"
        binding="basicHttpBinding"
        contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
      </service>
    </services>
    <client>
      <endpoint name="epMyWCFService"
      address="http://localhost:8080/MyWCFService/Service.svc"
      binding="basicHttpBinding"
      contract="*"/>
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="routingData">
          <routing filterTableName="routingTable" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <routing>
      <filters>
        <filter name="MatchAllDefaultFilter" filterType="MatchAll" />
      </filters>
      <filterTables>
        <filterTable name="routingTable">
          <add filterName="MatchAllDefaultFilter"
          endpointName="epMyWCFService"/>
        </filterTable>
      </filterTables>
    </routing>
  </system.serviceModel>
</configuration>
  • RsHost,Main方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Routing;
namespace RsHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("RS is running...");
            ServiceHost myServiceHost = new ServiceHost(typeof(RoutingService));
            myServiceHost.Open();
            Console.Read();
        }
    }
}
  • 測試執行RsHost專案可以執行,不會出現錯誤
  • File->Add->new Project->加入Console Application做為Client,名為ConClient
  • ConClient,Add Service Reference->Discover->OK
  • ConClient,Main Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            ServiceReference1.ServiceClient c = new ServiceReference1.ServiceClient();
            Console.WriteLine(c.GetData(10));
        }
    }
}

<client>
            <endpoint address="http://localhost:8081/routing/requestReply"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
                contract="ServiceReference1.IService" name="BasicHttpBinding_IService" />
        </client>
  • 設定同時執行三個專案,請注意專案順序,愈上面愈先執行
image
  • Run,ConClient可以取得服務回傳的結果
image
若服務掛點,在WCF4中我們可以設定備援清單,將請求繞送到其它服務執行。例如以下步驟,修改服務接聽9999 port ,但實際上這個服務並不存在,ConClient透過Routing Service叫用9999埠的服務,利用備援清單,導向接聽8080的服務。
  • RsHost,修改RSHost app.config檔,<client>下,加入 endpoint,聽port 9999,<Clinet>區段看起來如
<client>
     <endpoint name="epMyWCFService"
     address="http://localhost:8080/MyWCFService/Service.svc"
     binding="basicHttpBinding"
     contract="*"/>
     <endpoint name="Dead"
     address="http://localhost:9999/MyWCFService/Service.svc"
     binding="basicHttpBinding"
     contract="*"/>
   </client>
  • RsHost,在app.config,<routing>下,加
   <backupLists>
        <backupList name="bklist">
          <add endpointName="epMyWCFService"/>
        </backupList>
      </backupLists> 
  • RsHost,在app.config,修改filter,endpointName="Dead"  backupList="bklist"
<filterTables>
       <filterTable name="routingTable">
         <add filterName="MatchAllDefaultFilter"      
              endpointName="Dead"  backupList="bklist"  />
       </filterTable>
     </filterTables>
  • Run,ConClient可以取得服務回傳的結果

沒有留言:

總網頁瀏覽量