Windows AZURE:类库:如何知道是在 Web 角色中运行还是在常规 Web 服务中运行
我有一个类库,有时由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们使用一个带有几个属性的静态类,这些属性使用来自
Microsoft.WindowsAzure.ServiceRuntime
的RoleEnvironment
:效果很好。
We use a static class with a couple of properties that use
RoleEnvironment
fromMicrosoft.WindowsAzure.ServiceRuntime
:which works just fine.
这会带来一些规律性,但老实说,我会让事情变得简单,并根据配置做出这样的决定。要么通过在运行时读取的显式配置设置,要么依赖应用程序配置中定义的容器配置的依赖项注入。
最终,应用程序几乎肯定会被专门重新打包以发布到 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.
对于需要与云环境无关的代码,使用环境变量可能是个好主意。对于在 Windows Azure 中运行的服务,您可以添加如下内容:
此环境变量现在将仅显示在 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:
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.