4

I have an ASP.NET MVC 4 site running perfectly well in an Azure WebRole. The ASP.NET MVC project was started on its own, after which I added an Azure Cloud Service project to the solution and added the ASP.NET project/site as one of the 'roles' of the service (so it shows up in the 'Roles' folder).

My problem is that I would like to have working a WebRole.cs file within the ASP.NET MVC project, but no matter what I've tried to do, it appears that when deployed, it just never gets called. OnStart and the override of Run (which I know, must never leave the loop) -- these just apparently never get called.

But if you startup a new CloudService project and add, at that time from the start, an ASP.NET MVC project, it automatically has a WebRole.cs file in it, so my guess is that I need to configure something somewhere for the WebRole.cs (actually speaking, the WebRole class, which inherits RoleEntryPoint) to get called. What might that be?

using System;
using System.Web;
//using Microsoft.WindowsAzure.StorageClient;
using System.Diagnostics;
using System.Threading;

namespace Us.WebUI
{
    public class WebRole : Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint
    {
        public override bool OnStart()
        {
            return true; //return base.OnStart(); // CALL THIS???
        }

        public override void Run()
        {
            while (true) {
                Thread.Sleep(TimeSpan.FromSeconds(30));
                try {
                    EmailFuncs.SendEmailToUs("An email from our WebRole?????", "Email me this, email me that.");
                }
                catch { }
            }
        }

    }
}

UPDATE: Thanks, the question has been answered. But I will add: On doing this, while it clearly was working (fully deployed and in emulator), that suddenly I was having problems doing a full publish of the site. After a azure publish took 3 hours:

Verifying storage account 'xyz'... > Uploading Package... > - Updating... [stayed here for 3 hours], it failed with this error: The server encountered an internal error. Please retry the request. So one thing I was wondering is, did I need to override OnStop in WebRole.cs?

UPDATE 2: Those previous problems were fixed, and had nothing to do with this issue. Actually, I've learned this: If you ever have any warnings generated in your build, Azure often will not work with them even when they don't cause problems locally or in other hosts. Since then, I've been much more studious to tackling build warnings (but critical to this is turning off with warning codes the many warning types you want to ignore!).

Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69

1 Answers1

3

Adding a class to your Web Project which inherits from RoleEntryPoint is sufficient, it should just work. Did you try setting a breakpoint in the emulator?

What you might be experiencing is that EmailFuncs.SendEmailToUs requires info from the app/web.config and that this info is not available. You need to know that your WebRole class runs in a different process (not your web application), meaning it's not using your web.config. If you want the WebRole.cs to read info from the configuration file, you'll need to add these settings in WaIISHost.exe.config

Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • Thank you Sandrino, and I am very glad to hear that, ideally at least, just adding the class should work on its own (bravo for the team to make it that way). However, the email function I have there has no dependencies on web.config, etc. So, does anyone have another creative idea how I can test if the Run or OnStart are getting called? I updated the WebRole.cs code above to include the full code, just to make sure I am not missing something. – Nicholas Petersen Oct 04 '12 at 17:25
  • "Did you try setting a breakpoint in the emulator?" - I have never used the emulator, in fact am not sure how that would all work. I've avoided doing things like staging, because it seems like it would be a ton of work + far from reflective of real storage, etc. Anyways, I would try it, but setting breakpoints and debugging normally doesn't seem to use the emulator. – Nicholas Petersen Oct 04 '12 at 17:40
  • While it is true that the Compute Emulator is not a perfect representation of what happens when you run your app on Azure, it can be a good way of tracking down problems like this. Using the compute emulator is really easy, just set the cloud project to be your startup project and hit start debugging. As long as the installer has done a good job, it should all just work. You should still point to cloud storage and your regular database which will make things slower (don't get me started on the Storage Emulator) but much quicker than deploying to Azure every time. – knightpfhor Oct 04 '12 at 20:20
  • Well I'm not sure what I was missing before, but it is definitely working! Thanks guys. knightpfhor: thanks for those tips! I didn't know it was that easy to get the emulator working. On doing that, I was really surprised to see breakpoints actually working. – Nicholas Petersen Oct 05 '12 at 15:05