Azure Functions在Json.net和类型名称处理中效果不佳

时间:2018-06-28 14:58:21

标签: c# json azure json.net azure-functions

我已经花了10多个小时来研究这个问题,现在我觉得这是一个错误。

这是设置:

我有一个功能应用程序MyFunctionApp.csproj,该应用程序引用了我需要用于序列化/反序列化消息的项目MyDataContracts.csproj。  目前,我正在使用Json.net进行序列化/反序列化。

即使可以证明已加载MyDataContracts,我也可以证明尝试将JSON反序列化为MyDataContracts.dll中的类型时存在问题:

// MyClass is from MyDataContracts    
MyClass instance = new MyClass(); // good so far … interesting ...

Type type = instance.GetType();

Assembly assembly = type.Assembly;

log.Info($"Type {type} comes from assembly '{assembly.FullName}', location = '{assembly.Location}'");
// 2018-06-28T14:39:40.630 [Info] Instance of MyDataContracts.MyClass comes from assembly MyDataContracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null', location = 'D:\local\Temporary ASP.NET Files\root\48e7c30a\5fd0f557\assembly\dl3\9681789a\c5d216c9_9f0ed401\MyDataContracts.dll'

string json = JsonConvert.SerializeObject(type, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Auto,
    ypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});


log.Info($"Successfully serialized the {type.GetType()}");
// Successfully serialized the System.RuntimeType

log.Info(json);
// "MyDataContracts.MyClass, MyDataContracts, Version=10.0.0, Culture=neutral, PublicKeyToken=null"

// BOOM! This won't work.
type = JsonConvert.DeserializeObject<Type>(json, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Auto,
    TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});

// 2018-06-28T14:39:40.689 [Info] Newtonsoft.Json.JsonSerializationException

// 2018-06-28T14:39:40.689 [Info] Error converting value "MyDataContracts.MyClass, MyDataContracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" to type 'System.Type'. Path '', line 1, position 102.

0 个答案:

没有答案