問題描述
我目前正在考慮使用 .NET Core 2.0,以便可以在多個平臺上運行我的應用程序.
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);
在完整的 .NET 中,這將運行并返回我的對象??.但是在 .NET Core 2.0 中出現以下異常
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)
經過一番閱讀后,我在 StackOverflow 上發現了這一點,這表明 .NET Core 中不允許這種操作 https://stackoverflow.com/a/38385774/1211743
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
有人有什么見解嗎?
推薦答案
這是由于對 .NET Core 的工作原理缺乏了解.我打開了 csproj 并添加了對所需文件System.Security.Permissions"的引用并重新加載了項目.至此,nuget 解決了.Json.NET 現在按預期工作.
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.
這篇關于調用 JsonConvert.DeserializeObject<T> 時缺少 System.Security.Permissions在 .NET Core 2.0 中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!