Call Filters in Microsoft Orleans
Transcript
Hi, my name is Stu. If you're new here, this channel is all about me breaking down different technologies into easily digestible chunks. In this video, we're going to take a look at implementing call filters in Microsoft Orleans, building on the previous videos in this series.
So what are call filters? When a grain is called, you have the opportunity to intercept the call and perform some actions on it from either the perspective of the caller or the receiver of the call. Call filters are asynchronous in nature and allow you to modify the arguments on the request context, which we'll see a little bit later on.
We would typically use a call filter to perform one of the following tasks: check for a user's authorization, logging and telemetry, or error handling. In reality, any cross-cutting concern can be applied to either one or more types of grains, or globally as a global call filter. We have two types of call filter available to us: incoming and outgoing.
Let's first take a look at incoming. To start off with, I'm going to add a new folder to our solution called CallFilters. The incoming call filter that we're going to write is going to be one that writes to the console, so I'm going to call it ConsoleWritingCallFilter.
Next, we need to make sure we implement the interface IIncomingGrainCallFilter. This lives in the Orleans namespace. This gives us a single Invoke method which we can implement our custom logic into. For the purposes of this demo, I'm just going to write something out to the console quickly. I'm going to repeat it again but with the word "end".
Next, I'm going to make the method asynchronous and then await the Invoke method which lives on the context. The Invoke method on the context invokes the next thing in the pipeline, ultimately ending up on the method on the grain, unless you don't call the Invoke method. So this means you can stop the call chain at any point in time.
The last thing that we need to do is go to our Program.
cs file and we need to add the grain call filter to our silo. To do that, we call builder.AddIncomingGrainCallFilter passing in the type ConsoleWritingIncomingCallFilter, and just import the namespace.
Now this is what we call a global filter, so it is applied for every single grain. When I hit F5 now and run the application, what you'll see in the logs is every single time the grain is called, we're going to get two messages logged into our console. This is because it's applied across all of the system grains as well as our user grains.
For an incoming grain call filter, it doesn't matter where the source of the call is. It could be living inside of the cluster, but it could equally be outside of the cluster in a different API.
Now that we've taken a look at incoming call filters, let's take a look at outgoing. Outgoing call filters are executed whenever you're going to call a grain. This could be hosted in the same service or it could be in a different service altogether.
Let's take a look at how we can implement a simple one that writes to the console. Again, I'm going to add a new class to our CallFilters area. It's going to be a ConsoleWritingOutgoingCallFilter. Once again, we need to tell Orleans that this is going to be a call filter, so we need to implement the interface IOutgoingGrainCallFilter, which lives in the Orleans namespace. Again, this gives us a single Invoke method, and we're going to repeat the same steps. I'm just going to copy and paste them across. I'm going to change the words "incoming" to "outgoing" just so that we can distinguish the difference.
Once again, we need to go back to our Program.
cs and make sure that this is registered in our silo. On the builder, we need to call AddOutgoingGrainCallFilter and here we can add ConsoleWritingOutgoingCallFilter.
Now every time the grain is going to be called by something in the system, we should see messages in our console. As this is a global filter, we should see roughly the same amount of messages as we did before, albeit with a different message. As you can see, our outgoing grain call filter is working as we expect it to.
That pretty much wraps up what we're doing today for grain call filters in Microsoft Orleans. As with all technical concepts, I encourage you to go through and read the documentation to make sure that you understand the nuances of the implementation.
If you enjoyed this video, consider subscribing to the YouTube channel for more content like this.