1

I Have an Azure Cloud Function that pushes messages to a queue under a Azure Service Bus namespace. The receiver for this queue is a .NET Framework 4.6.1 application which has the Service Bus SDK[Azure.Messaging.ServiceBus] installed. Problem is that the code sample references online are mostly for console applications which has Program.cs. My application has a Global.asax file, i believe, as the startup file.

So I referred the code from here. Placed it in Global.asax but not able to receive messages.

What has been tried is, I've created a console application on the same VM as the one which has the application mentioned above. Installed the SDK and pasted the listener code. It was able to receive messages from Service bus, which strikes off any possibilities of network misconfiguration. So most likely its the setup at .net 4.6.1 application level

Can anyone help with a sample service bus receiver/listener code?

Karan Rao
  • 295
  • 2
  • 4
  • 7

1 Answers1

0
  1. Create a Service Bus in Azure.

enter image description here

  1. And create a Queue.

enter image description here

  1. And pushed some messages to the queue as per the link given.

enter image description here

enter image description here

For Queue_AccessKey string, copy the string from azure as shown below.

enter image description here

Use the Console application and use the below code in .Net 4.6.1 framework.

For this the NuGet "Microsoft.Azure.ServiceBus" version="5.2.0" targetFramework="net461” has to be installed in the project.

And the below namespace has to be used in the code. using Microsoft.Azure.ServiceBus;

connectionString = new ServiceBusConnectionStringBuilder(Queue_AccessKey);
qClient = new QueueClient(connectionString, ReceiveMode.ReceiveAndDelete, RetryPolicy.Default);    
var msgHandler = new MessageHandlerOptions(ListenerExceptionHandler)
      {
         MaxConcurrentCalls = 1,
         AutoComplete = false
      };    
qClient.RegisterMessageHandler(ReceiveMessageFromQueue, msgHandler);
Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • Thanks. My solution already contains multiple 4.6.1 mvc applications. Hence I was trying to find if anyone has placed the listener code in Global.asax file of startup project. While I can try adding a console app to the solution, can you suggest if thats the best way and how will it run on production, maybe without opening up the console? – Karan Rao Dec 23 '22 at 07:34
  • Global.asax file is for Web applications to trigger the Server events like "Application_Start, Application_End, Application_Error". And for Conole app we need to use Program.cs file as it is the startup class. – Rajesh Mopati Dec 23 '22 at 08:06
  • That's correct. My concern is can it be used in prod environment too as a background app? – Karan Rao Dec 23 '22 at 08:16
  • We can do it by using Web jobs, please check this [link](https://learn.microsoft.com/en-us/azure/app-service/webjobs-create) – Rajesh Mopati Dec 23 '22 at 08:52
  • Please be aware that `Microsoft.Azure.ServiceBus` is deprecated and is not recommended for new development. The current Azure SDK package for Service Bus is `Azure.Messaging.ServiceBus`, which is compatible with (and tested against) net461. – Jesse Squire Dec 23 '22 at 15:20