Dapr - actors之构建块(6)
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.Task
Task<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:ScoreActor
Actor
ScoreActor
IScoreActor
接下来,通过从派生类来实现参与者 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.host
ActorHost
ActorHost
Actor
上面代码段中的构造函数采用 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.AddOrUpdateStateAsync
IncrementScoreAsync
AddOrUpdateStateAsync
在上面的代码片段中,对方法的一次调用 StateManager.AddOrUpdateStateAsync
提供了完整的 IncrementScoreAsync
方法实现。 AddOrUpdateStateAsync
方法采用三个参数:
- The key of the state to update.
- The value to write if no score is stored in the state store yet.
- 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
- 要更新的状态的键。
- 如果尚未将评分存储在状态存储中,则为要写入的值。
- 在
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 :Startup
Configure
endpoints.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 挎斗调用应用程序来承载和与执行组件实例进行交互。
- 点赞
- 收藏
- 关注作者
评论(0)