为什么在调用RoleEnvironment.GetConfigurationSettingValue(“MYKEY”)时收到SEHException?

问题描述:

我试图打电话RoleEnvironment.GetConfigurationSetting("SOMEKEY")像这样:为什么在调用RoleEnvironment.GetConfigurationSettingValue(“MYKEY”)时收到SEHException?

public partial class AzureBasePage : System.Web.UI.Page 
{ 
    protected ChargifyConnect Chargify 
    { 
     get { 
      if (this._chargify == null) { 
       this._chargify = new ChargifyConnect(); 
       this._chargify.apiKey = RoleEnvironment.GetConfigurationSettingValue("CHARGIFY_API_KEY"); 
      } 
      return this._chargify; 
     } 
    } 
    private ChargifyConnect _chargify = null; 
} 

我ServiceConfiguration.cscfg关键是这样的:

<Setting name="CHARGIFY_API_KEY" value="AbCdEfGhIjKlMnOp" /> 

而且我得到这个错误:

Exception Details: System.Runtime.InteropServices.SEHException: External component has thrown an exception.

[SEHException (0x80004005): External component has thrown an exception.] RoleEnvironmentGetConfigurationSettingValueW(UInt16* , UInt16* , UInt32 , UInt32*) +0 Microsoft.WindowsAzure.ServiceRuntime.Internal.InteropRoleManager.GetConfigurationSetting(String name, String& ret) +92 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(String configurationSettingName) +67 ChargifyNET.ChargifyAzurePage.get_Chargify() in C:\NetProjects\ChargifyDotNET\Source\Chargify.NET\ChargifyAzurePage.cs:26 Chargify.Azure._Default.Page_Load(Object sender, EventArgs e) in C:\NetProjects\ChargifyDotNET\Source\Chargify.Azure\Default.aspx.vb:8 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

+0

是什么'Marshal.GetExceptionCode()'返回,当你在为'SEHException' catch块是? – 2010-06-02 13:48:30

+0

它返回“-1066598274”调用'Marshal.GetExceptionCode()的时候' – Kori 2010-06-02 14:35:59

+0

你确定你是在Windows Azure上运行? (开发结构还是真正的云?) – smarx 2010-06-02 17:17:18

你会如果您未在开发结构或Azure结构中运行,则尝试访问RoleEnvironment时会得到SEHException。我相信你无意中在asp.net开发服务器下运行你的网站,这意味着你不在开发结构中(我已经证实这会引发SEHException)。换句话说,你可以将你的网站项目设置为启动项目,或者你已经右键单击它并让它运行。

您必须将云项目本身设置为启动项目,默认情况下会显示您的网站在端口81上运行。云项目是作为其成员拥有所有角色定义的项目。你可以看一下浏览器的地址栏,轻松地告诉我们,如果你在asp.net开发服务器上运行,因为你会在一些随机端口号,而不是81端口

你应该确保你通过检查RoleEnvironment.IsAvailable运行在开发结构或Azure结构中。如果那是真的,你可以安全地调用RoleEnvironment中的任何东西。如果它是错误的,那么你并没有在结构中运行。

+0

谢谢。这是一个很好的答案,我会核实 - 但我确信就是这样。 – Kori 2010-06-18 15:57:41

+1

关于解决这个问题的另一个评论,我创建了一个需要在Azure和非Azure环境中运行的库 - 所以我不能直接引用RoleEnvironment。然而,我确实使用了反射来确定RoleEnvironment.IsAvailable()的值 – Kori 2010-07-08 15:29:21

+2

一个小观察:RoleEnvironment.IsAvailable是一个属性,而不是一个方法。 – 2012-02-16 19:54:38

为了跟上这一点,为了防止有人再次遇到同样的问题,也可能出现这种情况,无论出于何种原因,您的某个部署卡在计算模拟器中。

发生在我身上的是我有一个包含多个网站的webrole,每个网站绑定到不同的主机名。说:localhost和test.localhost。通常,你可以在localhost:81和test.localhost:81*问它们。因为虽然一些奇怪的原因,一个部署钻进它会在计算模拟器上市,没有Visual Studio的调试它一个奇怪的状态,它会说:“角色实例毁”或沿着这些线路的东西..这种部署仍然有部署在IIS中的网站。然后,我不知道这个错误部署,访问默认的网址,即。 test.localhost:81这将加载旧的部署文件。 (旧)网站一直工作,直到我打开了一个实际使用RoleEnvironment.GetConfigurationSettingValue方法的页面,然后才得到该例外。这真是令人沮丧,因为这种笨拙的部署obvioulsy没有达到任何断点也没有打破例外,但它看起来完全一样,我一直在努力的网站..

当我意识到这一点,我打开主机名下的新港口和那里的页面按预期工作。一旦我删除从计算模拟器这个马车部署IIS网站也删除了,幸好端口现在可以用作预期..

如果你确保你正在运行dev的面料后,同样的错误,请尝试将实例数减少到一个。这为我修好了。

不过,似乎不可思议,我不能用两个实例进行调试。

删除ServiceDefinition.csdef文件中的<Sites>标记对您来说可能是一种替代方法,因为您的网站将不会部署到云上的完整IIS。我们正在使用SDK的1.7。

总而言之:RoleEnvironment.IsAvailable = False与这包括在ServiceDefinition.csdef与实例计数为1我可能会添加。

<Sites> 
     <Site name="Blah"> 
     <Bindings> 
      <Binding name="Endpoint1" endpointName="Http" /> 
      <Binding name="Endpoint1" endpointName="Https" /> 
     </Bindings> 
     </Site> 
</Sites> 

取出<Sites>节点和部署,你可能会发现,现在RoleEnvironment.IsAvailable = True

有关于什么是实际发生的情况非常少的日志 - 该网站是罚款运行,没有警告,除了平时的你只有1个实例,为什么不部署2,该网站已启动并运行良好。

这是最近的问题,我相信一定会有在msshrtmi.dll做了一些修改。如果RoleEnvironment不可用,它可能会记录更多的实际问题。

尽管许多指出的是,你应该开发/ Azure结构,而不是asp.net开发服务器上运行,我认为这是值得一提的是,你需要选择正确的执行模型当您发布的应用程序如果你想使用RoleEnvironment Azure上。

是有3种型号,截至目前:

  • VM
  • 网站
  • 云服务

请参考这里:http://azure.microsoft.com/en-us/documentation/articles/fundamentals-application-models了解更多详情。

,特别是以下内容:

Cloud Services, which was the initial execution model provided by Azure, is an explicitly PaaS approach. While the line between PaaS and web hosting is blurry, Cloud Services differs in some important ways from Web Sites, including the following:

  • Unlike Web Sites, Cloud Services gives you administrative access to your application's VMs. This lets you install arbitrary software that your application needs, something that's not possible with Web Sites.
  • Because Cloud Services offers both web roles and worker roles, it's a better choice than Web Sites for multi-tier applications that need separate VMs for their business logic.
  • Cloud Services provides separate staging and production environments, making application updates somewhat smoother than Web Sites.
  • Unlike Web Sites, you can use networking technologies such as Azure Virtual Network and Azure Connect to hook on-premises computers to Cloud Services applications.
  • Cloud Services lets you use Remote Desktop to connect directly to an application's VMs, something that's not possible with Web Sites.

您可以检查RoleEnvironment.IsAvailable。如果它为假,则您的应用程序未运行Azure运行时,这意味着RoleEnvironment不适用。