Skip to content

Latest commit

 

History

History
 
 

Actor

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Dapr Actor example

The Actor example shows how to create a virtual actor (DemoActor) and invoke its methods on the client application.

Prerequisites

Projects in sample

  • The interface project (\IDemoActor) contains the interface definition for the actor. The interface defines the actor contract that is shared by the actor implementation and the clients calling the actor. Because client projects may depend on it, it typically makes sense to define it in an assembly that is separate from the actor implementation.

  • The actor service project (\DemoActor) implements ASP.Net Core web service that is going to host the actor. It contains the implementation of the actor. An actor implementation is a class that derives from the base type Actor and implements the interfaces defined in the corresponding interfaces project. An actor class must also implement a constructor that accepts an ActorService instance and an ActorId and passes them to the base Actor class.

  • The actor client project (\ActorClient) contains the implementation of the actor client which calls DemoActor's methods defined in IDemoActor's Interfaces.

Running the example

To run the actor service locally run this command in DemoActor directory:

 dapr run --dapr-http-port 3500 --app-id demo_actor --app-port 5010 dotnet run

The DemoActor service will listen on port 5010 for HTTP.

Make client calls

The ActorClient project shows how to make client calls for actor using Remoting which provides a strongly typed invocation experience. Run the client project from ActorClient directory as:

 dotnet run

Note: If you started the actor service with dapr port other than 3500, then set the environment variable DAPR_HTTP_PORT to the value of --dapr-http-port specified while starting the actor service before running the client in terminal.

On Windows: set DAPR_HTTP_PORT=<port>
On Linux, MacOS: export DAPR_HTTP_PORT=<port>

Invoke Actor method without Remoting over Http

You can invoke Actor methods without remoting (directly over http), if the Actor method accepts at-most one argument. Actor runtime will deserialize the incoming request body from client and use it as method argument to invoke the actor method. When making non-remoting calls Actor method arguments and return types are serialized, deserialized as JSON.

Save Data Following curl call will save data for actor id "abc" (below calls on MacOs, Linux & Windows are exactly the same except for escaping quotes on Windows for curl)

On Linux, MacOS:

curl -X POST http://127.0.0.1:3500/v1.0/actors/DemoActor/abc/method/SaveData -d '{ "PropertyA": "ValueA", "PropertyB": "ValueB" }'

On Windows:

curl -X POST http://127.0.0.1:3500/v1.0/actors/DemoActor/abc/method/SaveData -d "{ \"PropertyA\": \"ValueA\", \"PropertyB\": \"ValueB\" }"

Get Data Following curl call will get data for actor id "abc" (below calls on MacOs, Linux & Windows are exactly the same except for escaping quotes on Windows for curl)

On Linux, MacOS:

curl -X POST http://127.0.0.1:3500/v1.0/actors/DemoActor/abc/method/GetData

On Windows:

curl -X POST http://127.0.0.1:3500/v1.0/actors/DemoActor/abc/method/GetData