从 SSIS 连接到 Azure 服务总线

Connecting to Azure Service Bus from SSIS

我需要将一条消息从 SQL Server 2014 下的 SSIS 包 运行 放入 Azure ServiceBus 队列。如本 post 中所建议:, 我编写了一个引用 "Azure SDK 2.9" 的脚本任务。这种方法适用于我使用 Azure 存储帐户来处理 blob(引用 Microsoft.WindowsAzure.Storage 程序集),但它不适用于 Azure 存储总线(引用 Microsoft.ServiceBus 程序集)。我对该程序集进行的任何调用都会触发 运行 时间异常:"exception has been thrown by the target of an invocation: at System.RuntimeMethodHandle.InvokeMethod(...)" 当我注释掉对 Microsoft.ServiceBus 程序集的所有调用时,它运行良好,所以这显然与程序集引用有关(2.4 版)。我尝试使用 NuGet(3.0 版)更新到最新版本,但没有任何区别。

所以我的问题是:是否有人能够从 SSIS 将消息放入 Azure 服务总线队列中?如果是,您是怎么做到的?

因为有人会询问我的脚本任务代码,所以我正在 posting 它:

#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
    }
}

当然,在我 post 提出问题 10 分钟后,我突然想到了答案。我不得不 运行 GACUTIL -i Microsoft.ServiceBus。完成后,我选择使用代码中的第三种方法(最简单的)发送消息,并且效果很好。