![]() ![]() (IEndpointRouteBuilder, string, RequestDelegate)' with interceptor 'Interception. Then we'll get a compile-time exception: error CS9144: Cannot intercept method 'EndpointRouteBuilderExtensions.MapPost MapGet ( "/ping", ( HttpContext context ) => Task. If we try to intercept the following method call with our existing interceptor : app. That's true when you're using interceptors from source generators too.ĭefine the attribute somewhere in your project like this: namespace System. This is what drives the interceptor behaviour, but it's not included in the base class library (BCL) anywhere yet, so you need to define it yourself. Next, we need to define the attribute in our project. We also need to explicitly enable the experimental interceptors feature: net8.0 enable enable preview InterceptorsPreview csproj file to enable C#12 preview features. That creates an app that looks something like this: var builder = WebApplication. Create a new app (it doesn't have to be the new AOT template), for example by running dotnet new web Interceptors are designed to be used with source generators, but they don't need to be, so for simplicity, we're just going to write a "raw" interceptor! In this section I'm going to show a very impractical example of an interceptor. Looking at a simple (impractical) working interceptor example That's all quite abstract, so in the next section I'll show a very simple example of an interceptor, so that you can understand the mechanics of how they work. NET community standup a couple of months ago. There was a great discussion about interceptors on the. ![]() They could even improve existing source generators like the Regex generator or the logging generator instead of having to change your code to use the patterns the generator requires, the generator could automatically improve your existing code! Theoretically, at least. In theory, Interceptors should make it easier to build source generators like the configuration source generator or the minimal API source generator. Interceptors make the replacement explicit. If that sounds familiar, it's because that's exactly what the configuration source generator and existing minimal API source generator are already doing! The difference is that those generators currently rely on method specificity rules to "trick" the compiler into using the source generated method. By using interceptors you could take code which previously wasn't AOT friendly, and replace it with a source-generated version.Ĭustomer's don't need to change their code, the source generator just automatically "upgrades" the method calls to use the source generated versions. Interceptors aren't specifically for AOT, but they are clearly designed with AOT in mind. The main reason is ahead-of-time compilation (AOT) which I've discussed several times in this series (not surprising given it's a key focus of. Why not just call the substitute method directly? When your app is compiled, the compiler automatically "swaps out" the call to the original method with your substitute.Īn obvious question here is why would you want to do that. ![]() Interceptors are an interesting new experimental feature coming in C#12 that allow you to replace (or "intercept") a method call in your application with an alternative method. NET 8 finally ships in November 2023! What are interceptors and why do we need them? ![]() In this post, I look at a C#12 preview feature, Interceptors, show how they work and why they're useful, and discuss how the minimal API source generator from my previous post has been updated to use them!Īs these posts are all using the preview builds, some of the features may change (or be removed) before. In this series I look at some of the new features coming in the.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |