Search code examples
azureenvironmentworker

How to detect if the environment is staging or production in azure hosted service worker role?


I have a worker role in my hosted service. The worker is sending e-mail daily bases. But in the hosted service, there are 2 environment, Staging and Production. So my worker role sends e-mail 2 times everyday. I'd like to know how to detect if the worker is in stagning or production. Thanks in advance.


Solution

  • As per my question here, you'll see that there is no fast way of doing this. Also, unless you really know what you are doing, I strongly suggest you not do this.

    However, if you want to, you can use a really nice library (Azure Service Management via C#) although we did have some trouble with WCF using it.

    Here's a quick sample on how to do it (note, you need to include the management certificate as a resource in your code & deploy it to 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;
                }
            }