Dapr - actors之构建块(6)

举报
小云悠悠zZ 发表于 2023/01/30 16:35:46 2023/01/30
【摘要】 ActorHost类表示Actor 运行时中的actor 类型的宿主。 需要将此参数传递给基类的构造函数 Actor 。 actor 还支持依赖项注入。 使用 .NET 依赖关系注入容器来解析添加到actor 构造函数的任何其他参数。

Important

The return type of an actor method must be or . Also, actor methods can have at most one argument. Both the return type and the arguments must be serializable.TaskTask<T>System.Text.Json

Actor 方法的返回类型必须为 Task  Task<T> 。 此外,Actor 方法最多只能有一个参数。 返回类型和参数都必须支持 System.Text.Json 序列化。

Next, implement the actor by deriving a class from . The class must also implement the interface:ScoreActorActorScoreActorIScoreActor

接下来,通过从派生类来实现参与者 ScoreActor Actor  ScoreActor类还必须实现 IScoreActor 接口:

C#

public class ScoreActor : Actor, IScoreActor
{
    public ScoreActor(ActorHost host) : base(host)
    {
    }

    // TODO Implement interface methods.
}

The constructor in the snippet above takes a argument of type . The class represents the host for an actor type within the actor runtime. You need to pass this argument to the constructor of the base class. Actors also support dependency injection. Any additional arguments that you add to the actor constructor are resolved using the .NET dependency injection container.hostActorHostActorHostActor

上面代码段中的构造函数采用 host 类型的参数 ActorHost  ActorHost类表示Actor 运行时中的actor 类型的宿主。 需要将此参数传递给基类的构造函数 Actor 。 actor 还支持依赖项注入。 使用 .NET 依赖关系注入容器来解析添加到actor 构造函数的任何其他参数。

Let's now implement the method of the interface:IncrementScoreAsync

现在,让我们实现 IncrementScoreAsync 接口的方法:

C#

public Task<int> IncrementScoreAsync()
{
    return StateManager.AddOrUpdateStateAsync(
        "score",
        1,
        (key, currentScore) => currentScore + 1
    );
}

In the snippet above, a single call to provides the full implementation for the method. The method takes three arguments:StateManager.AddOrUpdateStateAsyncIncrementScoreAsyncAddOrUpdateStateAsync

在上面的代码片段中,对方法的一次调用 StateManager.AddOrUpdateStateAsync 提供了完整的 IncrementScoreAsync 方法实现。 AddOrUpdateStateAsync方法采用三个参数:

  1. The key of the state to update.
  2. The value to write if no score is stored in the state store yet.
  3. A to call if there already is a score stored in the state store. It takes the state key and current score, and returns the updated score to write back to the state store.Func
  1. 要更新的状态的键。
  2. 如果尚未将评分存储在状态存储中,则为要写入的值。
  3.  Func 状态存储中已有分数存储时要调用的。 它将使用状态键和当前评分,并返回更新后的分数以写回到状态存储区。

The implementation reads the current score from the state store and returns it to the client:GetScoreAsync

GetScoreAsync实现读取状态存储中的当前评分,并将其返回给客户端:

C#

public async Task<int> GetScoreAsync()
{
    var scoreValue = await StateManager.TryGetStateAsync<int>("score");
    if (scoreValue.HasValue)
    {
        return scoreValue.Value;
    }

    return 0;
}

To host actors in an ASP.NET Core service, you must add a reference to the Dapr.Actors.AspNetCore package and make some changes to the class. In the following example, the method adds the actor endpoints by calling :StartupConfigureendpoints.MapActorsHandlers

若要在 ASP.NET Core 服务中承载Actor,你必须添加对包的引用 Dapr.Actors.AspNetCore 并对Startup 类进行一些更改 。 在下面的示例中, Configure 方法通过调用来添加执行组件终结点 endpoints.MapActorsHandlers 

C#

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...

    // Actors building block does not support HTTPS redirection.
    //app.UseHttpsRedirection();

    app.UseEndpoints(endpoints =>
    {
        // Add actor endpoints.
        endpoints.MapActorsHandlers();

        endpoints.MapControllers();
    });
}

The actors endpoints are necessary because the Dapr sidecar calls the application to host and interact with actor instances.

actors 终结点是必需的,因为 Dapr 挎斗调用应用程序来承载和与执行组件实例进行交互。

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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