调用RoleEnvironment.GetConfigurationSettingValue("MYKEY")时为什么会出现SEHException?

Kor*_*ori 38 c# azure

我试着RoleEnvironment.GetConfigurationSetting("SOMEKEY")像这样打电话:

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;
}
Run Code Online (Sandbox Code Playgroud)

我的ServiceConfiguration.cscfg键如下所示:

<Setting name="CHARGIFY_API_KEY" value="AbCdEfGhIjKlMnOp" />
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

异常详细信息:System.Runtime.InteropServices.SEHException:外部组件引发了异常.

[SEHException(0X80004005):外部组件引发的异常.] RoleEnvironmentGetConfigurationSettingValueW(UINT16*,UINT16*,UInt32的,UInt32的*)0 Microsoft.WindowsAzure.ServiceRuntime.Internal.InteropRoleManager.GetConfigurationSetting(字符串名称,字符串&RET)92微软.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(字符串configurationSettingName)67 ChargifyNET.ChargifyAzurePage.get_Chargify()在C:\ NetProjects\ChargifyDotNET \源\ Chargify.NET\ChargifyAzurePage.cs:26 Chargify.Azure._Default.Page_Load(对象发件人,EventArgs e)在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(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+627

Dav*_*gon 85

如果您未在开发结构或Azure结构中运行,则尝试访问RoleEnvironment时,将获得SEHException.我相信你无意中在asp.net开发服务器下运行你的网站,这意味着你不在开发结构中(我已经确认这会抛出一个SEHException).换句话说,您要么将您的网站项目设置为启动项目,要么右键单击它并告诉它运行.

您必须将云项目本身设置为启动项目,然后默认情况下将显示您的网站在端口81上运行.云项目是作为其成员的所有角色定义的项目.您可以查看浏览器的URL栏并轻松判断您是否在asp.net开发服务器中运行,因为您将使用某个随机端口号而不是端口81.

您应该通过检查确保您在开发结构或Azure结构中运行RoleEnvironment.IsAvailable.如果这是真的,你可以安全地在RoleEnvironment中调用任何东西.如果它是假的,那么你就不会在结构中运行.

  • 一个小观察:RoleEnvironment.IsAvailable是一个属性,而不是一个方法. (2认同)