如何检测如果环境分期或生产蔚蓝的托管服务工作者的角色? [英] How to detect if the environment is staging or production in azure hosted service worker role?
问题描述
我在我的托管服务辅助角色。
工人正在发送电子邮件每日基地。
但在托管服务,有2个环境,分期和生产。
所以,我的辅助角色发送电子邮件每日2次。
我想知道如何检测,如果职工在stagning或生产。
先谢谢了。
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.
推荐答案
根据我的问题<一href=\"http://stackoverflow.com/questions/7110092/fetching-azure-subscription-id-deployment-slot-really-fast/7111195#7111195\">here,你会看到,没有任何的快速的这样的方式。另外,除非你真的知道自己在做什么,我强烈建议您无法做到这一点
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.
不过,如果你愿意,你可以用一个非常好的图书馆(天青通过C#),虽然我们也有一些的麻烦,使用WCF它。
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.
下面是关于如何做到这一点的快速样本(注意,你需要包括管理证书在code和资源,将其部署到Azure中):
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;
}
}
这篇关于如何检测如果环境分期或生产蔚蓝的托管服务工作者的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!