Not running in a hosted service or the Development Fabric + role discovery data is unavailable => Local development

Unanswered Not running in a hosted service or the Development Fabric + role discovery data is unavailable => Local development

  • Monday, November 21, 2011 10:39 PM
     
      Has Code

    Hi,

    I have the following setup: (I am talking about the local testing)

    I have 2 worker roles and both have the following config:

     

    <Contents>
          <Content destination=".\">
            <SourceDirectory path="C:\WindowsService\" />
          </Content>
        </Contents>
        <Startup>
          <Task commandLine="startup.cmd" executionContext="elevated" taskType="simple"></Task>
        </Startup>
    <Runtime executionContext="elevated"></Runtime> 

     

    The content copied from C:\WindowsService\ are the executable and dependencies for a windows service, including a startup.cmd script that installs and start the service. This works fine.

    In my windows Service I have following config:

     

    <configuration>
      <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" />
        <requiredRuntime version="v4.0.20506" />
      </startup>
    </configuration>

     

    After the windows service is started, at some point I execute following code:
    (This line of code is not executed within the worked role itself, it is executed in the windows service that gets installed by startup script 

     

    System.Diagnostics.Trace.Listeners.Add(new Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener());
    System.Diagnostics.Trace.AutoFlush = true;

    Which is giving the following exception:

    Not running in a hosted service or the Development Fabric. -    
    at Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitor.GetDefaultStartupInfoForCurrentRoleInstance() 
    at Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener..ctor()
    Following I also get exceptions at
    role discovery data is unavailable -    at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment
    RoleEnvironment is no longer available ...
    Found related topics, but it doesn't solve the issue:
    Anyone got any idea what might be causing this problem ?
    IMPORTANT: I do not have this issue with a real azure deployment, this is only with the local emulator.

    Thanks in advance,
    Robbin 

     



All Replies

  • Tuesday, November 22, 2011 11:01 AM
    Moderator
     
     

    Hi,

    The diagnostics listener is not available unless you’re in a web/worker role process. If you want to write logs in your custom application, you can log them to Windows event log. You can configure Azure diagnostics monitor to transfer Windows event log to table storage.

     

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
  • Tuesday, November 22, 2011 12:30 PM
     
      Has Code

    Thanks a lot for the answer. However I am still not able of executing my code locally without any errors.

    If I get it correctly, the issue is due to the windows service not running in the same process as the worker role on my local machine, therefor making the diagnostic listener not available ?

    If I disable the code for the diagnostic listener, I do not longer get the following error:

     

    Not running in a hosted service or the Development Fabric. -    
    at Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitor.GetDefaultStartupInfoForCurrentRoleInstance() 
    at Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener..ctor()


    However I still get the following errors when I try to acces RoleEnvironment. from the Windows Service:
    Role discovery data is unavailable - at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment....

    Is there a way how I can solve this, or any reason why the role discovery data is unavailable ?

     

    Some of error stacktraces where I get exceptions:

     at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_Roles()

     at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_CurrentRoleInstance()


  • Tuesday, November 22, 2011 1:45 PM
     
      Has Code

    Actually, after some further testing with a dummy project, I found the following issue:

    I have 1 WorkerRole with the following csdef:

     <WorkerRole name="WorkerRole1" vmsize="ExtraSmall">
        <Endpoints>
          <InternalEndpoint name="InternalEndpoint_DiscoveryService" protocol="tcp" />
        </Endpoints>
        <Imports>
          <Import moduleName="Diagnostics" />
        </Imports>
        <Contents>
          <Content destination=".\">
            <SourceDirectory path="C:\Development\CloudTest\DiscoveryService\bin\Debug\" />
          </Content>
        </Contents>
        <Startup>
          <Task commandLine="startup.cmd" executionContext="elevated" taskType="simple" />
        </Startup>
        <Runtime executionContext="elevated"></Runtime>
      </WorkerRole>

    The content copies the executables of a windows service to the approot folder on my azure instance package. Within these files a startup.cmd file is always included, which is being triggered at startup of the azure instance, which installs the windows service and runs it.

    My windows service exists of the following code:

            public Service1()
            {
                InitializeComponent();
            }
     
            protected override void OnStart(string[] args)
            {
                Persist_Trace_Logging();
                Write_Endpoints_ToLog();
            }
     
            public static void Write_Endpoints_ToLog()
            {
                string strLog = "WorkerRole Endpoint settings: \n";
                foreach (KeyValuePair<stringRole> keyvRole in RoleEnvironment.Roles)
                {
                    strLog += "For role " + keyvRole.Key + ": \n";
                    foreach (RoleInstance _RoleInstance in keyvRole.Value.Instances)
                    {
                        strLog += "Endpoints " + _RoleInstance.Role.Name + " " + _RoleInstance.Id + ": \n";
                        foreach (KeyValuePair<stringRoleInstanceEndpoint> keyv in _RoleInstance.InstanceEndpoints)
                        {
                            strLog += keyv.Key + ": " + keyv.Value.IPEndpoint.ToString() + "\n";
                        }
                    }
                    strLog += "\n\n";
                }
                EventLog.WriteEntry("Application", strLog, EventLogEntryType.Information);
            }
     
            protected override void OnStop() { }
     
            public static void Persist_Trace_Logging()
            {
                    System.Diagnostics.Trace.Listeners.Add(new Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener());
                    System.Diagnostics.Trace.AutoFlush = true;
     
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
                    DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration();
                    config.Logs.ScheduledTransferPeriod = TimeSpan.FromSeconds(10.0);
                    config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
     
                    config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromSeconds(10.0);
                    config.WindowsEventLog.DataSources.Add("Application!*");
                    config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
     
                    RoleInstanceDiagnosticManager roleInstanceDiagnosticManager = storageAccount.CreateRoleInstanceDiagnosticManager(RoleEnvironment.DeploymentId, RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);
                    roleInstanceDiagnosticManager.SetCurrentConfiguration(config);
                    DiagnosticMonitor.Start(storageAccount, config);
            }

     

    With 1 workerrole, I test this. This works fine.
    At my eventlog an entry is written with with the endpoints of this role instance.

     

    Then I added a new (2nd) workerole

      <WorkerRole name="WorkerRole2" vmsize="ExtraSmall">
        <Runtime executionContext="elevated"></Runtime>
      </WorkerRole>

    So a very simple second workerrole, which actually does nothing.


    Result:

    As soon as I run the project, the startup of the windows service fails with the following error:

     

     

    Service cannot be started. System.InvalidOperationException: Not running in a hosted service or the Development Fabric.
    
       at Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitor.GetDefaultStartupInfoForCurrentRoleInstance()
    
       at Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener..ctor()
    
       at DiscoveryService.Service1.Persist_Trace_Logging() 

     

    So in the startup of my service, I remove the Persist_Logging() method. Clean & Rebuild necessary stuff and I run the cloudproject again.

    Following error:

     

    Service cannot be started. System.InvalidOperationException: role discovery data is unavailable
    
       at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_Roles()
    
       at DiscoveryService.Service1.Write_Endpoints_ToLog()

     

    I tried to play a bit with settings of the roles, removing diagnostics everywhere ... anything ... but I keep getting this above error when working on the RoleEnvironment. Seriously ...

     

    Now I remove workerrole 2 again ... And run the cloud project again.
    The windows service starts up correctly again and writes following output to my eventlog:

     

     

    WorkerRole Endpoint settings: 
    
    For role WorkerRole1: 
    
    Endpoints WorkerRole1 deployment16(304).CloudTest.WorkerRole1_IN_0: 
    
    InternalEndpoint_DiscoveryService: 127.255.0.0:20000

    So apparently the issue seems to be when you have 2 workerroles. The windows service seems to catch exceptions on the azure specific stuff when the 2nd workerrole is being run. If there is only 1 workerrole, the attached windows service just runs fine.

     

    This some issue with the development emulator ? Or perhaps with multiple processes being run with 2 workerroles instead of 1 workerrole ?

    Anyone has any clue that could get me past this issue please?

     

    Thanks in advance,

    Robbin 


    Be nice to nerds ... Chances are you'll end up working for one!
  • Monday, August 13, 2012 7:24 PM
     
     

    Hi Robbin,

    As like Diagnostic, RoleEnvironment is also available only for web or worker role.

    Probably, you may fixed this on your Windows Service.


    Regards, Sheik (http://udooz.net/blog)