json.net - Azure Functions (Durable) - 类型是接口(interface)或抽象类,无法实例化

标签 json.net azure-functions azure-durable-functions

问题 我有一个包含编排和事件函数的 C# Durable Azure Functions 应用程序 (v2)。

编排调用事件如下:

propBag.CE = await context.CallActivityAsync<CEData>(nameof(GetCEByExternalIdActivity), propBag);

调用正常,Activity 函数执行,为 propBag.CE 构建内容并返回。

CE 对象包含一些其他对象,包括:

        public List<IParty> Individuals { get; set; }
        public List<IParty> Organisations { get; set; }

这些属性分配如下:

        consPort.Individuals = await GetParties(clientRel.RelatedIndividuals, CEEntityType.Individual);
        consPort.Organisations = await GetParties(clientRel.RelatedOrganisations, CEEntityType.Organisation);

它包含接口(interface)的事实似乎给 Durable Functions 运行时带来了问题。当它尝试反序列化事件函数返回的值时,出现以下错误:

无法创建 Interfaces.Avaloq.Application.Models.CE.IParty 类型的实例。类型是接口(interface)或抽象类,不能实例化

有人知道解决这个问题的好方法吗?也许有一种方法可以配置该函数将如何尝试反序列化 json?

解决方法 我通过更改类以包含 IParty 的具体类型列表来解决这个问题,如下所示:

        public List<Individual> Individuals { get; set; }
        public List<Organisation> Organisations { get; set; }

然后我必须确保在分配给这些属性之前从 IParty 转换为具体类型,如下所示:

var myList = (await GetParties(clientRel.RelatedIndividuals, CEEntityType.Individual));
        foreach (var party in myList)
        {
            consPort.Individuals.Add((Individual)party);
        }
        myList = (await GetParties(clientRel.RelatedOrganisations, CEEntityType.Organisation));
        foreach (var party in myList)
        {
            consPort.Organisations.Add((Organisation)party);
        }

不是很漂亮,但让我解决了这个问题。

最佳答案

看起来您遇到了 this issue on GitHub .除了解决方法之外,您还可以自定义 Durable Functions 使用的序列化程序设置以包含类型信息。还有一个StackOverflow post它描述了这一点并有一个例子。

关于json.net - Azure Functions (Durable) - 类型是接口(interface)或抽象类,无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62080090/

相关文章:

c# - JSON.Net 在构建时抛出异常

Azure Functions、功能键无法访问并且门户中的文件看起来丢失

azure - 使用 Application Insights 无法看到自定义日志

c# - 持久函数 : How to pass a parameter to the Orchestrator?

c# - 使用自定义格式作为引用时,如何使用 Json.NET 通过引用反序列化对象?

c# - 寻找错误版本的 Newtonsoft.Json.dll 的程序集

c# - Linq-To-JSON 查询以在分层 JSON 结构中查找特定属性的 sibling

azure - 根据区域禁用/启用 Azure 功能

azure - v2 Azure Function with Service Bus 触发器未触发

python - 如何为 Durable Azure Functions 编写单元测试?