c# - 在 .NET 4.5 应用程序上使用 WindowsAzure.Servicebus 包版本 4.1.10 时出现生成错误

标签 c# azure nuget-package azure-servicebus-queues

我似乎无法在项目中使用这个库

严重性代码描述项目文件行抑制状态 错误 CS0246 找不到类型或命名空间名称“WindowsAzure”(是否缺少 using 指令或程序集引用?)ClassLibrary2\Visual Studio 2017\Projects\ClassLibrary2\ClassLibrary2\EntityListener.cs 24 Active

using WindowsAzure.Servicebus;

我使用 nuget 数据包管理器安装,它是在我的 packages.config 文件中定义的。为什么我不能使用它?

包.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="WindowsAzure.ServiceBus" version="4.1.10" targetFramework="net452" />
</packages>

最佳答案

如果您的 .NET 项目版本恰好是 4.5(不是 4.5.x),则您需要回退到 WindowsAzure.ServiceBus 包版本 4.1.3。而且,这个

这是packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="WindowsAzure.ServiceBus" version="4.1.3" targetFramework="net45" />
</packages>

此外,要使用的正确命名空间如下:

using Microsoft.ServiceBus.Messaging;

下面是一个向 Azure 服务总线队列发送消息的示例 .NET 4.5 控制台应用程序。请注意,这只是一个示例,并不是生产就绪的代码。我希望这会有所帮助。

using System;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusSample
{
    class Program
    {
        static void Main(string[] args)
        {
            const string ConnectionString = "your service bus connection string";
            const string QueueName = "your service bus queue name";
            string message = "Hello World!";
            string sessionId = Guid.NewGuid().ToString();

            SendMessage(ConnectionString, QueueName, sessionId, message).Wait();

            Console.WriteLine("Press <ENTER> to exit...");
            Console.ReadLine();
        }

        private static async Task SendMessage(string connectionString, string queueName, string sessionId, string message, string correlationId = null)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    throw new ArgumentException("Service bus connection string cannot be null, empty or whitespace.");
                }

                if (string.IsNullOrWhiteSpace(queueName))
                {
                    throw new ArgumentException("Service bus queue name cannot be null, empty or whitespace.");
                }

                if (string.IsNullOrWhiteSpace(sessionId))
                {
                    throw new ArgumentException("Session id cannot be null, empty or whitespace.");
                }

                QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);

                BrokeredMessage brokeredMessage = new BrokeredMessage(message);
                brokeredMessage.SessionId = sessionId;

                await queueClient.SendAsync(brokeredMessage);
            }
            catch
            {
                // TODO: Handle exception appropriately (including logging)
                throw;
            }
        }
    }
}

关于c# - 在 .NET 4.5 应用程序上使用 WindowsAzure.Servicebus 包版本 4.1.10 时出现生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50639424/

相关文章:

c# - 在标签文本中显示文件名

.net - 如何在 .Net Web 应用程序中实现 Ajax Control Toolkit 自定义编辑器?

c# - 如何在 nuspec 中指定特定的依赖版本?

c# - T 必须是具有公共(public)无参数构造函数的非抽象类型,以便将其用作泛型类型或方法中的参数 'TModel'

c# - async/await 是否可以感知 Android Activity 生命周期?

azure - Azure Devops 中拉取请求的构建策略是否可以使用在所述 PR 中更新的 yaml 文件?

asp.net - 将 ASP.Net 5 Web 应用程序部署到 Azure 时如何删除现有文件

.net - 如何打包 .NET Framework 库?

c# - 如何使用 UnitsNet nuget 包中的 Parse() 方法

c# - 监控使用统计——它是如何完成的?