1
votes

I have a web project (asp.net mvc) deployed as a web role in windows azure. For the web role i have a WebRole class inheriting from RoleEntryPoint. I have overridden the OnStart() Method to initialize some static classes.

In a RequestHandler (IHttpHandler) of the same project I use these static classes, but they are not initialized - i have to initialize them in the Global.asax again. I think they are in different application domains.

I have not tested this behavior in the real azure environment, only in the emulator.

Is there a way to fix this? I need the static class to share data between the WebRole class and the request handlers.

Thanks

1

1 Answers

1
votes

The WebRole.cs runs in a different process than your actual web application (explained here):

enter image description here

If you want your web application to use static classes you will need to use the Global.asax. If you don't want to duplicate code consider storing the static properties in a different class and have them initialized in both the WebRole.cs and Global.asax, like this:

public static class MyStaticThingie
{
    public static string XmlContentThingie { get; private set; }
    public static Container IoCContainer { get; private set; }

    public static void Init()
    {
        IoCContainer = ...;
        XmlContentThingie = File.ReadAllText("Somefile.xml");
    }
}

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        MyStaticThingie.Init();

        var something = MyStaticThingie.IoCContainer.GetSomething();
        something.DoSomething();

        return base.OnStart();
    }
}

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        MyStaticThingie.Init();
    }
}