1
protected void Application_Start()
{
                        AreaRegistration.RegisterAllAreas();
                        GlobalConfiguration.Configure(WebApiConfig.Register);
                        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                        RouteConfig.RegisterRoutes(RouteTable.Routes);
                        BundleConfig.RegisterBundles(BundleTable.Bundles);

                        string logsDir = this.GetLoggingPath();
}

private string GetLoggingPath()
{
                        var agentDataDirPath = Path.GetTempPath();

                        Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Role Environment is available : {0}", RoleEnvironment.IsAvailable));

                        // If running in Azure use default local storage  
                        if (RoleEnvironment.IsAvailable)
                        {
                            try
                            {
                                Trace.WriteLine("Getting the agentDataDir location");
                                agentDataDirPath = RoleEnvironment.GetLocalResource(agentDataDirStorage).RootPath;
                            }
                            catch (RoleEnvironmentException exp)
                            {
                                throw new InvalidOperationException(exp);
                            }
                        }

                        return agentDataDirPath;
}

Even when my cloud service is running on azure RoleEnvironment.IsAvailable is false. My service is running on IIS 8.5 and it is running under Network Service.

Any idea what am i doing wrong here. Also when i remote in and change the web.config a bit by adding space then IIS RoleEnvironment.IsAvailable is evaluated to true.

Also i am using Azure version 2.5

StackOverflowVeryHelpful
  • 2,347
  • 8
  • 34
  • 46

2 Answers2

0

It is a known bug that causes RoleEnvironment.IsAvailable to always return false on the emulator, see https://connect.microsoft.com/VisualStudio/feedback/details/695609/roleenvironment-isavailable-is-useless-returns-true-when-invoked-by-code-not-hosted-in-azure-emulator for more information.

You could do something like the following until the bug is fixed:

if ( RoleEnvironment.IsEmulator || RoleEnvironment.IsAvailable)
{
// Role is available
}
  • Small edit : RoleEnvironment.IsEmulated. Will try the solution mentioned by you and update the thread – StackOverflowVeryHelpful Sep 13 '15 at 18:24
  • Getting this error: [InvalidOperationException: the role environment has not been initialized] Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_IsEmulated() +150 – StackOverflowVeryHelpful Sep 13 '15 at 18:48
  • Also the webrole is already running on Azure and not on my local machine. Steps that i did was deployed my cspkg and csfg using manage.windowsazure.com. I invoke the url from browser. Remote into the machine and check the trace logs and see RoleEnvironment is false. Edit Web.config a bit to make sure Application_Start is called again and then RoleEnvironment is set to true. – StackOverflowVeryHelpful Sep 13 '15 at 18:55
  • `RoleEnvironment` may not be available in `Application_Start()` as it is unrelated to Azure, could you perhaps use the WebRole.cs `Onstart` event? See this discussion for more info http://stackoverflow.com/questions/16680007/whats-the-difference-between-the-webrole-onstart-event-and-application-start –  Sep 13 '15 at 19:34
0

shouldn't it be

if ( RoleEnvironment.IsAvailable && RoleEnvironment.IsEmulated)
{
     // we are running the workerrole locally in debug 
}
hannes neukermans
  • 12,017
  • 7
  • 37
  • 56