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

荒凉一梦 提交于 2019-12-05 08:05:17
Anže Vodovnik

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.

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