title: RoleEnvironmentException in Microsoft Azure Instance Deployment
body: <p>I'm having a game with a WCF service and Azure. </p>
  
  <p>I have several WCF Services running successfully on Azure, this particular service has been operating correctly until I decided to redeploy it under a different (existing) Cloud Service. </p>
  
  <p>I removed it from under the role of cloud service X in my VS solution and added it to another cloud service role. I published and when I do, I receive the exception error 
  "Unhandled Exception: Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironmentException". I'm told that the service is been recycled, constantly.</p>
  
  <p>This message appears in the Azure management portal under the instance this WCF service is under. I've tried it on it's own cloud service and under a service with of instances. </p>
  
  <p>Having reviewed scant articles on the issue (<a href="http://blogs.msdn.com/b/davidmcg/archive/2011/03/10/diagnosticmonitor-roleenvironmentexception-was-unhandled.aspx" rel="nofollow">http://blogs.msdn.com/b/davidmcg/archive/2011/03/10/diagnosticmonitor-roleenvironmentexception-was-unhandled.aspx</a>) and (<a href="http://www.microsofttranslator.com/bv.aspx?from=&amp;to=en&amp;a=http%3A%2F%2Fpul.se%2FBlog-Post-Error-RoleEnvironmentException-was-unhandled_Video-ptFrIOhJU6%2ClWCxfMvJiNbE" rel="nofollow">http://www.microsofttranslator.com/bv.aspx?from=&amp;to=en&amp;a=http%3A%2F%2Fpul.se%2FBlog-Post-Error-RoleEnvironmentException-was-unhandled_Video-ptFrIOhJU6%2ClWCxfMvJiNbE</a>)</p>
  
  <p>Remoting onto the instance, I have the following in event viewer:</p>
  
  <pre><code>Application: WaIISHost.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironmentException
  Stack:
     at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.&lt;InitializeRole&gt;b__0()
     at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>I have also found:</p>
  
  <pre><code>Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironmentException: error
     at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetLocalResource(String localResourceName)
     at WCFServiceDataTransfer.AzureLocalStorageTraceListener.GetLogDirectory()
     at WCFServiceDataTransfer.WebRole.OnStart()
  </code></pre>
  
  <p>But this is auto generated code in AzureLocalStorageTraceListener.</p>
  
  <p>How can I get to the bottom of this problem?</p>
  
  <p>Web.Config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;!--  To collect diagnostic traces, uncomment the section below or merge with existing system.diagnostics section.
          To persist the traces to storage, update the DiagnosticsConnectionString setting with your storage credentials.
          To avoid performance degradation, remember to disable tracing on production deployments.
    &lt;system.diagnostics&gt;     
      &lt;sharedListeners&gt;
        &lt;add name="AzureLocalStorage" type="WCFServiceDataTransfer.AzureLocalStorageTraceListener, WCFServiceDataTransfer"/&gt;
      &lt;/sharedListeners&gt;
      &lt;sources&gt;
        &lt;source name="System.ServiceModel" switchValue="Verbose, ActivityTracing"&gt;
          &lt;listeners&gt;
            &lt;add name="AzureLocalStorage"/&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
        &lt;source name="System.ServiceModel.MessageLogging" switchValue="Verbose"&gt;
          &lt;listeners&gt;
            &lt;add name="AzureLocalStorage"/&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
      &lt;/sources&gt; 
     &lt;/system.diagnostics&gt; --&gt;
    &lt;system.diagnostics&gt;
      &lt;trace&gt;
        &lt;listeners&gt;
          &lt;add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics"&gt;        
          &lt;/add&gt;
        &lt;/listeners&gt;
      &lt;/trace&gt;
    &lt;/system.diagnostics&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;client /&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="TransferServiceBehavior" name="WCFServiceDataTransfer.TransferService"&gt;
          &lt;endpoint address="" binding="basicHttpBinding" bindingConfiguration="TransferService" contract="WCFServiceDataTransfer.ITransferService"&gt;
          &lt;/endpoint&gt;
        &lt;/service&gt;
  
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="TransferService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
            transferMode="Streamed"&gt;
            &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
  
          &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="TransferServiceBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;dataContractSerializer maxItemsInObjectGraph="2147483647" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
            &lt;serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500" /&gt;
  
          &lt;/behavior&gt;
          &lt;behavior&gt;                   
            &lt;!-- To avoid disclosing metadata information, set the value below to false before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
  
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
      &lt;!--
          To browse web app root directory during debugging, set the value below to true.
          Set to false before deployment to avoid disclosing web app folder information.
      --&gt;
      &lt;directoryBrowse enabled="true" /&gt;
    &lt;/system.webServer&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
      &lt;httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true" executionTimeout="14400"   /&gt;   
    &lt;/runtime&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Entry</p>
  
  <pre><code>public class WebRole : RoleEntryPoint
      {
          public override bool OnStart()
          {
              // To enable the AzureLocalStorageTraceListner, uncomment relevent section in the web.config  
              DiagnosticMonitorConfiguration diagnosticConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
              diagnosticConfig.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
              diagnosticConfig.Directories.DataSources.Add(AzureLocalStorageTraceListener.GetLogDirectory());
  
              // For information on handling configuration changes
              // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
  
              return base.OnStart();
          }
      }
  </code></pre>