遠程啟動SSIS包

來源:互聯網
上載者:User

在前面介紹的一篇文章中介紹了用代碼啟動SSIS包的方法,http://www.cnblogs.com/Farseer1215/archive/2010/11/08/1871464.html,其中第二種方法必須在安裝了SSIS組件的伺服器上運行.

如果這樣處理的實用性不是很強,不可能在每一台客戶機上都裝一個SSIS組件,所以最好的辦法是在安裝了SSIS組件的伺服器上安裝一個服務,其他用戶端調用即可,於是可以考慮用WCF寫一個小程式來運行SSIS包,其他的用戶端直接調用服務。
1.建立WCF服務
在VS2010中選擇建立WCF服務庫,這時VS會用WCF庫的模板建立一個項目,設定檔,介面和類實現都已經建立好,唯一要做的是事情是實現自己的邏輯。
要想啟動SSIS需要用到ManagedDTS這個類庫,添加對Microsoft.SqlServer.ManagedDTS這個DLL 的引用。

在介面定義中定義相應的操作和資料契約,在類中實現這些介面,定義了如下兩個方法:

public SSISPackageResult ExecSSISPackage(string _packageName)
        {
            SSISPackageResult result = new SSISPackageResult();
            try
            {
                Application application = new Application();
                Package package = application.LoadFromSqlServer(_packageName, ".", null, null,null);
                result.Execresult = package.Execute();

                if (result.Execresult == DTSExecResult.Failure || result.Execresult == DTSExecResult.Canceled)
                    result.ExecInfo = string.Format("SSIS包{0}運行失敗,請查看包作業記錄!",_packageName);
                else
                    result.ExecInfo = string.Format("SSIS包{0}運行成功",_packageName);
            }
            catch (Exception ex)
            {  
                result.Execresult = DTSExecResult.Failure;
                result.ExecInfo = string.Format("SSIS包{0}運行失敗,具體異常資訊為:"+ex.Message.ToString()); 
            }

            return result;
        }

 

public SSISPackageResult ExecSSISPackageWithParam(string _packageName, string _variableName, string _variableValue)
        {
            SSISPackageResult result = new SSISPackageResult();
            try
            {
                Application application = new Application();
                Package package = application.LoadFromSqlServer(_packageName, ".", null, null,null);
                package.Variables[_variableName].Value = _variableValue;
                result.Execresult = package.Execute();

                if (result.Execresult == DTSExecResult.Failure || result.Execresult == DTSExecResult.Canceled)
                    result.ExecInfo = string.Format("SSIS包{0}運行失敗,請查看包作業記錄!",_packageName);
                else
                    result.ExecInfo = string.Format("SSIS包{0}運行成功",_packageName);
            }
            catch (Exception ex)
            {
                
                result.Execresult = DTSExecResult.Failure;
                result.ExecInfo = string.Format("SSIS包{0}運行失敗,具體異常資訊為:"+ex.Message.ToString()); 
            }
            return result;
        }

 

2.建立承載WCF服務的Windows服務

WCF服務的宿主可以是IIS,Windows Services,Windows Form甚至控制台命令程式,考慮到並不是所有伺服器都會安裝IIS,SRS2008也不再依賴於IIS,Windows Form和控制台命令程式控制起來不方便,所以採用Windows Services做為WCF服務的宿主程式。
在解決方案中加入一個新的項目WS_SSIS,項目模板採用Windows服務,添加對項目WCF_SSIS和System.ServiceModel的引用,在OnStart方法中添加啟動WCF服務的代碼:

ServiceHost serviceHost = new ServiceHost(typeof(SSISService));
            serviceHost.Open();

因為System.Configuration不支援庫的設定檔為服務增加一個設定檔,把WCF服務的設定檔內容拷貝過來。

為了安裝服務,增加如下類,需要添加對程式集System.Configuration.Install的引用:

[RunInstaller(true)]
    public class SSISServicesInstaller:Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public SSISServicesInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;

            service = new ServiceInstaller();
            service.ServiceName = "SSIS Service";
            service.Description = "該服務用來執行SSIS包完成相應的資料整合任務";

            Installers.Add(process);
            Installers.Add(service);
        }
    }

這樣在用InstallUtil.exe安裝服務時會自動添加相應的服務,在Win7或者Windows2008上安裝服務時要注意使用管理員運行InstallUtil,否則會出錯。

運行後會在服務中添加SSISService:

安裝時讓SSIS服務木運行在本地服務帳戶下,可根據實際情況修改服務的運行帳戶,讓其可以正確訪問SSIS,然後啟動服務。
3.建立WCF用戶端調用WCF服務完成操作
可以用多種方法來實現在AX中調用WCF服務,可以先用C#寫個DLL,使用添加服務組建代理程式類,然後用代理類調用WCF服務,然後在AX中引用該DLL。AX2009也支援直接加入服務參考,自動組建代理程式類供調用WCF服務使用。
AOT->References->加入服務參考

填寫正確的資訊:
 
其中WSDL URL就是WCF向外提供服務的URL地址,具體的詳細資料參見WCF的相關知識。
.NET 代碼命名空間在AX產生的代理類的命名空間,這個可以按照自己的習慣隨便取名。
點擊確定:

這樣正確產生了調用WCF服務需要的用戶端程式。
4.調用服務
寫一個類調用WCF服務

static server void main(Args _args)
{
    InterOpPermission                   interop = new InterOpPermission(InteropKind::ClrInterop);
    SSISClient.ISSISServiceClient       ssisServiceClient;
    SSISClient.SSISPackageResult        packageResult;
    str bb;
    ;
    interop.assert();

    ssisServiceClient = new SSISClient.ISSISServiceClient();
    packageResult = ssisServiceClient.ExecSSISPackage("InventTable");
    
    info(packageResult.get_ExecInfo());

    CodeAccessPermission::revertAssert();

}

運行結果如下:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.