Dapr - actors之构建块(8)

举报
小云悠悠zZ 发表于 2023/01/30 16:38:20 2023/01/30
【摘要】 默认情况下,Dapr actors 不可重入。 这意味着不能在同一链中多次调用 Dapr actor。 例如, Actor A -> Actor B -> Actor A 不允许使用的调用链。 撰写本文时,有一个预览功能可用于支持重入。 但是,尚无 SDK 支持。 

下面的示例指定了用于actor  JsonSerializerOptions 状态持久性和消息反序列化的自定义:

C#

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddActors(options =>
    {
        var jsonSerializerOptions = new JsonSerializerOptions()
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            PropertyNameCaseInsensitive = true
        };

        options.JsonSerializerOptions = jsonSerializerOptions;
        options.Actors.RegisterActor<ScoreActor>();
    });
}

The call to registers the for .NET dependency injection. This allows ASP.NET Core to inject an instance into your controller classes. The following example calls an actor method from an ASP.NET Core controller class:AddActorsIActorProxyFactoryIActorProxyFactory

 AddActors 注册 IActorProxyFactory .net 依赖项注入而调用的。 这允许 ASP.NET Core 将实例注入 IActorProxyFactory 控制器类。 下面的示例从 ASP.NET Core 控制器类调用执行组件方法:

C#

[ApiController]
[Route("[controller]")]
public class ScoreController : ControllerBase
{
    private readonly IActorProxyFactory _actorProxyFactory;

    public ScoreController(IActorProxyFactory actorProxyFactory)
    {
        _actorProxyFactory = actorProxyFactory;
    }

    [HttpPut("{scoreId}")]
    public Task<int> IncrementAsync(string scoreId)
    {
        var scoreActor = _actorProxyFactory.CreateActorProxy<IScoreActor>(
            new ActorId(scoreId),
            "ScoreActor");

        return scoreActor.IncrementScoreAsync();
    }
}

Actors can also call other actors directly. The base class exposes an class through the property. To create an actor proxy from within an actor, use the property of the base class. The following example shows an that invokes operations on two other actors:ActorIActorProxyFactoryProxyFactoryProxyFactoryActorOrderActor

Actors还可以直接调用其他Actors。 Actor基类 IActorProxyFactory 通过属性公开类 ProxyFactory 。 若要从Actor 中创建执行组件代理,请使用 ProxyFactory 基类的属性 Actor 。 下面的示例演示一个 OrderActor ,它对两个其他Actor 调用操作:

C#Copy

public class OrderActor : Actor, IOrderActor
{
    public OrderActor(ActorHost host) : base(host)
    {
    }

    public async Task ProcessOrderAsync(Order order)
    {
        var stockActor = ProxyFactory.CreateActorProxy<IStockActor>(
            new ActorId(order.OrderNumber),
            "StockActor");

        await stockActor.ReserveStockAsync(order.OrderLines);

        var paymentActor = ProxyFactory.CreateActorProxy<IPaymentActor>(
            new ActorId(order.OrderNumber),
            "PaymentActor");

        await paymentActor.ProcessPaymentAsync(order.PaymentDetails);
    }
}
PLAINTEXT 复制 全屏

Note

备注

By default, Dapr actors aren't reentrant. This means that a Dapr actor cannot be called more than once in the same chain. For example, the call chain is not allowed. At the time of writing, there's a preview feature available to support reentrancy. However, there is no SDK support yet. For more details, see the official documentation.Actor A -> Actor B -> Actor A

默认情况下,Dapr actors 不可重入。 这意味着不能在同一链中多次调用 Dapr actor。 例如, Actor A -> Actor B -> Actor A 不允许使用的调用链。 撰写本文时,有一个预览功能可用于支持重入。 但是,尚无 SDK 支持。 

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。