Arch is built on the ASP.NET platform. To get started developing with Arch, first add the Application Api to your program.
The initial calls that set up the Application Api also set up the entire Arch framework.
NuGet Setup
Add NuGet package
TriunniaLabs.Arch.Microservice to your project. This brings in all the necessary framework code and the Application Api clients.
Note that we use our own
NuGet package feed, which you must add as a package source in your environment.
Host Setup
The very first thing is to make a call to
UseTriunniaLabsMicroservice() on the
Host Builder.
This sets up the logging infrastructure and other metadata.
builder.Host.UseTriunniaLabsMicroservice(EnvironmentType.Development);
Service Setup
In the main
Service Composition section of your program startup, make the call to
AddTriunniaLabsMicroservice() to add the Application Api.
This registers the Application Api clients, the microservice context, encryption algorithms, MessagePack formatters, and http services, caching, and resilience policies.
It is required by all microservices participating in the application for it is the main call that makes the framework available to the microservice.
builder.Services.AddTriunniaLabsMicroservice(builder.Configuration);
App Setup
After service composition, when building the application itself, add another call to
UseTriunniaLabsMicroservice() on the
Application Builder.
This performs further setup functions, such enabling the ArchDependencyMiddleware, making registered MessagePack formatters available on the StaticCompositeResolver, and activating AES-GCM encryption with the Bootstrap Key if that feature is enabled.
app.UseTriunniaLabsMicroservice();
The following code can be helpful for development environments:
if (builder.Environment.IsDevelopment())
builder.Services.AddSingleton<DummyIpAddressMiddleware>();
And:
if (app.Environment.IsDevelopment())
app.UseMiddleware<DummyIpAddressMiddleware>();
Authentication Setup
Back in the
Service Composition section, set up the Application Api Authentication Scheme as shown below.
This authorization is required for communication between microservices within the application.
builder.services.AddAuthentication(options =>
options.DefaultScheme = ArchConstants.ApplicationAuthenticationScheme)
.AddApplicationApiScheme();
Diagnostics Setup
Register an
IDiagnosticConfigurationProvider and
implement it so that you can diagnose any configuration problems.
builder.Services.AddTransient<IDiagnosticConfigurationProvider, DiagnosicConfigurationProvider>();
Formatter Setup
Any data type that would transit within Arch's protected envelope must be serializable by MessagePack.
A MessagePackFormatter for all such types must exist in StaticCompositeResolver.
Arch provides convenience methods for this registration in the form of:
services.AddStandardMessagePackFormatter<T>();
And:
services.AddStandardMessagePackFormatters(new[] { typeof(T1), typeof(T2), ... });
An untrusted byte array converter might also need to be added. Example:
builder.Services.AddKeyedSingleton<IByteArrayConverter<MyApp.MyType>,
MessagePackUntrustedByteArrayConverter<MyApp.MyType>>
("MessagePackUntrustedByteArrayConverter<MyApp.MyType>");
See:
Program.cs
Conclusion
At this point, your application is set up as an Arch application.
Any incoming requests will be validated by the Application Api automatically and Arch resources are available to your application via the
IMicroserviceContext and any of the clients.
Writing a Controller
When writing a controller, inherit from
ArchControllerBase.
This makes the MicroserviceContext easily available and provides necessary support functions for interacting with protected envelopes.