Condividi tramite


Generazione e definizione di eventi in un'attività personalizzata

Il motore di runtime di Integration Services include una raccolta di eventi che forniscono lo stato di avanzamento durante la convalida e l'esecuzione di un'attività. L'interfaccia IDTSComponentEvents definisce questi eventi e viene fornita alle attività come parametro per i metodi Validate e Execute.

È disponibile un altro set di eventi, definiti nell'interfaccia IDTSEvents, che vengono generati per conto dell'attività da TaskHost. L'oggetto TaskHost genera gli eventi che si verificano prima e dopo la convalida e l'esecuzione, mentre l'attività genera gli eventi che si verificano durante l'esecuzione e la convalida.

Creazione di eventi personalizzati

Gli sviluppatori di attività personalizzate possono definire nuovi eventi personalizzati creando un nuovo oggetto EventInfo nell'implementazione sottoposta a override del metodo InitializeTask. L'oggetto EventInfo, una volta creato, viene aggiunto alla raccolta EventInfos tramite il metodo Add. La firma del metodo Add è la seguente:

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

Nell'esempio di codice seguente è illustrato il metodo InitializeTask di un'attività personalizzata, in cui vengono creati due eventi e vengono impostate le relative proprietà. I nuovi eventi vengono quindi aggiunti alla raccolta EventInfos.

Per il primo evento personalizzato, eventName è impostato su "OnBeforeIncrement" e description su "Fires after the initial value is updated". Il parametro successivo, il valore true, indica che questo evento deve consentire la creazione di un contenitore gestore eventi per gestire l'evento. Il gestore eventi è un contenitore che fornisce la struttura in un pacchetto e servizi alle attività, analogamente ad altri contenitori come Sequence, ForLoop e ForEachLoop. Quando il parametro allowEventHandlers è true, gli oggetti DtsEventHandler vengono creati per l'evento. I parametri definiti per l'evento sono ora disponibili per DtsEventHandler nella raccolta di variabili di 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

Generazione di eventi personalizzati

Gli eventi personalizzati vengono generati con una chiamata al metodo FireCustomEvent. La riga di codice seguente genera un evento personalizzato.

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

Esempio

Nell'esempio di codice seguente è illustrata un'attività che definisce un evento personalizzato nel metodoInitializeTask, aggiunge l'evento personalizzato alla raccolta EventInfos, quindi genera l'evento personalizzato durante il relativo metodo Execute chiamando il metodo FireCustomEvent.

[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
Icona di Integration Services (piccola) Rimanere aggiornati con Integration Services

Per informazioni sui download, gli articoli, gli esempi e i video Microsoft più recenti, nonché sulle soluzioni selezionate dalla community, visitare la pagina Integration Services su MSDN o Technet:

Per ricevere notifica automatica su questi aggiornamenti, sottoscrivere i feed RSS disponibili nella pagina.