category: [ Just Coding ] tags: [ async ] [ C# ] [ MVC ]
created: 26 Jan 2018 @ 03:42 modified: 26 Jan 2018 @ 03:45
A couple days ago I needed to call a remote web api call in my AuthorizeAttribute sometimes but as mentioned in this (Is it possible to use async/await in MVC 4 AuthorizeAttribute?) StackOverflow question (and other forums) it isn’t supported but is in the newer .net core. Unfortunately the project I needed this on was traditional MVC so I was left still finding a way .
Running asynchronous methods in C# synchronous
For the longest time to achieve running async functionality synchronously I've used an async helper class, not sure this is the exact place I found it but Chris McKee currently hosts a version on GitHub gists but for convenience and in case it goes away I have hosted it on mine as well as you can see below
The code allows you to easily wrap up async code and runs it's properly synchronously. A sample of how to do this is below
Now that we have the utility out the way let's look at what this post is actually solving
Creating your async AuthorizeAttribute
It's worth know that this bit of code magic does work everywhere you need to have async code and isn't specific to auth attribute.
Basically what we need to do is in the standard OnAuthorization override we'll add code like above that will just call an async OnAuthorization method and then dump all our logic in there to keep things cleaner.
That's it, with this code you will have no problem calling external api's when trying to call async code would generally cause deadlocks. Your usage of the attribute will be as you would a normal AuthorizeAttribute.
Some of you might be thinking why is this necessary when you could always just do synchronous api calls in your MVC project, although you aren't wrong in my situation the framework components I was using only supported async so I was forced down this path or re-write the framework component that would probably have taken a lot longer.
Gordon Beeming works at Nologo Studios in the sunny city of Durban, South Africa. He is the Lead for the Data and Services Team and has a strong focus on Developer Efficiencies and R&D. When he's not hacking away at a keyboard in Visual Studio he'll generally be relaxing with his family or hitting the black top getting in some mileage. He is a Visual Studio ALM Ranger and Visual Studio ALM MVP.