简体   繁体   中英

timer issue in published asp.net mvc website

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?

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.

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 .

You should turn on the Always On of your Azure web app. https://docs.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM