【问题标题】:How to detect if the environment is staging or production in azure hosted service worker role?如何检测环境是在 azure 托管服务工作者角色中暂存还是生产?
【发布时间】:2015-04-30 16:19:29
【问题描述】:

我在托管服务中有一个辅助角色。 工人每天都在发送电子邮件。 但是在托管服务中,有 2 个环境,Staging 和 Production。 所以我的工人角色每天发送 2 次电子邮件。 我想知道如何检测工人是否处于停滞状态或生产状态。 提前致谢。

【问题讨论】:

标签: azure environment worker


【解决方案1】:

根据我的问题here,您会发现没有快速 方法可以做到这一点。此外,除非您真的知道自己在做什么,否则我强烈建议您不要这样做

但是,如果您愿意,您可以使用一个非常好的库 (Azure Service Management via C#),尽管我们确实有一些 trouble with WCF using it.

这是一个关于如何做的快速示例(注意,您需要将管理证书作为资源包含在代码中并将其部署到 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】:

    如果您使用的是 SQL 服务器(Azure SQL 或托管在 VM 中的 SQL Server),您可以通过仅允许生产实例的公共 IP 访问数据库服务器来阻止暂存辅助角色的工作。

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2012-12-31
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多