Azure Functions(Durable)-类型是接口或抽象类,无法实例化 [英] Azure Functions (Durable) - Type is an interface or abstract class and cannot be instantiated

查看:73
本文介绍了Azure Functions(Durable)-类型是接口或抽象类,无法实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 我有一个包含编排和活动功能的C#耐用Azure函数应用(v2).

Problem I have a C# Durable Azure Functions app (v2) containing an orchestration and activity function.

业务流程按如下方式调用活动:

The orchestration calls the activity as follows:

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

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

The call works ok and the Activity function executes, builds up the content for propBag.CE and returns.

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

The CE object contains a few other objects, including:

        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);

它包含接口的事实似乎给持久功能"运行时带来了问题.当它尝试反序列化活动函数返回的值时,出现以下错误:

The fact that it contains an Interface seems to give the Durable Functions runtime a problem. When it attempts to deserialize the value returned from the activity function, I get the following error:

无法创建类型为Interfaces.Avaloq.Application.Models.CE.IParty的实例.类型是接口或抽象类,无法实例化

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

Does anyone know of a good fix for this? Maybe there's a way to configure how the function will attempt to deserialize the json?

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

Workaround I worked-around the problem by changing the class to contain lists of the concrete types of IParty as follows:

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

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

I then had to ensure that I cast to the concrete type from the IParty before assigning to those properties, as follows:

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

不好看,但是可以解决我的问题.

Not pretty but gets me around the problem.

推荐答案

您似乎正在遇到 StackOverflow帖子对此进行了描述,并有一个示例.

It looks like you're running into this issue on GitHub. In addition to your workaround, you can customize the serializer settings used by Durable Functions to include type information. There is another StackOverflow post which describes this and has an example.

这篇关于Azure Functions(Durable)-类型是接口或抽象类,无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆