Azure功能Json.Net依赖问题

Rob*_*obG 8 json azure

我有一个Azure函数,反序列化主题中的消息:

#r "Lib.PosLog.dll"
#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus"

using System;
using System.Threading.Tasks;
using System.Configuration;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;
using Lib.PosLog;
using System.Reflection;

public static void Run(string message, TraceWriter log)
{
    var settings = new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.Auto
    };

    var transaction =    JsonConvert.DeserializeObject<TransactionDomainSpecific>(message, settings);
}
Run Code Online (Sandbox Code Playgroud)

该消息依赖于自定义DLL Lib.PosLog中的类型:

"$type":"Lib.PosLog.SaleBase, Lib.PosLog", 
Run Code Online (Sandbox Code Playgroud)

DeserializeObject函数失败,出现以下内部异常:

  InnerException: 
   HResult=-2146233088
   Message=Could not load assembly 'Lib.PosLog'.
   Source=Newtonsoft.Json
   StackTrace:
        at Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
        at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
        at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
        at Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType(String assemblyName, String typeName)
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolveTypeName(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, String qualifiedTypeName)
Run Code Online (Sandbox Code Playgroud)

我检查了GetTypeFromTypeNameKey的源代码,它似乎只调用Assembly.LoadWithPartialName("Lib.PosLog"),它返回null.

然后它检查当前AppDomain的类型.

如果我在调用DeserializeObject之前调用Assembly.LoadWithPartialName("Lib.PosLog"),那么它可以正常工作,因为它从AppDomain获取类型,例如:

public static void Run(string message, TraceWriter log)
{
    var settings = new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.Auto
    };

    Assembly.LoadWithPartialName("Lib.PosLog")
    var transaction =    JsonConvert.DeserializeObject<TransactionDomainSpecific>(message, settings);
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是为什么LoadWithPartialName在run.csx中调用它时工作但在Json.net内部调用它时似乎不起作用?我想我错过了一些重要的东西.我也很确定在将运行时版本更新为1之前,这曾经在Azure上运行.

谢谢

Chr*_*ann 0

鉴于这个问题是从 2017 年初(差不多 2 年前)开始的,我认为这是 Azure Functions 的一个错误,而且早已修复。我一直将 JSON.NET 与 Azure Functions 结合使用,无论是 v1 还是 v2,都没有出现问题。:)