1

I have a website which as published on Azure (as App Service) which was developed using ASP.NET MVC, SQL, EF6 technology stack.

I have a class inside the project which constantly pings the service bus queue for new messages.

If there is a new message in service bus queue, it should fetch the message and log into the database.

Problem is that when I published into Azure, the class is not working properly and not reading the messages always.

Is there a better solution to do that?

8
  • Does that class work properly on local? Commented Feb 23, 2018 at 3:37
  • yup, its working fine on development PC. Commented Feb 23, 2018 at 3:39
  • what message are you getting from the server? Commented Feb 23, 2018 at 3:43
  • can you please elaborate your question @JosueMartinez Commented Feb 23, 2018 at 3:47
  • So when published, it sometimes reads the service bus messages, or it never reads them? Is it working intermittently? Does it give any error messages? Commented Feb 23, 2018 at 3:53

3 Answers 3

1

For your scenario, you could create a background task to listen messages from your service bus queue, then retrieve the message and save it to your database. For example, you could define _maunalResetEvent under Global.asax.cs file:

private static ManualResetEvent _maunalResetEvent = new ManualResetEvent(false);

And add the following code under the Application_Start event of the Global.asax.cs.

    ThreadPool.QueueUserWorkItem(_ =>
    {
        var connectionString = "";
        var queueName = "samplequeue";
        var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
        client.OnMessage(message =>
        {
            var messageBody = message.GetBody<String>();
            System.Diagnostics.Trace.TraceInformation(String.Format("Message body: {0}", messageBody));

           //TODO: save the message to the database
        });
        _maunalResetEvent.WaitOne();
    });

Moreover, you could use Azure Functions as Joe Wilson answered, and use Azure Service Bus bindings for Azure Functions for event-based message handling instead of creating a timer or scheduler to manually retrieve the queue message(s). Also, you could leverage WebJobs to implement your scenario, details you could follow How to use Azure Service Bus with the WebJobs SDK.

Additionally, for hosting on Azure App service, you app would go idle after a few minutes (20 minutes by default) of inactivity. So you need to enable the Always On option. Note: You could enable Always On to keep your app loaded all the time in Basic or higher app service plan. Details you could follow here.

Sign up to request clarification or add additional context in comments.

Comments

1

If your timer doesn't fire unless someone is hitting the web server, you need another way to trigger the timer and do the service bus checking.

You are already using Azure, so you should look into Azure Functions. It has a timer trigger option and can run your message checking every 5 minutes or whatever your application needs.

Comments

1

You should turn on the Always On of your Azure web app. https://learn.microsoft.com/en-us/azure/app-service/web-sites-configure

As others mentioned, a better way is to move the logic to Azure Functions with a timer trigger, or create WebJobs. If you go with WebJobs, you still need to turn on the Always On.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.