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

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();

}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.