開發自訂工作的使用者介面

適用於:SQL Server Azure Data Factory 中的 SSIS Integration Runtime

Integration Services 物件模型可讓自訂工作開發人員輕鬆地為工作建立自訂使用者介面,然後在 SQL Server Data Tools (SSDT) 中整合並顯示該介面。 使用者介面可在 SSIS Designer 中提供有用的資訊給使用者,然後指引使用者正確地設定自訂工作的屬性和設定。

為工作開發自訂使用者介面牽涉到兩個重要類別的使用。 下表將描述這些類別。

類別 描述
DtsTaskAttribute 識別受管理的工作,並透過它的屬性來提供設計階段資訊,以控制 SSIS Designer 如何顯示物件以及與物件互動的屬性。
IDtsTaskUI 此工作所使用,以便將此工作與其自訂使用者介面產生關聯的介面。

本節描述當您為自訂工作開發使用者介面時 DtsTaskAttribute 屬性和 IDtsTaskUI 介面的角色,並提供有關如何在 SSIS Designer 內建立、整合、部署及偵錯此工作的詳細資料。

SSIS Designer 針對此工作提供使用者介面的多個進入點:使用者可以在捷徑功能表上選取 [編輯]、按兩下此工作,或是按一下屬性工作表表底部的 [顯示編輯器] 連結。 當使用者存取其中一個進入點時,SSIS Designer 就會尋找及載入包含此工作之使用者介面的組件。 此工作的使用者介面會負責建立要在 SQL Server Data Tools (SSDT) 中顯示給使用者的屬性對話方塊。

工作和它的使用者介面是不同的實體。 它們應該在不同的組件中實作,以減少當地語系化、部署和維護工作。 工作 DLL 不會載入、呼叫,或是通常會包含有關其使用者介面的任何知識,除了在工作中編碼之 DtsTaskAttribute 屬性值內包含的資訊以外。 這是工作與其使用者介面產生關聯的唯一方式。

DtsTask 屬性

DtsTaskAttribute 屬性會包含在工作類別程式碼中,好讓工作與其使用者介面產生關聯。 SSIS Designer 會使用此屬性 (Attribute) 的屬性 (Property) 來決定如何在設計師中顯示此工作。 這些屬性包含顯示名稱和圖示 (如果有的話)。

下表描述 DtsTaskAttribute 屬性 (Attribute) 的屬性 (Property)。

屬性 描述
DisplayName 在控制流程工具箱內顯示工作名稱。
Description 工作描述 (繼承自 DtsLocalizableAttribute)。 這個屬性會顯示在工具提示中。
IconResource 顯示在 SSIS Designer 中的圖示。
RequiredProductLevel 如果有使用的話,請將它設定為 DTSProductLevel 列舉內的其中一個值。 例如: RequiredProductLevel = DTSProductLevel.None
TaskContact 保留連絡人資訊給此工作需要技術支援的情況使用。
TaskType 指派類型給此工作。
Attribute.TypeId 在衍生類別中實作時,取得這個屬性的唯一識別碼。 如需詳細資訊,請參閱 .NET Framework 類別庫中的 Attribute.TypeID 屬性。
UITypeName 此組件的類型名稱,由 SSIS Designer 用來載入此組件。 這個屬性是用來尋找此工作的使用者介面組件。

下列程式碼範例會顯示 DtsTaskAttribute 的面貌 (在類別定義上方編碼)。

using System;  
using Microsoft.SqlServer.Dts.Runtime;  
namespace Microsoft.SSIS.Samples  
{  
  [DtsTask  
  (  
   DisplayName = "MyTask",  
   IconResource = "MyTask.MyTaskIcon.ico",  
   UITypeName = "My Custom Task," +  
   "Version=1.0.0.0," +  
   "Culture = Neutral," +  
   "PublicKeyToken = 12345abc6789de01",  
   TaskType = "PackageMaintenance",  
   TaskContact = "MyTask; company name; any other information",  
   RequiredProductLevel = DTSProductLevel.None  
   )]  
  public class MyTask : Task  
  {  
    // Your code here.  
  }  
}  
Imports System  
Imports Microsoft.SqlServer.Dts.Runtime  
  
<DtsTask(DisplayName:="MyTask", _  
 IconResource:="MyTask.MyTaskIcon.ico", _  
 UITypeName:="My Custom Task," & _  
 "Version=1.0.0.0,Culture=Neutral," & _  
 "PublicKeyToken=12345abc6789de01", _  
 TaskType:="PackageMaintenance", _  
 TaskContact:="MyTask; company name; any other information", _  
 RequiredProductLevel:=DTSProductLevel.None)> _  
Public Class MyTask  
  Inherits Task  
  
  ' Your code here.  
  
End Class 'MyTask  

SSIS Designer 會使用此屬性 (Attribute) 的 UITypeName 屬性 (Property),該屬性 (Attribute) 包含組件名稱、類型名稱、版本、文化特性和公開金鑰權杖,以便在全域組件快取 (GAC) 中尋找組件,並載入它供設計師使用。

找到組件之後,SSIS Designer 會使用此屬性 (Attribute) 中的其他屬性 (Property),在 SSIS 設計師中顯示有關此工作的其他資訊,例如此工作的名稱、圖示和描述。

DisplayNameDescriptionIconResource 屬性會指定此工作要如何呈現給使用者。 IconResource 屬性包含內嵌在使用者介面組件內之圖示的資源識別碼。 此設計師會從組件中依照識別碼載入圖示資源,並在此工作加入到封裝時,將它顯示在工具箱和設計介面上的工作名稱旁邊。 如果工作不提供圖示資源,設計師會針對此工作使用預設圖示。

IDTSTaskUI 介面

IDtsTaskUI 介面會定義 SSIS Designer 所呼叫之方法和屬性的集合,以初始化及顯示與此工作有關的使用者介面。 當叫用工作的使用者介面時,設計師會呼叫 Initialize 方法 (當您撰寫此方法時,它會由工作使用者介面所實作),然後分別提供此工作和封裝的 TaskHostConnections 集合當做參數。 這些集合會儲存在本機,而且之後會在 GetView 方法中使用。

設計師會呼叫 GetView 方法,要求顯示於 SSIS Designer 中的視窗。 此工作會建立包含此工作之使用者介面的視窗執行個體,並將此使用者介面傳回給設計師以供顯示。 一般來說,TaskHostConnections 物件會透過多載建構函式提供給視窗,好讓它們可用於設定工作。

SSIS Designer 會呼叫工作使用者介面的 GetView 方法,以顯示此工作的使用者介面。 此工作使用者介面會從這個方法傳回 Windows 表單,而 SSIS Designer 會將這個表單顯示為強制回應對話方塊。 當關閉此表單時,SSIS Designer 會檢查此表單的 DialogResult 屬性值,以判斷此工作是否已經修改過,以及是否應該儲存這些修改。 如果 DialogResult 屬性值為 OK,SSIS 設計師會呼叫此工作的保存方法來儲存變更,否則會捨棄這些變更。

下列程式碼範例會實作 IDtsTaskUI 介面,並假設有一個名為 SampleTaskForm 的 Windows Form 類別存在。

using System;  
using System.Windows.Forms;  
using Microsoft.SqlServer.Dts.Runtime;  
using Microsoft.SqlServer.Dts.Runtime.Design;  
  
namespace Sample  
{  
   public class HelloWorldTaskUI : IDtsTaskUI  
   {  
      TaskHost   taskHost;  
      Connections connections;  
      public void Initialize(TaskHost taskHost, IServiceProvider serviceProvider)  
      {  
         this.taskHost = taskHost;  
         IDtsConnectionService cs = serviceProvider.GetService  
         ( typeof( IDtsConnectionService ) ) as   IDtsConnectionService;   
         this.connections = cs.GetConnections();  
      }  
      public ContainerControl GetView()  
      {  
        return new HelloWorldTaskForm(this.taskHost, this.connections);  
      }  
     public void Delete(IWin32Window parentWindow)  
     {  
     }  
     public void New(IWin32Window parentWindow)  
     {  
     }  
   }  
}  
Imports System  
Imports Microsoft.SqlServer.Dts.Runtime  
Imports Microsoft.SqlServer.Dts.Runtime.Design  
Imports System.Windows.Forms  
  
Public Class HelloWorldTaskUI  
  Implements IDtsTaskUI  
  
  Dim taskHost As TaskHost  
  Dim connections As Connections  
  
  Public Sub Initialize(ByVal taskHost As TaskHost, ByVal serviceProvider As IServiceProvider) _  
    Implements IDtsTaskUI.Initialize  
  
    Dim cs As IDtsConnectionService  
  
    Me.taskHost = taskHost  
    cs = DirectCast(serviceProvider.GetService(GetType(IDtsConnectionService)), IDtsConnectionService)  
    Me.connections = cs.GetConnections()  
  
  End Sub  
  
  Public Function GetView() As ContainerControl _  
    Implements IDtsTaskUI.GetView  
  
    Return New HelloWorldTaskForm(Me.taskHost, Me.connections)  
  
  End Function  
  
  Public Sub Delete(ByVal parentWindow As IWin32Window) _  
    Implements IDtsTaskUI.Delete  
  
  End Sub  
  
  Public Sub [New](ByVal parentWindow As IWin32Window) _  
    Implements IDtsTaskUI.[New]  
  
  End Sub  
  
End Class  

另請參閱

建立自訂工作
撰寫自訂工作的程式碼
開發自訂工作的使用者介面