Reputation: 85
I seem to be unable to use this library in project
Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'WindowsAzure' could not be found (are you missing a using directive or an assembly reference?) ClassLibrary2 \Visual Studio 2017\Projects\ClassLibrary2\ClassLibrary2\EntityListener.cs 24 Active
using WindowsAzure.Servicebus;
I installed using nuget packet manager, and it is defined in my packages.config file. Why can I not use it?
Packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="WindowsAzure.ServiceBus" version="4.1.10" targetFramework="net452" />
</packages>
Upvotes: 1
Views: 1588
Reputation: 2642
If your .NET project version is exactly 4.5 (not 4.5.x), you will need to fall back to WindowsAzure.ServiceBus package version 4.1.3. Moreover, this
Here is the packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="WindowsAzure.ServiceBus" version="4.1.3" targetFramework="net45" />
</packages>
In addition, the correct namespace to use is the following:
using Microsoft.ServiceBus.Messaging;
Find below a sample .NET 4.5 Console Application that sends a message to an Azure Service Bus Queue. Please note this is just a sample and it is not production ready code. I hope this helps.
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;
}
}
}
}
Upvotes: 0