引發並在自訂工作中定義事件
Integration Services 執行階段引擎提供事件的集合,可提供工作在驗證和執行時的進度狀態。 IDTSComponentEvents 介面會定義這些事件,而且是以 Validate 和 Execute 方法的參數提供給工作。
另一組定義在 IDTSEvents 介面的事件,是由 TaskHost 代表工作而引發。 TaskHost 會引發在驗證和執行前後發生的事件,而工作會引發在執行和驗證期間發生的事件。
建立自訂事件
自訂工作開發人員可以透過在 EventInfo 方法的覆寫實作中建立新的 InitializeTask,以定義新自訂事件。 EventInfo建立 之後,它會使用 Add 方法新增至EventInfos
集合。 Add 方法的方法簽章如下所示:
public void Add(string eventName, string description, bool allowEventHandlers, string[] parameterNames, TypeCode[] parameterTypes, string[] parameterDescriptions);
下列程式代碼範例顯示 InitializeTask
自定義工作的 方法,其中會建立兩個自定義事件,並設定其屬性。 然後會將新事件加入 EventInfos 集合。
第一個自定義事件具有 「OnBeforeIncrement」 的 eventName,以及更新初始值之後引發的描述。值下一個參數true
表示這個事件應該允許建立事件處理程式容器來處理事件。 事件處理常式是提供封裝中結構與工作服務的容器,就像封裝、時序、ForLoop 和 ForEachLoop 等其他容器一樣。 當allowEventHandlers 參數為true
時,DtsEventHandler會為事件建立物件。 為事件定義的任何參數現在可供 DtsEventHandler 的變數集合中之 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
引發自訂事件
自訂事件是透過呼叫 FireCustomEvent 方法來引發。 下列程式碼行會引發自訂事件。
componentEvents.FireCustomEvent(this.onBeforeIncrement.Name,
this.onBeforeIncrement.Description, ref arguments,
null, ref bFireOnBeforeIncrement);
componentEvents.FireCustomEvent(Me.onBeforeIncrement.Name, _
Me.onBeforeIncrement.Description, arguments, _
Nothing, bFireOnBeforeIncrement)
範例
下列範例示範在 方法中InitializeTask
定義自定義事件的工作、將自定義事件新增至EventInfos集合,然後藉由呼叫 FireCustomEvent 方法在方法期間Execute
引發自定義事件。
[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
使用 Integration Services 保持最新狀態
如需來自Microsoft的最新下載、文章、範例和影片,以及來自社群的所選解決方案,請流覽 MSDN 上的 Integration Services 頁面:
流覽 MSDN 上的 Integration Services 頁面
如需這些更新的自動通知,請訂閱頁面上可用的 RSS 摘要。