Automating .NET Core Services with PostSharp and Aspect-Oriented Code
Tags: aop, programming, dotnetcore, patterns
Published: 2019-08-14 14:02:38

---
title: Automating .NET Core Services with PostSharp and Aspect-Oriented Code
published: true
tags: aop,programming, dotnet-core,patterns
canonical_url: https://medium.com/dealeron-dev/automating-net-core-services-with-postsharp-and-aspect-oriented-code-a8a51d8d84ec
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xex0g4140esxnp2nfxho.png
---

If you’re writing software targeting .NET then there’s a good chance you’re using Object-Oriented principles to do your work. In the process you may run into design patterns which you find yourself often repeating. Some of these patterns start as an abstract need such as “I want to log what my code is doing”. In practice this is easy to solve but as your solutions grow in scope you may find yourself spending a significant amount of time just deciding where and how to log your program’s state. Eventually you may want to change the logging pattern but that will require a significant amount of changes to the source.

While many people have solved this in an Object-Oriented way, these cross-cutting concerns will eventually add up to significant time sinks for any development team. They also present an architectural challenge later down the line when you need to make a significant change. You will have to walk back through all of that boilerplate code at some point when you want to add new log sinks, monitors, or internal log exception handlers. This problem comes up again on many common concerns such as security, caching, threading, rate limiting, and even alerting a GUI that some viewable object has been updated.

Aspect-Oriented designs are focused on targeting those concerns by dealing with the problem in one place and then applying that solution everywhere in your code where it is needed. What I am trying to show here is a way to purposefully hide away those concerns after investing the time to fully model them out. By spending some extra time considering our approach to logging or threading then we can write Aspects which are more like qualities that can be assigned to areas of code. Once you write a [Log] attribute, then you can decide a class or a method will be logged simply by tagging it with the attribute. Later on the compiler does the rest of the work by going back over and adding the missing code where those Aspects are declared.

As a quick note before I get into the main example, Microsoft released [.NET Core 3 Preview 1](https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/) last month and with it comes support for native Windows desktop namespaces. While this article deals with .NET Core 2.1, you can recompile it at any time under netcoreapp3.0 as a project target and it will support a wider surface of types, including UWP libraries, without any extra configuration.

### Remarks

This article is an attempt to cover certain difficult topics which vary in design from one business to the next. The code I will reference is written to be as short as possible, not fast or efficient. This is in the interest of showcasing each of the features and creating a usable demonstration.

The goal here is to explore the cross-platform capabilities of the .NET Generic Host when combined with the power of a pattern-aware compiler. By wrapping the platform and runtime concerns around the generic host it becomes possible to seamlessly link almost any part of the .NET library for any platform. This doesn’t mean you can run a UWP library on Linux, but you won’t have to rewrite any code if you want to run that library when it detects Windows 10 as the OS.

[PostSharp](https://www.postsharp.net/) is a pattern-aware compiler extension for Visual Studio and will be featured heavily throughout the article. If you don’t have a license and want to follow along with the code then there is still a solution. The free [Essentials](https://www.postsharp.net/essentials) version of this framework covers up to ten classes which is enough to implement the full program in this article. You will also need to [download the extension](https://marketplace.visualstudio.com/items?itemName=PostSharpTechnologies.PostSharp) for Visual Studio 2017.

### Hosting Services

I spent the past couple months getting a handle on the new [.NET Core Generic Host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2) which is familiar to anyone who has worked in ASP.NET Core web applications. The host can also be used in console applications as well to handle built-in app configuration, dependency injection, and logging. Let’s take a moment to see how the service hosting fits into this design.

The goal is to create a small application which can:

- Host common services and load native classes based on OSVersion.
- Automatically wrap logging around the entire solution.
- Automatically thread the services for a multi-core computer.
- Validate architecture so that bugs throw exceptions in a deterministic way.

Now because I have spent a lot of time working on ETL solutions I tend to map out the flow of my program and look at what my goals are from a high level. Below I’ve drawn out just some basic shapes to illustrate what the structure is and try to see where the boundaries are going to be. These boundaries are important because they represent areas where concerns cross over and cut into the concerns of individual objects. When using AOP in a project I always find it helps to clearly define where those boundaries are because the goal is to hide how they work. You’ll have to excuse my MS Paint aesthetic here:

![](https://cdn-images-1.medium.com/max/1024/1*VBWR2Bo6CGvt6AYYHS6CaA.png)<figcaption>Immutable (Frozen) Runtime and Platform areas which host Services wrapped in an Actor Threading Model.</figcaption>

**Program:** A console app with `Main()` in this example.

**Runtime:** This will be the top level for all of the PostSharp wrappers and the constructor for the rest of the program. This should launch the service host.

**Platform:** This contains any methods needed to load native classes and provide a thread-safe reference to the Host in case any inter-service communication needs to be implemented.

**Frozen:** Both of the previous layers will be frozen which provides some of the same guarantees as an immutable object. This will be covered later on.

**Hosting:** The .NET Generic Host builder.

**Services:** The services will be wrapped with an Actor model ensuring that they can run on a multi-core computer.

### Log Output

Throughout this exercise you will see several examples of log output which describes the state of the program in a specific template. I’ve marked some of them here as an example of what the values represent.

![](https://thepracticaldev.s3.amazonaws.com/i/68iweed02si31faunxq5.png)<figcaption>Console log output generated automatically by PostSharp</figcaption>

### Program

First, let’s start with a new .NET Core 2.1 console app.

{% gist https://gist.github.com/AlexanderJohnston/803a8088cc8b7928926d3c1b7494edf3 %}

Next we need to add PostSharp to the project. If you have [the extension for Visual Studio](https://marketplace.visualstudio.com/items?itemName=PostSharpTechnologies.PostSharp) then you can right click on the project and add PostSharp to it through the menu. You can also use the Package Manager Console to run Install-Package PostSharp  
You will also need to add the following NuGet packages:

- PostSharp.Patterns.Common The bulk of the code contracts and aspects.
- PostSharp.Patterns.Diagnostics Tracing, logging, and diagnostics.
- PostSharp.Patterns.Diagnostics.Serilog Automatically inject Serilog.
- PostSharp.Patterns.Threading Threading models, dispatching, and deadlock detection.
- Serilog Logging framework.
- Serilog.Sinks.Console and Serilog.Enrichers.Thread for the sink.

Now that we have our packages, let’s drop down one layer away from Program for now. I suggest a sealed runtime layer with static properties and an isolated, non-static entry-point. This allows the runtime to be instantiated as an object with its own threading model which can then launch onto a platform compatible with the caller’s context. The static properties are isolated away from the entry point and can then be used to attach async monitors or metrics later on.

{% gist https://gist.github.com/AlexanderJohnston/1f66a99f0fb863ebd9f70fa714b62bfb %}

Now that we have a basic structure, we can talk about threading models. PostSharp provides three major features in [PostSharp.Patterns.Threading](https://doc.postsharp.net/threading-models).

- **Threading Models** : A threading model is a design pattern that gives [guarantees that your code executes safely](https://doc.postsharp.net/threading-model-compatibility) on a multi-core computer.
- **Thread Dispatching** : Custom attributes [DispatchedAttribute](https://doc.postsharp.net/p_postsharp_patterns_threading_dispatchedattribute_fireandforget) and [BackgroundAttribute](https://doc.postsharp.net/t_postsharp_patterns_threading_backgroundattribute) cause the execution of a method to be dispatched to the UI thread or to a background thread, respectively.
- **Deadlock Detection** : Detects [deadlocks at run time](https://doc.postsharp.net/deadlock-detection) and throws an exception instead of allowing your application to freeze.

Threading models are named solutions to recurring problems and can be considered a design pattern which we will inject at compile time. Defects are discovered by validating the code both during build and while running. If the application is improperly threaded then it will fail in a deterministic way inside of these models. Without this assurance, race conditions or deadlocks tend to show up randomly and can corrupt data without any warning.

The primary threading model we will be looking at is the [Actor model](https://doc.postsharp.net/actor). Actors are services which run with their own state and a mailbox to receive messages in synchronous order. Their execution is more complex than this image describes, but they can be thought of as independent objects which run asynchronously against a queue of messages.

![](https://thepracticaldev.s3.amazonaws.com/i/lar9a3vu4i0klelr237a.png)<figcaption>Actors do not map one-to-one with threads but it is helpful for illustrating execution.</figcaption>

The second threading model being used is the [Freezable](https://doc.postsharp.net/freezable) threading model. An immutable object can be safely accessed from multiple threads but the restrictions of immutability often makes configuration difficult. By using [Freezable](https://doc.postsharp.net/freezable) objects we can define at what point in time the object becomes immutable while still benefiting from mutability during creation.

{% gist https://gist.github.com/AlexanderJohnston/17d86118e780a3d34cd471b1ad204e6e %}

Now let’s wrap the [Runtime](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Runtime.cs) class with a frozen model.

To freeze an object we need to decorate it with an aspect called [[Freezable]](https://doc.postsharp.net/freezable). To enforce most threading models, PostSharp relies on the [[Aggregatable]](https://doc.postsharp.net/aggregatable) attribute; this [specifies that a class has its Parent/Child](https://doc.postsharp.net/aggregatable-adding) relationship explicitly defined. Any properties which are not children should be marked as a [[Reference]](https://doc.postsharp.net/t_postsharp_patterns_model_referenceattribute). Any methods which return the same value for each input and make no observable state changes can be marked as [[Pure]](https://doc.postsharp.net/t_postsharp_patterns_model_pureattribute) to inform PostSharp that they are safe in a threaded context. I opted to include [NotNull] to indicate which properties are available from the entry point.

{% gist https://gist.github.com/AlexanderJohnston/9140ad3db9328113d6c1928fc4e5d849 %}

Now that the [Runtime](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Runtime.cs) class has been updated we can move on to the [Platform](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GenericPlatform.cs). While developing for a cross-platform application on .NET Core, it’s helpful to have a layer where concerns of the platform can be safely handled. Let’s take a look at some functions you might see here:

- GenericHost Creating the [.NET generic host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2).
- GetNativeClass Services may need to load platform-specific implementations and this will provide the correct type.
- Crash If we are going to inject threading models safely then services also need a way to crash if state becomes fatal.

You will see the following patterns on the [Platform](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GenericPlatform.cs) code:

- [[Required]](https://doc.postsharp.net/t_postsharp_patterns_contracts_requiredattribute) Throws if null is passed in so that the responsibility belongs to the caller.
- INativeClass Serves as an interface to load and terminate native classes after importing them.
- `.Crash()` Allows native platform classes to call for immediate debugging or termination if a fatal state is reached. It is marked with [[ContractAnnotation (“=\> halt”)](https://www.jetbrains.com/help/resharper/Contract_Annotations.html)] which specifies that for any given input the output will be a halted state. This allows the IDE to emit warnings appropriately.
- GenericHost Will be decorated with [[Reference]](https://doc.postsharp.net/t_postsharp_patterns_model_referenceattribute) instead of [[Child]](https://doc.postsharp.net/aggregatable-adding) which doesn’t guarantee thread safety anymore at the boundary but also doesn’t force the host to implement a compatible model.

{% gist https://gist.github.com/AlexanderJohnston/d9b69ad328abaecec90077e0053baddc %}

Now we have a [Runtime](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Runtime.cs) with a thread safe entry point and a Platform which can load native classes. At this point we can go ahead and use the .NET Generic Host, specifically [HostBuiler](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2) to configure services and host them.

Several packages are helpful for using the [HostBuilder](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2):

- [Microsoft.Extensions.Configuration](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2)
- [Microsoft.Extensions.Configuration.CommandLine](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2)
- [Microsoft.Extensions.Configuration.EnvironmentVariables](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2)
- [Microsoft.Extensions.Configuration.Json](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2)
- [Microsoft.Extensions.DependencyInjection](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2)
- [Microsoft.Extensions.Hosting](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/?view=aspnetcore-2.2)
- [Microsoft.Extensions.Options.ConfigurationExtensions](https://github.com/aspnet/Extensions/tree/9bc79b2f25a3724376d7af19617c33749a30ea3a)

{% gist https://gist.github.com/AlexanderJohnston/3723edc8be57e57fcc05707faa83b6aa %}

{% gist https://gist.github.com/AlexanderJohnston/49f4cc02ab5f6419b319dffaef830bf4 %}

The `.Start()` method is decorated with a [[SingleEntryMethod]](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Aspects/SingleEntryMethodAttribute.cs) attribute which automatically intercepts the method and returns null if it has been called more than once. This permits us to leave a reference to the Generic Host inside of the Platform without worrying about this method being called later on. Because this class is being intercepted, we can decorate it with the [[ExplicitlySynchronized]](https://doc.postsharp.net/threading-waiving-verification) aspect which informs PostSharp not to verify the safety of this class.

[[SingleEntryMethod]](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Aspects/SingleEntryMethodAttribute.cs) is a custom aspect written with PostSharp. To create one of these we only need to [decorate a class](https://doc.postsharp.net/aspect-serialization) with [[PSerializable]](https://doc.postsharp.net/aspect-serialization) and then inherit from any of the [PostSharp aspects](https://doc.postsharp.net/n_postsharp_aspects). In this case we want to intercept calls at the boundary of the decorated method which is a feature provided by [MethodInterceptionAspect](https://doc.postsharp.net/method-interception).

{% gist https://gist.github.com/AlexanderJohnston/5568276a6055df1cc115291c7e070dd3 %}

As you can see in the Generic Host, one service was registered called [TimingService](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/TimingService.cs). Let’s set that class up now. This will use the [[Actor]](https://doc.postsharp.net/actor) threading model with a few new attributes:

- [[Actor]](https://doc.postsharp.net/actor) Generates code which prevents fields of an actor from being accessed from an invalid context. Requires [[Aggregatable]](https://doc.postsharp.net/aggregatable-adding) parent/child decoration as well as the following two aspects.
- [[Reentrant]](https://doc.postsharp.net/actor) This attribute declares that an async method can be safely re-entered on each await statement. For the actor model, this means other methods can be invoked while waiting. This must be applied to all async actor methods.
- [[ExplicitlySynchronized]](https://doc.postsharp.net/threading-waiving-verification) Opts out of the threading model by declaring that the object is handling its own safety.
- [[EntryPoint]](https://doc.postsharp.net/t_postsharp_patterns_threading_entrypointattribute) This specifies that the method can be invoked from threads which do not currently have access to the object. This means an event handler, background task, or callback can safely enter the threading model. Only needs to be applied to private methods.

The [TimingService](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/TimingService.cs) class also inherits from [IHostedService](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2) which integrates with the .NET Generic Host [which is covered by this MSDN article](https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/background-tasks-with-ihostedservice). Each of these services will have `StartAsync()` called in the order they were registered during configuration earlier in the [GenericHost](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GenericHost.cs) class. The queue reverses this order when `StopAsync()` is called, starting with the last service registered and working its way back through the queue.

{% gist https://gist.github.com/AlexanderJohnston/baa9da88b1688a2d9373ebb3890f8df0 %}

In the interest of keeping this example small we’re going to cheat here and just use a few Timer objects to run on threads and report back. This should illustrate that our class is appropriately using the threading model. These aren’t technically native namespaces either, but calling out to platform-specific code will work exactly the same way without any configuration.

With that in mind we need to implement the [NativeTimers](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Win32NT/NativeTimers.cs) class which is imported from the [Platform](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GenericPlatform.cs) into [TimingService](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/TimingService.cs) after `StartAsync()` is called.

[NativeTimers](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Win32NT/NativeTimers.cs) will need to be [placed under a namespace compatible](https://docs.microsoft.com/en-us/dotnet/api/system.platformid?view=netframework-4.7.2) with the platform this code is running on. In my case I am running on Windows 10 17134 which will return [“Win32NT”](https://docs.microsoft.com/en-us/dotnet/api/system.platformid?view=netframework-4.7.2) as the Platform. A list of potential platforms is [provided by Microsoft](https://docs.microsoft.com/en-us/dotnet/api/system.platformid?view=netcore-2.2).

- MacOSX
- Unix
- Win32NT

{% gist https://gist.github.com/AlexanderJohnston/fb385707af5398b42ea36c764be65876 %}

At this point we have created the following areas of code:

- [Runtime](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Runtime.cs) Entered from Program, configures logging and then sets up the platform.
- [Platform](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GenericPlatform.cs) Contains methods for loading native types and starts the generic host.
- [Generic Host](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GenericHost.cs) .NET Core Generic Host which configures and creates services.
- [TimingService](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/TimingService.cs) An example service being threaded with the Actor model.
- [NativeTimers](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/Win32NT/NativeTimers.cs) An example being loaded natively by the Timing Service.

Now we just need to inject the logging wrapper and freeze our layers, starting with the `.Entry()` function in Runtime.

You will see the following patterns here:

- [LoggingServices.DefaultBackend](https://doc.postsharp.net/logging) This is the PostSharp logging back end. By setting our logger here we can then inject it later on.
- `Post.Cast<T, IFreezable>( object ).Freeze()` This is the call to freeze an object and mark it as immutable.

{% gist https://gist.github.com/AlexanderJohnston/7b0c2c855895885ebae46c378bba4dd2 %}

The `VerboseLogger()` implementation and [BlueConsole](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/ConsoleExtensions.cs) theme:

{% gist https://gist.github.com/AlexanderJohnston/b05224085fad73268cf4ac756609f80a %}

{% gist https://gist.github.com/AlexanderJohnston/fac3972eda029e0d4acddb1481918d60 %}

Last we just need to call the Runtime from the program and inject logging into the assembly.

Create a new [GlobalAspects.cs](https://github.com/AlexanderJohnston/SharpCrafting/blob/master/SharpCrafting/GlobalAspects.cs) class without any using or namespace declarations. Include these lines:

{% gist https://gist.github.com/AlexanderJohnston/5d9a86036316301a089b784bb187b03c %}

This will wrap all Public, Private, Internal, and Protected members with the [[Log]](https://doc.postsharp.net/t_postsharp_patterns_diagnostics_logattribute) attribute from the assembly level.

Now just call the runtime from Program and watch it run.

{% gist https://gist.github.com/AlexanderJohnston/a3c20bddd4185efc9cccc94bb6a4d71a %}

If everything is set up correctly then we should be able to watch the entire flow of execution print out to the console.

We expect to see something like this:

- Generic Host starts up and creates the services.

![](https://thepracticaldev.s3.amazonaws.com/i/aqky9g0b692s1v5pghnv.png)

- Services access AppConfig which creates the Platform.

![](https://thepracticaldev.s3.amazonaws.com/i/dtzxdbxvg85wu2wwo0wz.png)

- Services start asynchronously and ask for native types from the Platform.

![](https://thepracticaldev.s3.amazonaws.com/i/kypu7x21o8sjjttv847m.png)

- Native Timers class is constructed and three timers are set.

![](https://thepracticaldev.s3.amazonaws.com/i/hld2bqxmxkr0taxfus98.png)

- Application starts.

![](https://thepracticaldev.s3.amazonaws.com/i/uh8ijrjcrbic3d2qasyy.png)

- Timers callback to the private 1[EntryPoint]1 on 1WriteTime()1.

![](https://thepracticaldev.s3.amazonaws.com/i/2uvavmftki50amuis82y.png)

If you’ve been following along up to this point then you now can launch native classes onto services hosted cross-platform with the logging and threading handled.

### Where can we go from here?

- Calling any libraries or namespaces from the services will automatically pipe the log statements if those libraries also implement Serilog, whether or not they have a custom log handler. This can be made compatible with other loggers as well.
- State machines will automatically be maintained by PostSharp so that asynchronous code is threaded safely. You can see these decorated in the logs above. The services are able to call into asynchronous code from other frameworks as well; this means a UWP .NET Framework 4.7.1 class library can be called from .NET Standard 2.0 library referenced by a service running under .NET Core 3.0. This so far has worked without any needed configuration for me other than to compile the console application under `<TargetFramework>netcoreapp3.0</TargetFramework>`.
- Asynchronous log monitors can be attached to Serilog with the ability to call back into Runtime or Platform as needed. These can be used to monitor flow, handle dropped log events, and handle deterministic internal logger exceptions.
- An Event-Sourcing timeline, Inter-Process Communication Channel, or API can all be attached to the service actor model. Each actor can write events back to a central source asynchronously while guaranteeing the messages are threaded properly.

### Final Remarks

I will attach [the source code](https://github.com/AlexanderJohnston/SharpCrafting) for this entire project below if you’d like to clone it and play around with the solution. Feel free to modify or redistribute it as needed.

Hopefully this article was able to show you the power of Aspect Oriented Programming when combined with the new .NET Core architecture to provide highly extensible service hosting on many platforms.

If you ever have questions related to PostSharp or .NET Core then feel free to comment or get in touch with me any time.

{% github https://github.com/AlexanderJohnston/SharpCrafting %}

* * *