天青:code拉设置,将在所有三种情况下工作:湛蓝的网站,角色和放大器;本地 [英] Azure: code to pull settings that will work in all three scenarios: azure website, role & locally
问题描述
我目前正在测试我的ASP.NET网站有三种:
I'm currently testing my ASP.NET site in three ways:
- 调试本地
- 部署为一个蓝色的网站
- 测试中的Azure模拟器(即将与角色部署)
我有一个如果/其他这是试图以检测作用环境可用。它工作正常,在当地,但在Azure的网站环境中运行时炸毁了:
I have an if/else which is trying to detect if the role environment is available. It works fine locally, but blows up when run in the Azure website environment with:
System.IO.FileLoadException:无法加载文件或程序
Microsoft.WindowsAzure.ServiceRuntime,版本= 2.5.0.0,
文化=中性公钥= 31bf3856ad364e35'或一个其
依赖。找到的程序集清单定义不
匹配程序集引用。 (异常来自HRESULT:0x80131040)
文件名:'Microsoft.WindowsAzure.ServiceRuntime,版本= 2.5.0.0,
文化=中性公钥= 31bf3856ad364e35
System.IO.FileLoadException: Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) File name: 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
我的code是如下:
if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) && Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable)
{
//drop item on the queue for it to be picked up by the worker role
}
else
{
//Process here
}
我有短路使得RoleEnvironment code将永远不会在一个网站时运行。我已附加一个调试器,以验证该语句的第一部分返回false。但是,发生异常时,我第一个跳进包含调用该方法。
I have it short circuit such that the RoleEnvironment code will never run when on a website. I've attached a debugger to verify that the first part of the statement returns false. But the exception happens whenever I first jump into the method containing that call.
有没有人有任何建议,让我的code正常工作,无论它是否有可用的角色运行,在蔚蓝的网站,或在本地?它的部署后,我没有做任何定制网站。
Does anyone have any suggestions to allow my code to function correctly, regardless of if it's running with roles available, in an azure website, or locally? I haven't done anything to customize the website after it's deployed.
编辑:在此基础上螺纹:
<一href=\"http://stackoverflow.com/questions/14189071/could-not-load-file-or-assembly-microsoft-windowsazure-serviceruntime-version\">Could无法加载文件或程序集Microsoft.WindowsAzure.ServiceRuntime,当版本部署到云 = 1.8.0.0
based on this thread: Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=1.8.0.0 when deployed to the cloud
大会Microsoft.WindowsAzure.Diagnostics和Microsoft.WindowsAzure.ServiceRuntime不能在网站上使用。
The assemblies Microsoft.WindowsAzure.Diagnostics and Microsoft.WindowsAzure.ServiceRuntime cannot be used in a Web Site.
如果是这样的话,有没有什么办法来帮助实现自动化,这样我就不必维护我的code两个独立版本?
If that's the case, is there any way to help automate this so that I don't have to maintain two separate versions of my code?
编辑2:我在考虑做条件编译解决这个问题。不是全自动的,但可能是最好的解决方案。
Edit 2: I'm considering doing conditional compilation around this. Not fully automatic, but might be the best possible solution.
推荐答案
在我的经验,在你有需要知道,如果他们在一个云服务或Azure的网站运行code /库的情况下,是最好用的,而不是试图去检查是否 RoleEnvironment
可配置设置。所有我见过的解决方案,或者是片状,或要求还可以部署额外的依赖,这是一个麻烦。
In my experience, in cases where you have code / libraries that need to be aware if they are running in a Cloud Service or an Azure Website, are best to use a configuration setting instead of trying to check if a RoleEnvironment
is available. All of the solutions I've seen are either flaky, or require additional dependencies to also be deployed, which is a hassle.
读取配置的最佳方法是使用 CloudConfigurationManager.GetSetting()
方法。
The best way to read configuration is to use the CloudConfigurationManager.GetSetting()
method.
这是聪明的,从一个云服务配置文件,如果present足够的阅读,然后将回退要么在的app.config
或的web.config
。
It is smart enough read from a Cloud Service configuration file if present, and will then fall-back to either the app.config
or web.config
.
这篇关于天青:code拉设置,将在所有三种情况下工作:湛蓝的网站,角色和放大器;本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!