从SSIS连接到Azure Service Bus

时间:2017-06-23 17:16:40

标签: sql-server azure ssis azureservicebus script-task

我需要从在SQL Server 2014下运行的SSIS包中将消息放入Azure ServiceBus队列。正如本文中所述:connecting to azure service bus queue from ssis, 我编写了一个脚本任务,引用了" Azure SDK 2.9"。这种方法适用于Azure存储帐户,可以使用blob(引用Microsoft.WindowsAzure.Storage程序集),但它不适用于Azure存储总线(引用Microsoft.ServiceBus程序集)。我在该程序集中进行的任何调用都会触发一个运行时异常:"调用的目标抛出了异常:在System.RuntimeMethodHandle.InvokeMethod(...)"当我注释掉所有对Microsoft.ServiceBus程序集的调用时,它运行正常,所以它显然是关于程序集引用(版本2.4)。我尝试使用NuGet(3.0版)更新到最新版本,但没有任何区别。

所以我的问题是:是否有人能够在SSIS的Azure Service Bus队列中放置一条消息,如果是,那你是怎么做到的?

由于有人会要求我的脚本任务代码,我发布它:

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
#endregion

#region CustomNamespaces
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
#endregion

namespace ST_dba6519c1eda4e0c968485a6eb7a6c29
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            try
            {
                // Create the message for the Queue
                string ClientShortName = Dts.Variables["$Package::ClientShortName"].Value.ToString();
                bool bExtendedForecast = (bool)Dts.Variables["$Package::ExtendedForecast"].Value;
                var msg = new BrokeredMessage(ClientShortName + ": ExtendedForecast=" + bExtendedForecast.ToString());  // this statement throws the exception

                // get Service Bus Connection Information from the Package Parameters
                string SBAccessKey = Dts.Variables["$Package::ServiceBusAccessKey"].Value.ToString();
                string SBNamespace = Dts.Variables["$Package::ServiceBusNamespace"].Value.ToString();
                string SBQueue = Dts.Variables["$Package::ServiceBusQueueName"].Value.ToString();
                String connStr = "Endpoint=sb://" + SBNamespace +
                        ".servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=" + SBAccessKey;

                // First Method tried
                Uri SBUri = ServiceBusEnvironment.CreateServiceUri(String.Empty, SBNamespace, String.Empty); // this statement throws the exception
                TokenProvider SBToken = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", SBAccessKey);
                NamespaceManager nsMgr = new NamespaceManager(SBUri, SBToken);
                MessagingFactory msgFactory = MessagingFactory.Create(nsMgr.Address, nsMgr.Settings.TokenProvider);
                QueueClient queueClient2 = msgFactory.CreateQueueClient(SBQueue);
                queueClient2.Send(msg);

                // Second Method tried
                MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connStr); // this statement throws the exception
                MessageSender queueSender = factory.CreateMessageSender(SBQueue);
                queueSender.Send(msg);

                // Third Method tried
                QueueClient queueClient = QueueClient.CreateFromConnectionString(connStr, SBQueue); // this statement throws the exception
                queueClient.Send(msg);

                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }
        }
        #region ScriptResults declaration
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
    }
}

1 个答案:

答案 0 :(得分:2)

所以,当我发布问题后10分钟,我找到了答案。我不得不运行GACUTIL -i Microsoft.ServiceBus。一旦完成,我选择在代码中使用第三种方法(最简单的)来发送消息,并且它工作正常。