gpt4 book ai didi

c# - 我可以检测我的代码是否在 Azure 辅助角色中执行吗?

转载 作者:太空狗 更新时间:2023-10-29 21:13:03 24 4
gpt4 key购买 nike

我有一些在 Winforms 应用程序、Windows 服务和现在的 Azure 辅助角色中使用的共享程序集/项目。

有什么方法可以在运行时检测我是否在 Azure 角色中运行。

我找到了如何检测是否正在运行 Azure 模拟器:

 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsEmulated

但这并没有达到我想要的效果。我也不想在我的共享程序集中添加对任何 Azure 程序集的引用。

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

System.Environment.UserInteractive

有什么东西可以给我这个逻辑吗?

最佳答案

对于任何感兴趣的人,我想分享一下我是如何实现的,感谢@Sandrino Di Mattia 的回答:

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

请注意,这并不适合 Winforms 应用程序,因为我最终实际上只需要它用于服务 - 即在运行为

的服务之间进行检测
  • Azure 辅助角色
  • Windows 服务
  • 控制台应用程序

这是一个大纲:

public static class ServiceRunner
{
private static bool IsAzureWorker
{
get { return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("RoleRoot")); }
}

public static void Run(string[] args)
{
if (IsAzureWorker)
{
//Running as Azure Worker
}
else if (Environment.UserInteractive) //note, this is true for Azure emulator too
{
//Running as Console App
}
else
{
//Running as Windows Service
}
}
}

关于c# - 我可以检测我的代码是否在 Azure 辅助角色中执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20561896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com