Add WebRole.cs - and it invoked - on an existing ASP.NET MVC website converted to a web role

I have an ASP.NET MVC 4 site that works fine in Azure WebRole. The ASP.NET MVC project was launched on its own, after which I added the Azure Cloud Service project to the solution and added the ASP.NET project / site as one of the "roles" of the service (so that it appears in the Roles folder).

My problem is that I would like to work with the WebRole.cs file in an ASP.NET MVC project, but no matter what I tried to do, it seems that it just never gets called when deployed. OnStart and Run redefinition (which, as I know, should never leave the loop) - these, apparently, are never called.

But if you started a new CloudService project and added an ASP.NET MVC project at that time from the very beginning, it automatically has a WebRole.cs file, so I assume that I need to configure something for WebRole.cs (in fact, WebRole class, which inherits RoleEntryPoint) to call. What could it 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: Thank you, the question has been answered. But I will add: while doing this, although it clearly worked (fully deployed and in the emulator), suddenly I had problems with the full publication of the site. After the azure publication, it took 3 hours:

Checking the storage account "xyz" ...> Downloading the package ...> - Update ... [remained here for 3 hours], this error failed: the server detected an internal error. Repeat request. So I was wondering if I need to override OnStop in WebRole.cs?

UPDATE 2: These previous issues have been fixed and had nothing to do with this issue. In fact, I found out about this: if you get any warnings created in your assembly, Azure will often not work with them, even if they do not create problems locally or on other hosts. Since then, I have tried much harder to solve problems with assembly warnings (but it is critical that with the warning codes many warning types be turned off that you want to ignore!).

+4
source share
1 answer

Adding a class to your web project that inherits from RoleEntryPoint is enough, it should just work. Have you tried to set a breakpoint in the emulator?

What you may experience is that EmailFuncs.SendEmailToUs requires information from the /web.config application and this information is not available. You need to know that your WebRole class is running in a different process (and not in a web application), which means that it is not using your web.config. If you want WebRole.cs to read information from the configuration file, you need to add these parameters to WaIISHost.exe.config

+3
source

Source: https://habr.com/ru/post/1437726/


All Articles