Windows AZURE:类库:如何知道是在 Web 角色中运行还是在常规 Web 服务中运行

发布于 2024-12-08 12:03:39 字数 140 浏览 0 评论 0原文

我有一个类库,有时由 WebRole 服务引用,有时由内部服务器上的 IIS 中运行的常规 WCF 服务引用。 (它是一个包含util函数的内部框架类库)

该类库内​​部是否有办法检测它当前是在Windows AZURE环境中运行还是在IIS中运行?

I have a class library that is sometimes referenced by a WebRole service and sometimes by a regular WCF service running in IIS on an internal server. (It's an in-house framework class library containing util functions)

Is there a way inside that class library to detect if it is currently running in the Windows AZURE environment or running in IIS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2024-12-15 12:03:39

我们使用一个带有几个属性的静态类,这些属性使用来自 Microsoft.WindowsAzure.ServiceRuntimeRoleEnvironment

    public static bool InAzureEnvironment
    {
        get
        {
            return RoleEnvironment.IsAvailable;
        }
    }

    public static bool InCloud
    {
        get
        {
            return InAzureEnvironment && !RoleEnvironment.IsEmulated;
        }
    }

效果很好。

We use a static class with a couple of properties that use RoleEnvironment from Microsoft.WindowsAzure.ServiceRuntime:

    public static bool InAzureEnvironment
    {
        get
        {
            return RoleEnvironment.IsAvailable;
        }
    }

    public static bool InCloud
    {
        get
        {
            return InAzureEnvironment && !RoleEnvironment.IsEmulated;
        }
    }

which works just fine.

不奢求什么 2024-12-15 12:03:39

这会带来一些规律性,但老实说,我会让事情变得简单,并根据配置做出这样的决定。要么通过在运行时读取的显式配置设置,要么依赖应用程序配置中定义的容器配置的依赖项注入。

最终,应用程序几乎肯定会被专门重新打包以发布到 Azure,因此特定于部署的配置并不是什么大问题。

This comes up with some regularity, but honestly, I'd keep things simple and base such decisions on configuration. Either by having an explicit configuration setting that you read at runtime, or relying on dependency injection with the container configuration defined within the application configuration.

At the end of the day, the application will almost certainly be repackaged specifically to publish to Azure, so a deployment-specific config is no big issue.

孤独患者 2024-12-15 12:03:39

对于需要与云环境无关的代码,使用环境变量可能是个好主意。对于在 Windows Azure 中运行的服务,您可以添加如下内容:

<Runtime>
  <Environment>
     <Variable name="INCLOUD" value="true" />
 </Environment>
...

此环境变量现在将仅显示在 Windows Azure 中运行(当然假设您不在云中时不在本地设置)。您的代码不需要“了解”有关 RoleEnvironment 的任何信息或引用 ServiceHosting.dll。

如果您需要确定是否在仿真中运行或使用任何 RoleEnvironment 设置,您也可以在此处使用基于新 Xpath 变量的变量。检查 http://msdn.microsoft.com/en-us/library/ windowsazure/hh404006.aspx 了解更多信息。

For code that needs to be agnostic to the cloud environment, it might be a good idea to use an environment variable. For services running in Windows Azure, you would add something like:

<Runtime>
  <Environment>
     <Variable name="INCLOUD" value="true" />
 </Environment>
...

This env variable will now only show running in Windows Azure (assuming you don't set this locally when not in cloud of course). Your code will not need to 'know' anything about RoleEnvironment or reference ServiceHosting.dll.

If you need to determine if you are running in emulation or using any RoleEnvironment settings, you can use the new Xpath variable based vars as well here. Check http://msdn.microsoft.com/en-us/library/windowsazure/hh404006.aspx for more information on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文