加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Json.NET在.NET 4下说“操作可能会使运行时不稳定”,但在.NET 3.

发布时间:2020-12-16 18:40:57 所属栏目:百科 来源:网络整理
导读:这段代码: namespace ConsoleApplication3{ class Program { static void Main(string[] args) { var client = new WebClient(); client.Headers.Add("User-Agent","Nobody"); var response = client.DownloadString(new Uri("http://www.hanselman.com/sma
这段代码:

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new WebClient();
            client.Headers.Add("User-Agent","Nobody");
            var response = client.DownloadString(new Uri("http://www.hanselman.com/smallestdotnet/json.ashx"));

            var j = JsonConvert.DeserializeObject<SmallestDotNetThing>(response);
        }

        public class SmallestDotNetThing
        {
            public DotNetVersion latestVersion { get; set; }
            public List<DotNetVersion> allVersions { get; set; }
            public List<DotNetVersion> downloadableVersions { get; set; }
        }

        public class DotNetVersion
        {
            public int major { get; set; }
            public int minor { get; set; }
            public string profile { get; set; }
            public int? servicePack { get; set; }
            public string url { get; set; }
        }

    }
}

在.NET 4下使用.NET 4版本的JSON.NET时,将在Deserialize上抛出异常“操作可能会破坏运行时的稳定性”.

但是,将目标切换到3.5(并将JSON.NET引用更改为3.5版本)效果很好.我正在使用NuGet的JSON.NET.

思考?

解决方法

.NET 4的运行时(参见 Karel Zikmunds answer和 .NET Security Blog条目)中的安全模型似乎发生了变化,它依赖于AllowPartiallyTrustedCallersAttribute.

Karel还发布了一些解决方案:

You have these options:

  1. If you don’t need APTCA,remove it.
  2. Run SecAnnotate tool from SDK and fix all transparency violations –
    07002.
  3. Use Level1 attribute to switch your assembly to v2 security model –
    07003

Stackoverflow上的另一个post,C#中的协方差和逆变量可能存在问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读