2011-09-12 15 views
6

Ho un ruolo di lavoratore nel mio servizio in hosting. Il lavoratore sta inviando le basi giornaliere di posta elettronica. Ma nel servizio ospitato ci sono 2 ambienti, Staging e Production. Quindi il mio ruolo di lavoratore invia e-mail 2 volte al giorno. Mi piacerebbe sapere come rilevare se il lavoratore è in stagnazione o produzione. Grazie in anticipo.Come rilevare se l'ambiente è in fase di staging o di produzione nel ruolo di operatore di servizio in hosting in azzurro?

+1

Leggi qui: http://stackoverflow.com/questions/4328462/staging-or-production-instance – Igorek

risposta

1

Come per la mia domanda here, vedrete che non c'è il veloce modo di farlo. Inoltre, a meno che tu non sappia veramente cosa stai facendo, ti consiglio caldamente di non farlo .

Tuttavia, se si desidera, è possibile utilizzare una veramente bella libreria (Azure Service Management via C#) anche se abbiamo avuto un po 'di trouble with WCF using it.

Ecco un esempio veloce su come farlo (nota, è necessario includere il certificato di gestione come risorsa nel codice & distribuirlo a Azure):

private static bool IsStaging() 
     { 
      try 
      { 
       if (!CloudEnvironment.IsAvailable) 
        return false; 

       const string certName = "AzureManagement.pfx"; 
       const string password = "Pa$$w0rd"; 

       // load certificate 
       var manifestResourceStream = typeof(ProjectContext).Assembly.GetManifestResourceStream(certName); 
       if (manifestResourceStream == null) 
       { 
        // should we panic? 
        return true; 
       } 

       var bytes = new byte[manifestResourceStream.Length]; 
       manifestResourceStream.Read(bytes, 0, bytes.Length); 

       var cert = new X509Certificate2(bytes, password); 

       var serviceManagementChannel = Microsoft.Toolkit.WindowsAzure.ServiceManagement.ServiceManagementHelper. 
        CreateServiceManagementChannel("WindowsAzureServiceManagement", cert); 

       using (new OperationContextScope((IContextChannel)serviceManagementChannel)) 
       { 
        var hostedServices = 
         serviceManagementChannel.ListHostedServices(WellKnownConfiguration.General.SubscriptionId); 

        // because we don't know the name of the hosted service, we'll do something really wasteful 
        // and iterate 
        foreach (var hostedService in hostedServices) 
        { 
         var ad = 
          serviceManagementChannel.GetHostedServiceWithDetails(
           WellKnownConfiguration.General.SubscriptionId, 
           hostedService.ServiceName, true); 

         var deployment = 
          ad.Deployments.Where(
           x => x.PrivateID == Zebra.Framework.Azure.CloudEnvironment.CurrentRoleInstanceId). 
           FirstOrDefault 
           (); 

         if (deployment != null) 
         { 
          return deployment.DeploymentSlot.ToLower().Equals("staging"); 
         } 
        } 
       } 

       return false; 
      } 
      catch (Exception e) 
      { 
       // if something went wrong, let's not panic 
       TraceManager.AzureFrameworkTraceSource.TraceData(System.Diagnostics.TraceEventType.Error, "Exception", e); 
       return false; 
      } 
     } 
0

Se si utilizza un server SQL (o SQL Azure o SQL Server ospitato in VM), si potrebbe fermare il ruolo di lavoratore Staging da fare il lavoro consentendo solo l'IP pubblico dell'istanza di produzione di accedere al server di database.