System.Security.VerificationException:操作可能会破坏运行时的稳定性.(亚音速2.2)

Chr*_*ein 9 c# exception asp.net-4.0 subsonic2.2

我最近尝试升级一个.net 2.0项目,该项目将SubSonic 2.2生成的DAL转换为Visual Studio 2010下的.NET 4.0.

这些项目在没有错误的情况下进行了转换,但是当我尝试启动时,我现在收到一条相当卑鄙的错误消息

System.Security.VerificationException: Operation could destabilize the runtime.  

at SubSonic.DataProvider.ApplyConfig(NameValueCollection config, Boolean& parameterValue, String configName) in C:\Documents and Settings\Desktop\4.0 Production\rel_1.0\server\Server.DAL\Server.DAL.SubSonic\DataProviders\DataProvider.cs:line 955
   at SubSonic.DataProvider.Initialize(String name, NameValueCollection config) in C:\Documents and Settings\Desktop\4.0 Production\rel_1.0\server\Server.DAL\Server.DAL.SubSonic\DataProviders\DataProvider.cs:line 916
   at System.Web.Configuration.ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type providerType)
Run Code Online (Sandbox Code Playgroud)

抛出异常的代码:

    ApplyConfig(config, ref extractClassNameFromSPName, ConfigurationPropertyName.EXTRACT_CLASS_NAME_FROM_SP_NAME);

    private static void ApplyConfig(System.Collections.Specialized.NameValueCollection config, ref bool parameterValue, string configName)
    {
        if(config[configName] != null)
        {
            parameterValue = Convert.ToBoolean(config[configName]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它执行类似的调用,唯一的区别是它严格地是一个字符串而不是它正在操纵的布尔值.

private static void ApplyConfig(System.Collections.Specialized.NameValueCollection config, ref string parameterValue, string configName)
{
    if(config[configName] != null)
    {
        parameterValue = config[configName];
    }
}
Run Code Online (Sandbox Code Playgroud)

config被定义为具有3个键的System.Collections.Specialized.NameValueCollection generateNullableProperties,connectionStringName,generatedNamespace extractClassNameFromSPName == false

EDIT1: 启动错误的代码位于Global.asax的Application_Start()方法中

System.Data.SqlClient.SqlDependency.Start(SystemSetting.Schema.Provider.DefaultConnectionString);
Run Code Online (Sandbox Code Playgroud)

EDIT2:错误冒出来引发targetinvocation错误引用我的web.config

<SubSonicService defaultProvider="appPlan">
    <providers>
        <clear/>
        <add name="appPlan" type="SubSonic.SqlDataProvider, appPlan.Server.DAL.SubSonic" generateNullableProperties="false" connectionStringName="appPlan" generatedNamespace="appPlan.Server.DAL"/>
    </providers>
</SubSonicService>
Run Code Online (Sandbox Code Playgroud)

还有其他人遇到过这样的问题吗?我可以升级到SubSonic3.x,但我认为这将是一项更大的任务.

谢谢.

Fun*_*eng 1

这能解决问题吗?

private static void ApplyConfig(System.Collections.Specialized.NameValueCollection config, ref bool parameterValue, string configName)
{
    if(config[configName] != null)
    {
        string val = config[configName];
        parameterValue = Convert.ToBoolean(val);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果没有,那么尝试

string val = config[configName];
if (val.ToLower() == "false")
    parameterValue = false;
else
    parameterValue = true;
Run Code Online (Sandbox Code Playgroud)

原始代码失败的原因可能有两个。首先,.NET 的早期版本(可能是 1.1)存在一些类型问题。NameValueCollection我不知道到底是什么,但我怀疑它可能无法识别直接从into传递的值的类型ToBoolean。第二种可能性是该值不是“真”或“假”,而是其他值。同样,这两个可能是也可能不是原因。我不能确定,因为我没有 SubSonic 2.2。

  • 我尝试了这个,但错误实际上发生在: if(config[configName] != null) 而不是布尔转换处。我一直在作弊,直到我能弄清楚并将其添加到 AssemblyInfo.cs 文件中:: [程序集:SecurityRules(SecurityRuleSet.Level1, SkipVerificationInFullTrust = true)] 我发现提到了 .net4 与 .net2 透明度问题与 Unreliable /unsafe 代码和 .net4.0 的安全规则将行添加到文件中告诉它使用 2.0 规则(据我所知) (2认同)