强制转换会导致“操作可能会破坏运行时的稳定性”在一个 ASP.NET 4.0 机器上而不是在另一个机器上

发布于 2024-12-08 01:28:52 字数 862 浏览 3 评论 0原文

这是我在 .NET 2.0 中使用多年的简单反序列化方法(部分)。 T 是一个不受约束的类型参数。

protected virtual T ItemFromString(string s) {

    if (typeof(T).IsPrimitive ||
        typeof(T) == typeof(string)) {

        try {
            // needed for string, too: compiler doesn't allow (T)s
            return (T)Convert.ChangeType(s, typeof(T));
        }
        catch (Exception ex) {
            // stuff
        }
    }
}

我将应用程序池更改为在 4.0 中运行,一切都很好 — 直到我将其部署到远程服务器。在那里,我得到了“操作可能会破坏运行时的稳定性”

return (T)Convert.ChangeType(s, typeof(T));

(实际上报告的行是该方法的结束大括号,但我已将其

范围缩小到该行。)如果我将运行时改回为2.0。

运行时版本均为 4.0.30319。该应用程序在其他方面是相同的,包括 web.config。这两个应用程序都使用共享应用程序池并以完全信任的方式运行。大概是另一个设置影响了它,但我不知道如何弄清楚是什么。

我发现的关于此的所有内容都涉及Reflection.Emit 或协方差,但我没有使用它们。

有线索吗?

谢谢。

Here's (part of) a simple deserialization method that I've used in .NET 2.0 for years. T is an unconstrained type parameter.

protected virtual T ItemFromString(string s) {

    if (typeof(T).IsPrimitive ||
        typeof(T) == typeof(string)) {

        try {
            // needed for string, too: compiler doesn't allow (T)s
            return (T)Convert.ChangeType(s, typeof(T));
        }
        catch (Exception ex) {
            // stuff
        }
    }
}

I changed the application pool to run in 4.0, and everything was fine — until I deployed it to the remote server. There, I get "Operation could destabilize the runtime" on the line

return (T)Convert.ChangeType(s, typeof(T));

(Actually the line reported is the ending brace of the method, but I've narrowed it down to that line.)

The problem goes away if I change the runtime back to 2.0.

The runtime versions are both 4.0.30319. The app is otherwise identical, including web.config. Both apps using shared application pools and running in Full trust. Presumably another setting is affecting it, but I have no idea how to figure out what.

Everything I've found about this involves Reflection.Emit or covariance, which I'm not using.

Any leads?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2024-12-15 01:28:52

既然这个问题已经被查看了 0x80 次,我将发布我最近在无法再推迟迁移到 .NET 4 时使用的解决方案。

您必须将 .NET 4 平台作为目标构建,并设置以下程序集属性。

using System.Security;

// Needed to enable generic deserialization in partial trust.
[assembly: SecurityRules(SecurityRuleSet.Level1)]

我曾评论说我处于完全信任模式,但我一定是错的。这是部分信任环境中的一个问题。

Now that this question has been viewed 0x80 times, I'll post the solution I recently used when I could no longer put off the move to .NET 4.

You have to target the .NET 4 platform in the build, and set the following assembly property.

using System.Security;

// Needed to enable generic deserialization in partial trust.
[assembly: SecurityRules(SecurityRuleSet.Level1)]

I'd commented that I was in full trust mode, but I must have been wrong about that. This is only an issue in partially-trusted environments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文