2

Am trying to listen to an azure service bus queue but am getting an exception when initializing the connection to to the queue. I have tried to search for a way out but with less success. Note that am using .NETCore 2.1. This is how am initializing the connection it:

// Initialize the connection to Service Bus Queue
_queueClient = QueueClient.CreateFromConnectionString(_interswitchQueueConnectionString, _interswitchQueueName, ReceiveMode.ReceiveAndDelete);

And this the exception am getting:

System.TypeInitializationException: The type initializer for 'Microsoft.ServiceBus.Messaging.Constants' threw an exception. ---> System.TypeLoadException: Could not load type 'System.UriTemplate' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. at Microsoft.ServiceBus.Messaging.Constants..cctor() --- End of inner exception stack trace --- at Microsoft.ServiceBus.ServiceBusConnectionStringBuilder..ctor() at Microsoft.ServiceBus.Messaging.QueueClient.CreateFromConnectionString(String connectionString, String path, ReceiveMode mode)

The same initialization works fine when using .net framework. How can I do it in .NetCore ? Please Help Thanks in advance

You Nguyen
  • 9,961
  • 4
  • 26
  • 52

3 Answers3

5

With .NET Core you should use the new .NET Standard Azure Service Bus client, Microsoft.Azure.ServiceBus. The old client WindowsAzure.ServiceBus is a legacy client, only works with Full Framework, and is not recommended moving forward.

The new client does not have a concept of factory. You're in charge of creating entity clients (QueueClient, SubscriptionClient, and TopicClient) and managing connections.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • How can you make `CloudConfigurationManager.GetSetting` work when using `Microsoft.Azure.ServiceBus` / .NET Core? I can't seem to find a straight answer anywhere. – Panzercrisis Jul 03 '20 at 04:04
  • `CloudConfigurationManager` is a legacy configuration API. You should look into alternatives. Also, not entirely sure how this is related to the answer – Sean Feldman Jul 03 '20 at 17:10
  • Oh, yeah, I guess I was tired earlier and kind of threw the two in the same category. – Panzercrisis Jul 04 '20 at 13:32
2

On Dotnet Core 2.1 you can try this.

var queueClient = new QueueClient(connectionString, entityPath, ReceiveMode.ReceiveAndDelete);

Hope this helps.

0

You can also do using MessagingFactory

MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);

            queueClient = await factory.CreateMessageReceiverAsync(_entityPath, ReceiveMode.ReceiveAndDelete);
Yuvaranjani
  • 300
  • 2
  • 12
  • 1
    This requires WindowsAzure.ServiceBus, and as @Sean Feldman said, it only works with Full Framework. I get a FileNotFoundException when trying to use MessagingFactory in Dotnet Core 2.1 – Met-u Feb 26 '20 at 06:03