简体   繁体   中英

How to make parallel database calls and bind to model ASP.NET Core MVC

I'm using ASP.NET Core MVC 2.0 and I've got an API that calls a method which opens a DB connection and inserts values into a model.

I'm hitting the same method 7 times and I'm just passing different information into it the parameters to build out my model. I HAVE to hit it seven different times, no rewrite of code will prevent this.

Instead of creating a loop and calling that method I wish to make parallel calls to the db so the response is faster. I've found multiple articles explaining how to do this eg How to properly make asynchronous / parallel database calls but there's enough difference that I can't seem to get it to work.

Following is the my method hitting the DB:

    public async Task<RS_Model> Get_Results(RequestS_Model values)
    {
       //Deleted a bunch of code to keep it simple
        string strQS = @"EXEC Get param1,param2,etc";

        using (SqlConnection conSQL = new SqlConnection(connectionString))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strQS, conSQL))
            {
                conSQL.Open();

                using (SqlDataReader dtrSQL = cmdSQL.ExecuteReader())
                {
                    while (dtrSQL.Read())
                    {
                        Double.TryParse(dtrSQL["Value1"].ToString(), out dblVal1);

                    } //Ends While
                } //end SQLDataReader
            } //Ends cmdSQL
        } //ends using

        results.Price = dblVal1;

        return  results;
    } //Ends Get Results

My IActionResult for the api is:

    [HttpGet]
    public async Task<IActionResult> Get([FromQuery] RequestS_Model values)
    {
        SV_Results Results = new SV_Results();

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        RS_Model model = new RS_Model();

        model.dblr[0] = await  Results.Get_Results(values);
        values.Parm1 = 2;
        model.dblr[1] = await    Results.Get_Results(values);
        values.Parm1 = 3;
        model.dblr[2] = await    Results.Get_Results(values);
        values.Parm1 = 4;
        model.dblr[3] = await    Results.Get_Results(values);
        values.Parm1 = 5;
        model.dblr[4] = await    Results.Get_Results(values);
        values.Parm1 = 6;
        model.dblr[5] = await    Results.Get_Results(values);
        values.Parm1 = 7;
        model.dblr[6] = await    Results.Get_Results(values);

        //int[] results = await Task.WhenAll(new Task<int>[] { task1, task2 });


        return new OkObjectResult(model);
    } //IActionResults

I know that I've forced them into a synchronous call be doing what I am, but I can't seem to make the 7 calls asynchronous and then wait for them to be all done before I build my final model. The final response needs to be in Json, but I haven't even gotten that far yet.

Instead of this:

 model.dblr[0] = await Results.Get_Results(values);
 model.dblr[1] = await Results.Get_Results(values);
 model.dblr[2] = await Results.Get_Results(values);
 model.dblr[3] = await Results.Get_Results(values);
 model.dblr[4] = await Results.Get_Results(values);
 model.dblr[5] = await Results.Get_Results(values);
 model.dblr[6] = await Results.Get_Results(values);

Create a list of tasks and await them as a group:

var tasks = Enumerable.Range(0,7).Select( i => Results.Get_Results(values) ).ToList();
await Task.WhenAll(tasks);
for (int i=0; i<7; i++) model.dblr[i] = tasks[i].Result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM