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

StackOverflow https://stackoverflow.com/questions/7391284

  •  29-10-2019
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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;
            }
        }

다른 팁

If you're using an SQL server (either Azure SQL or SQL Server hosted in VM), you could stop the Staging worker role from doing work by only allowing the public IP of the Production instance access to the database server.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top