2010-06-02 29 views
38

我試圖打電話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

回答

82

你會如果您未在開發結構或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

1

爲了跟上這一點,爲了防止有人再次遇到同樣的問題,也可能出現這種情況,無論出於何種原因,您的某個部署卡在計算模擬器中。

發生在我身上的是我有一個包含多個網站的webrole,每個網站綁定到不同的主機名。說:localhost和test.localhost。通常,你可以在localhost:81和test.localhost:81上訪問它們。因爲雖然一些奇怪的原因,一個部署鑽進它會在計算模擬器上市,沒有Visual Studio的調試它一個奇怪的狀態,它會說:「角色實例毀」或沿着這些線路的東西..這種部署仍然有部署在IIS中的網站。然後,我不知道這個錯誤部署,訪問默認的網址,即。 test.localhost:81這將加載舊的部署文件。 (舊)網站一直工作,直到我打開了一個實際使用RoleEnvironment.GetConfigurationSettingValue方法的頁面,然後纔得到該例外。這真是令人沮喪,因爲這種笨拙的部署obvioulsy沒有達到任何斷點也沒有打破例外,但它看起來完全一樣,我一直在努力的網站..

當我意識到這一點,我打開主機名下的新港口和那裏的頁面按預期工作。一旦我刪除從計算模擬器這個馬車部署IIS網站也刪除了,幸好端口現在可以用作預期..

0

如果你確保你正在運行dev的面料後,同樣的錯誤,請嘗試將實例數減少到一個。這爲我修好了。

不過,似乎不可思議,我不能用兩個實例進行調試。

2

刪除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不可用,它可能會記錄更多的實際問題。

0

儘管許多指出的是,你應該開發/ 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不適用。

相關問題