Make controller methods asynchronous or leave synchronous?

I am working with an existing C # ASP.NET MVC webapplication, and I am adding some functionality to it that I cannot decide whether to make an asynchronous call.

Currently, the application’s homepage processes the page and the user logs in. Nothing more and nothing happens asynchronously.

I am adding functionality that when visiting the homepage generates a web API call, which subsequently calls a database that captures the identifier and returns it to the HTML tag on the main page. This identifier will not be displayed on the screen, only on the source / HTML representation (this is added for various purposes of internal tracking).

The call to the Web API / database is simple, just take the identifier and return it to the controller. Regardless, I wonder if the application should make this call asynchronously? Website traffic is not huge, but I'm still curious about concurrency, performance, and future scalability.

The only thing I have to do is make the entire asynchronous ActionMethod, and I'm not sure what the consequences of this will be. The main template, currently synchronous, is below:

    public ActionResult Index()
    {
        var result = GetID();

        ViewBag.result = result.Data;

        return View();
    }

    public JsonResult GetID()
    {
        var result = "";
        var url = "http://APIURL/GetID";

        using (WebClient client = new WebClient())
        {
            result = client.DownloadString(url);
        }

        return Json(result, JsonRequestBehavior.AllowGet);

    }

Any thoughts?

+4
source share
3 answers

async IO , . . , . - 100, , .

, -, IO. ( * ) . , 100 10 1 .

, - .

+2

, async -. - , . , 1 thread == 1, , 1000 (), 1000 . , . HTML - , , JS, CSS .. . , AJAX . , 20+ -.

, , ( ), , . , . , ( , -, ..), async , . , , , .

, . , , . , async , , . , , .

Async!= . , - , . - " ?" Async - . async , , . , , . , , - ( , BTW), async . , , , . , -, , -, , , .

, , . . , , , async . , async , , . (, Facebook?), .

+1

?

, ... .;)


, . , .
  • -, / .
  • API , , - , /.

async. :

public async Task<ActionResult> Index()
{
    var result = await GetIdAsync();

    ViewBag.result = result.Data;

    return View();
}

public async Task<JsonResult> GetIdAsync()
{
    var result = "";
    var url = "http://APIURL/GetID";

    using (WebClient client = new WebClient())
    {
        // Note the API change here?
        result = await client.DownloadStringAsync(url);
    }

    return Json(result, JsonRequestBehavior.AllowGet);
}

async await Task<T>. . API client.DownloadStringAsync, .

+1

All Articles