我可以检测我的代码是否以Azure辅助角色执行吗? [英] Can I detect if my code is executing in an Azure worker role?

查看:53
本文介绍了我可以检测我的代码是否以Azure辅助角色执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在Winforms应用程序,Windows服务以及现在的Azure工作人员角色中使用的共享程序集/项目.

I have some shared assemblies/projects that are used within Winforms apps, windows services and now Azure worker roles.

如果我以Azure角色运行,是否可以在运行时检测到任何方式.

Is there any way that I can detect at runtime if I am running in an Azure role.

我已经找到了如何检测是否运行Azure模拟器的方法:

I have found how you can detect if running Azure emulator or not:

 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsEmulated

但这不能满足我的要求.我也希望不必在共享程序集中添加对任何Azure程序集的引用.

But this does not do what I want. I would also prefer not to have to add references to any of the Azure assemblies in my shared assemblies.

理想情况下,我想要类似于用来检测是作为控制台还是服务运行的东西:

Ideally I would like something similar to what I use to detect if running as a console vs a service:

System.Environment.UserInteractive

有什么能给我这种逻辑的吗?

Is there anything that gives me this logic?

推荐答案

您可以检查RoleRoot环境变量的存在(至少对于Cloud Services):

You can check for the presence of the RoleRoot environment variable (for Cloud Services at least):

  • http://blog.toddysm.com/2011/03/what-environment-variables-can-you-use-in-windows-azure.html
  • MSDN: http://msdn.microsoft.com/en-us/library/windowsazure/gg557552.aspx

或者,为什么不简单地将设置添加到您的配置(AppSettings或服务配置):

Or, why not simply add a setting to your config (AppSettings or Service Configuration):

  <appSettings>
    ...
    <add key="AppEnvironment" value="Azure.CloudService|Azure.Website" />
  </appSettings>

然后,您可以简单地检查设置是否存在特定的值,以查看运行位置.这也意味着在(自动)构建或部署过程中,您将需要包括此设置(例如,使用XDT可以做到这一点).

Then you can simply check if the setting exists with a specific value to see where you're running. This also means that during your (automated) build or deploy process you'll need to include this setting (this is possible with XDT for example).

这篇关于我可以检测我的代码是否以Azure辅助角色执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆