How to call an async service function from an ASP .NET razor *.cshtml master page in Full .NET Framework 4.7.2

 

If you need to call an async C# service method from an ASP .NET razor page, you can use the System.Threading.Tasks.Task.Run method.

Assuming we have a C# service class “MyService.cs” and this service contains an async method called “GetMyData”, then you can call it in a razor page like so:

 

@{

    var myService = new MyService();

var myData = System.Threading.Tasks.Task.Run(() => myService.GetMyData()).GetAwaiter().GetResult();

}