Bagikan melalui


Menaikkan dan Menentukan Peristiwa dalam Tugas Kustom

Berlaku untuk: Integration Runtime SSIS SQL Server di Azure Data Factory

Mesin run-time Layanan Integrasi menyediakan kumpulan peristiwa yang memberikan status tentang kemajuan tugas saat tugas divalidasi dan dijalankan. Antarmuka IDTSComponentEvents mendefinisikan peristiwa ini, dan disediakan untuk tugas sebagai parameter untuk Validate metode dan Execute .

Ada serangkaian peristiwa lain, yang didefinisikan dalam IDTSEvents antarmuka, yang dinaikkan atas nama tugas oleh TaskHost. memunculkan TaskHost peristiwa yang terjadi sebelum dan sesudah validasi dan eksekusi, sedangkan tugas memunculkan peristiwa yang terjadi selama eksekusi dan validasi.

Membuat Kejadian Kustom

Pengembang tugas kustom dapat menentukan peristiwa kustom baru dengan membuat yang baru EventInfo dalam implementasi metode yang InitializeTask ditimpa. EventInfo Setelah dibuat, itu ditambahkan ke koleksi EventInfos dengan menggunakan Add metode . Tanda tangan Add metode dari metode ini adalah sebagai berikut:

public void Add(string eventName, string description, bool allowEventHandlers, string[] parameterNames, TypeCode[] parameterTypes, string[] parameterDescriptions);

Contoh kode berikut menunjukkan metode InitializeTask dari tugas kustom, di mana dua peristiwa kustom dibuat dan propertinya diatur. Peristiwa baru kemudian ditambahkan ke EventInfos koleksi.

Peristiwa kustom pertama memiliki eventName "OnBeforeIncrement" dan deskripsi "Fires setelah nilai awal diperbarui." Parameter berikutnya, nilai sebenarnya , menunjukkan bahwa peristiwa ini harus memungkinkan kontainer penanganan aktivitas dibuat untuk menangani peristiwa. Penanganan aktivitas adalah kontainer yang menyediakan struktur dalam paket dan layanan untuk tugas, seperti kontainer lain seperti paket, Urutan, ForLoop, dan ForEachLoop. Ketika parameter allowEventHandlersbenar, DtsEventHandler objek dibuat untuk peristiwa tersebut. Parameter apa pun yang didefinisikan untuk peristiwa sekarang tersedia untuk DtsEventHandler dalam kumpulan variabel dari DtsEventHandler.

public override void InitializeTask(Connections connections,  
   VariableDispenser variables, IDTSInfoEvents events,  
   IDTSLogging log, EventInfos eventInfos,  
   LogEntryInfos logEntryInfos, ObjectReferenceTracker refTracker)  
{  
    this.eventInfos = eventInfos;  
    string[] paramNames = new string[1];  
    TypeCode[] paramTypes = new TypeCode[1]{TypeCode.Int32};  
    string[] paramDescriptions = new string[1];  
  
    paramNames[0] = "InitialValue";  
    paramDescriptions[0] = "The value before it is incremented.";  
  
    this.eventInfos.Add("OnBeforeIncrement",   
      "Fires before the task increments the value.",  
      true,paramNames,paramTypes,paramDescriptions);  
    this.onBeforeIncrement = this.eventInfos["OnBeforeIncrement"];  
  
    paramDescriptions[0] = "The value after it has been incremented.";  
    this.eventInfos.Add("OnAfterIncrement",  
      "Fires after the initial value is updated.",  
      true,paramNames, paramTypes,paramDescriptions);  
    this.onAfterIncrement = this.eventInfos["OnAfterIncrement"];  
}  
Public Overrides Sub InitializeTask(ByVal connections As Connections, _  
ByVal variables As VariableDispenser, ByVal events As IDTSInfoEvents, _  
ByVal log As IDTSLogging, ByVal eventInfos As EventInfos, _  
ByVal logEntryInfos As LogEntryInfos, ByVal refTracker As ObjectReferenceTracker)   
  
    Dim paramNames(0) As String  
    Dim paramTypes(0) As TypeCode = {TypeCode.Int32}  
    Dim paramDescriptions(0) As String  
  
    Me.eventInfos = eventInfos  
  
    paramNames(0) = "InitialValue"  
    paramDescriptions(0) = "The value before it is incremented."  
  
    Me.eventInfos.Add("OnBeforeIncrement", _  
      "Fires before the task increments the value.", _  
      True, paramNames, paramTypes, paramDescriptions)  
    Me.onBeforeIncrement = Me.eventInfos("OnBeforeIncrement")  
  
    paramDescriptions(0) = "The value after it has been incremented."  
    Me.eventInfos.Add("OnAfterIncrement", _  
      "Fires after the initial value is updated.", True, _  
      paramNames, paramTypes, paramDescriptions)  
    Me.onAfterIncrement = Me.eventInfos("OnAfterIncrement")  
  
End Sub  

Menaikkan Peristiwa Kustom

Peristiwa kustom dimunculkan dengan memanggil FireCustomEvent metode . Baris kode berikut memunculkan peristiwa kustom.

componentEvents.FireCustomEvent(this.onBeforeIncrement.Name,  
   this.onBeforeIncrement.Description, ref arguments,  
   null, ref bFireOnBeforeIncrement);  
componentEvents.FireCustomEvent(Me.onBeforeIncrement.Name, _  
Me.onBeforeIncrement.Description, arguments, _  
Nothing,  bFireOnBeforeIncrement)  

Sampel

Contoh berikut menunjukkan tugas yang menentukan peristiwa kustom dalam metode InitializeTask , menambahkan peristiwa kustom ke EventInfos koleksi, lalu menaikkan peristiwa kustom selama metode Execute dengan memanggil FireCustomEvent metode .

[DtsTask(DisplayName = "CustomEventTask")]  
    public class CustomEventTask : Task  
    {  
        public override DTSExecResult Execute(Connections connections,   
          VariableDispenser variableDispenser, IDTSComponentEvents componentEvents,  
           IDTSLogging log, object transaction)  
        {  
            bool fireAgain;  
            object[] args = new object[1] { "The value of the parameter." };  
            componentEvents.FireCustomEvent( "MyCustomEvent",   
              "Firing the custom event.", ref args,  
              "CustomEventTask" , ref fireAgain );  
            return DTSExecResult.Success;  
        }  
  
        public override void InitializeTask(Connections connections,  
          VariableDispenser variableDispenser, IDTSInfoEvents events,  
          IDTSLogging log, EventInfos eventInfos,  
          LogEntryInfos logEntryInfos, ObjectReferenceTracker refTracker)  
        {  
            string[] names = new string[1] {"Parameter1"};  
            TypeCode[] types = new TypeCode[1] {TypeCode.String};  
            string[] descriptions = new string[1] {"Parameter description." };  
  
            eventInfos.Add("MyCustomEvent",  
             "Fires when my interesting event happens.",  
             true, names, types, descriptions);  
  
        }  
   }  
<DtsTask(DisplayName = "CustomEventTask")> _   
    Public Class CustomEventTask  
     Inherits Task  
        Public Overrides Function Execute(ByVal connections As Connections, _  
          ByVal variableDispenser As VariableDispenser, _  
          ByVal componentEvents As IDTSComponentEvents, _  
          ByVal log As IDTSLogging, ByVal transaction As Object) _  
          As DTSExecResult  
  
            Dim fireAgain As Boolean  
            Dim args() As Object =  New Object(1) {"The value of the parameter."}  
  
            componentEvents.FireCustomEvent("MyCustomEvent", _  
              "Firing the custom event.", args, _  
              "CustomEventTask" ,  fireAgain)  
            Return DTSExecResult.Success  
        End Function  
  
        Public Overrides  Sub InitializeTask(ByVal connections As Connections, _  
          ByVal variableDispenser As VariableDispenser,  
          ByVal events As IDTSInfoEvents,  
          ByVal log As IDTSLogging, ByVal eventInfos As EventInfos, ByVal logEnTryInfos As LogEnTryInfos, ByVal refTracker As ObjectReferenceTracker)  
  
            Dim names() As String =  New String(1) {"Parameter1"}  
            Dim types() As TypeCode =  New TypeCode(1) {TypeCode.String}  
            Dim descriptions() As String =  New String(1) {"Parameter description."}  
  
            eventInfos.Add("MyCustomEvent", _  
              "Fires when my interesting event happens.", _  
              True, names, types, descriptions)  
  
        End Sub  
  
    End Class  

Lihat juga

Penanganan Aktivitas Integration Services (SSIS)
Menambahkan Penanganan Aktivitas ke Paket