我正在使用 LoadPackage(...) 调用 SSIS 包。
是否可以将此调用设为异步调用?
是的,使用异步委托,如下所示:
如果您只是希望它在后台运行,那么可以,您可以启动一个线程或调用一些 T-SQL 来动态创建一个作业(然后再次删除它)。如果您想异步运行它并在完成后想要回调,那么我认为您很不幸。
您是在问 1) 在后台线程上调用 LoadPackage 是否合法,还是 2) 是否有可能。对于#1,我无法给出明确的答案,因为我不使用 SSIS 框架。
但是#2(只要#1为真)绝对是可行的。恕我直言,您最好使用现有的框架,该框架的 API 旨在调用 API 的异步并等待结果。例如,使用 Parellel Extensions June 08 CTP,下面的代码就可以了。
using System.Threading.Tasks;
...
var future = Future.Create(()=>LoadPackage); // Starts loading the package
// Do other stuff
var package = future.Value; // Wait for package load to complete and get the value
我通过异步 WCF 服务调用从我的 UI (WPF) 调用 SSIS 包。服务代码为:
public string ImportMarriageXML(bool isWakeUp, bool clearExistingMarriage)
{
try
{
var dts = new Microsoft.SqlServer.Dts.Runtime.Application();
using (var package = dts.LoadFromSqlServer(
ServiceSettings.Settings.SSIS.ImportMarriages,
ServiceSettings.Settings.SSIS.ServerIP,
ServiceSettings.Settings.SSIS.UserID,
ServiceSettings.Settings.SSIS.Password,
null))
{
package.InteractiveMode = false;
package.Connections["DB.STAGING"].ConnectionString = String.Format("{0};Provider={1};", DBSettings.ConnectionString(Core.Database.Staging), ServiceSettings.Settings.SSIS.Provider);
var variables = package.Variables;
variables["IsWakeUp"].Value = isWakeUp;
variables["ClearExistingMarriage"].Value = clearExistingMarriage;
variables["XmlDirectory"].Value = ServiceSettings.Settings.SSIS.Properties.XmlDirectory;
if (package.Execute() == DTSExecResult.Failure)
{
// HACK: Need to refactor this at some point. Will do for now.
var errors = new System.Text.StringBuilder();
foreach (var error in package.Errors)
errors.AppendFormat("SubComponent: {0}; Description: {1}{2}", error.SubComponent, error.Description, Environment.NewLine);
throw new ApplicationException(errors.ToString());
}
return package.Connections["Text Logging"].ConnectionString;
}
}
}
并且(部分)客户端代码如下:
private void InvokeLoadMarriages()
{
integrationServicesServiceClient.BeginImportMarriageXML(false, OnEndImportMarriageXML, null);
}
private void OnEndImportMarriageXML(IAsyncResult asyncResult)
{
view.InvokeDisplayResults(integrationServicesServiceClient.EndImportMarriageXML(asyncResult));
}
其中 BeginImportMarriageXML 和 EndImportMarriageXML 是代理类中生成的异步操作。