9

我最近尝试将其 DAL 由 SubSonic 2.2 生成的 .net 2.0 项目升级到 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)

抛出异常的代码:

    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]);
        }
    }

它执行与此处类似的调用,唯一的区别是它严格来说是一个字符串,而不是它正在操作的布尔值。

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

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

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

System.Data.SqlClient.SqlDependency.Start(SystemSetting.Schema.Provider.DefaultConnectionString);

EDIT2:错误冒泡导致引用我的 web.config 的 targetinvocation 错误

<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>

有没有其他人遇到过这样的问题?我可以升级到 SubSonic3.x,但我相信这将是一项更大的任务。

谢谢。

4

2 回答 2

3

在直接从手工制作的 IL 生成程序集之前,我已经看到过这个异常。.NET 运行时验证程序集中的原始指令的正确性,尤其是在将程序集加载到受限上下文时。例如,有一项检查以确保在执行方法之前将所需数量的参数加载到调用堆栈中。

即使验证失败,仍然可以加载程序集;但它只能在完全信任的情况下运行。在部分信任场景中,您会收到此“操作可能会破坏运行时”错误。原因是如果程序集“行为不正确”,运行时无法保证在部分信任下安全运行程序集。

PEVERIFY您可以使用该工具(可通过 Visual Studio 命令提示符获得)手动检查程序集。尝试验证所有引用的程序集以查看报告的内容。我怀疑 .NET 2.0 和 .NET 4.0 之间的验证规则发生了变化,导致其中一个 SubSonic 2.2 程序集的验证失败。

您在回应 Fun Mun Pieng 时提到的作弊也表明验证是问题所在。

于 2011-03-14T15:22:30.033 回答
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);
    }
}

如果没有,那就试试

string val = config[configName];
if (val.ToLower() == "false")
    parameterValue = false;
else
    parameterValue = true;

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

于 2011-03-11T03:34:13.570 回答