Setup
Prerequisite software: Docker
Creating a projects
Create a new console application project in a new folder using dotnet command.
> dotnet new console --name TaskManager.Service
Also, create a Web API project using the same folder for communicating with our Service. Then add both projects to the common solution.
dotnet new webapi --name TaskManager.Api
Then we add the Cronus dependency.
cd TaskManager.Api
dotnet add package Cronus
cd ../TaskManager.Service
dotnet add package Cronus
dotnet add package Cronus.Transport.RabbitMQ
dotnet add package Cronus.Persistence.Cassandra
dotnet add package Cronus.Serialization.NewtonsoftJson
dotnet add package Microsoft.Extensions.Hosting
This is the minimum set of packages for our Cronus host to work.
Run docker images
Setup Cassandra (Container memory is limited to 2GB):
docker run --restart=always -d --name cassandra -p 9042:9042 -p 9160:9160 -p 7199:7199 -p 7001:7001 -p 7000:7000 cassandra
Setup RabbitMq (Container memory is limited to 512MB):
docker run --restart=always -d --hostname node1 -e RABBITMQ_NODENAME=docker-UNIQUENAME-rabbitmq --name rabbitmq -p 15672:15672 -p 5672:5672 elders/rabbitmq:3.8.3
Setup configuration file
Add appsettings.json with the following configuration into the project folder.
//This should be int the Service and in the Api.
{
"Cronus": {
"BoundedContext": "taskmanager",
"Tenants": [ "tenant" ],
"Transport": {
"RabbitMQ": {
"Server": "127.0.0.1",
"VHost": "taskmanager"
},
"PublicRabbitMQ": [
{
"Server": "127.0.0.1",
"VHost": "unicom-public",
"FederatedExchange": {
"UpstreamUri": "guest:guest@localhost:5672",
"VHost": "unicom-public",
"UseSsl": false,
"MaxHops": 1
}
}
]
},
"Persistence": {
"Cassandra": {
"ConnectionString": "Contact Points=127.0.0.1;Port=9042;Default Keyspace=taskmanager_es"
}
},
"Projections": {
"Cassandra": {
"ConnectionString": "Contact Points=127.0.0.1;Port=9042;Default Keyspace=taskmanager_projections"
}
},
"Cluster": {
"Consul": {
"Address": "127.0.0.1"
}
},
"AtomicAction": {
"Redis": {
"ConnectionString": "127.0.0.1:6379"
}
}
}
}
You can also see how the Cronus application can be configured in more detail in Configuration.
This is the code that your Program.cs in TaskManager.Service should contain.
using Cronus11Service;
using Elders.Cronus;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddCronus(hostContext.Configuration);
})
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
options.ValidateScopes = false;
options.ValidateOnBuild = false;
})
.Build();
host.Run();
This is the code that you should add in the Program.cs in TaskManager.Api.
builder.Services.AddCronus(builder.Configuration);
builder.Host.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
options.ValidateScopes = false;
options.ValidateOnBuild = false;
}
);
....
app.UseCronusAspNetCore();
F5