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

柔情痞子 提交于 2019-12-22 05:29:20

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/7391284/how-to-detect-if-the-environment-is-staging-or-production-in-azure-hosted-servic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!