json - Azure Function Json.Net 依赖关系问题

标签 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);
}

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

"$type":"Lib.PosLog.SaleBase, Lib.PosLog", 

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)

我检查了 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.csx 中调用 LoadWithPartialName 时它可以工作,但当它由 Json.net 内部调用时却似乎不起作用?我想我错过了一些重要的事情。我也非常确定,在将运行时版本更新到 1 之前,这曾经可以在 Azure 上运行。

谢谢

最佳答案

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

关于json - Azure Function Json.Net 依赖关系问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42692208/

相关文章:

javascript - Json 数据未呈现到 DOM

Azure 批量定价可在一小时内扩大和缩小规模

azure - nservicebus 和 azure sdk 2

python - 从我的本地计算机的 Azure Keyvault 读取值

python - Azure Databricks Jupyter Notebook Python 和 R 在一个单元中

java - Json 到 Java 对象的转换 - Jackson 失败

javascript - 发布后更新 angularJS 中的 $scope 属性

json - PowerShell:如何将哈希表输出为 json 并使用 foreach 循环将其存储在变量中?

javascript - Google 可视化 API 示例中的 “Invalid JSON string”

sql - Azure 备份数据库(sql 转储文件)