共用方式為


建立自定義路線傳訊服務

Microsoft BizTalk ESB 工具組中的行程架構支援使用實作 IMessagingService 介面的類別來執行行程傳訊服務的步驟。 當您想要讓服務負責下列專案時,您可以實作自定義傳訊服務:

  • 在行程中設定的自定義訊息驗證

  • 在行程中設定的自定義訊息轉換

  • 訊息的自定義處理

    在這些情況下,您實作的自定義行程服務會做為攔截器,並由發送器管線元件呼叫。

    自定義傳訊型路線服務或傳訊服務全都會實作 IMessagingService 介面。 這個介面會公開 NameSupportsDisassemble 屬性和 ExecuteShouldAdvanceStep 方法。

    Name 屬性是服務的名稱,因為它會出現在行程中。 它必須符合 Esb.config 檔案中路線服務組態中設定的名稱。

    SupportsDisassemble 屬性會指出您要建立的自定義傳訊服務是否支援反組譯碼和執行多個解析程式。

    ShouldAdvanceStep 方法會採用目前的行程步驟和目前的訊息,並傳回 Boolean 值,指出發送器是否應在服務執行之後推進行程。 在幾乎所有情況下,這個方法都應該傳回 true

    Execute 方法對於傳訊服務而言非常重要,而且包含將在運行時間執行的邏輯。 它會採用管線內容、訊息、解析字串,以及當前流程步驟;並傳回更新的訊息。

    Execute 方法的參考實作可以在 ESB.Itinerary.Services 專案的 RoutingService.cs 檔案中找到,如下列程式代碼所示。

public IBaseMessage ExecuteRoute(IPipelineContext context, IBaseMessage msg, string resolverString)  
        {  
            if (context == null)  
                throw new ArgumentNullException("context");  
            if (msg == null)  
                throw new ArgumentNullException("msg");  
            if (string.IsNullOrEmpty(resolverString))  
                throw new ArgumentException(Properties.Resources.ArgumentStringRequired, "resolverString");  
            try  
            {  
                ResolverInfo info = ResolverMgr.GetResolverInfo(ResolutionType.Endpoint, resolverString);  
                if (!info.Success)  
                    throw new RoutingException(Properties.Resources.ResolverStringInvalid, resolverString);  
  
                // Resolve configuration for routing.  
                Dictionary<string, string> resolverDictionary = ResolverMgr.Resolve(info, msg, context);  
  
                if (string.IsNullOrEmpty(resolverDictionary["Resolver.TransportLocation"]))  
                    throw new RoutingException(Properties.Resources.TransportLocationNotResolved, resolverString);  
  
                AdapterMgr.SetEndpoint(resolverDictionary, msg.Context);  
  
                return msg;  
            }  
            catch (System.Exception ex)  
            {  
                EventLogger.Write(MethodInfo.GetCurrentMethod(), ex);  
                throw;  
            }        
        }  

實作傳訊的自定義路線服務

  1. 使用衍生自 IMessagingService 的 類別建立元件;在 Execute 方法中,包含修改訊息或訊息內容所需的所有邏輯(如果有的話)。

  2. 藉由在 Esb.config 檔案的 itineraryServices 區段中,新增一個 <itineraryService> 元素,該元素的id 屬性為 GUID,name 屬性為服務名稱,type 屬性為類別的完整名稱,scope 屬性為 Messaging,而階段 屬性為允許的階段(例如,OnRampReceiveOnRampSendOffRampSendOffRampReceiveAllSendAllReceiveAll)。

  3. 在全域程式集緩存中註冊新的元件。