System.Security.Permissions missing when invoking JsonConvert.DeserializeObject<T> in .NET Core 2.0

谁都会走 提交于 2020-05-07 12:49:08

问题


I am currently looking at using .NET Core 2.0 so that I can run my app on multiple platforms.

One thing I need to do is take an incoming string and deseralise it into an object. e.g.

var resultingObject = JsonConvert.DeserializeObject<MyDataContract>(request);

In full .NET, this would run and return me my object. However in .NET Core 2.0 I get the following exception

Could not load file or assembly 'System.Security.Permissions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
at Newtonsoft.Json.Serialization.JsonTypeReflector.get_DynamicCodeGeneration()
 at Newtonsoft.Json.Serialization.JsonTypeReflector.get_ReflectionDelegateFactory()
 at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator(Type createdType)
 at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)
 at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
 at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
 at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
 at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
 at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
 at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)

After some reading around I found this on StackOverflow which suggests that this kind of operation is not permitted in .NET Core https://stackoverflow.com/a/38385774/1211743

Does anyone have any insight?


回答1:


This was due to a lack of understanding of how .NET Core works. I opened up the csproj and added a reference to the required file 'System.Security.Permissions' and reloaded the project. At this point, nuget resolved it. Json.NET now works as expected.




回答2:


In my case, I've simply updated to Newtonsoft.Json latest available version and the issue is gone. I had the issue while migrating a lib from .net framework to .net standard and it was quite outdated.




回答3:


First look at the Packages section of your solution, most probably you will see warnings there:

You should know that NuGet will not necessarily install the latest version for the library.
Especially with the Newtonsoft.Json version 8.0.3 there is an issue with System.Security.Permissions reference, however, such an issue already doesn't exist with the 12.0.3 version.

In this case (and most probably for a lot of other cases), the solution is to right-click on the Packages and choose "Manage Nuget Packages...".

Then click on the "Update" button:

Rebuild the solution(also you can try to close and open a solution too) and that's it ))



来源:https://stackoverflow.com/questions/48766856/system-security-permissions-missing-when-invoking-jsonconvert-deserializeobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!