none
c# .net core razor page timing for Bing rest service async call

    Question

  • I have a razor page where in the 'OnGetAsync' method it is making a async Bing Rest Service call to find the longitude/latitude for a given address string. The service is working and it is returning the longitude/latitude, but too late because the page is already drawn. I've tried to use the 'await' keyword in a few places, but that's just guessing and it doesn't compile. How do I either repage the cshtml or await the async bing service call? Or is the problem more structural than that? Here is the .cshtml.cs file...

    using System;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Options;
    using Northwind.ModelsDB;
    using System.Runtime.Serialization.Json;
    using BingMapsRESTToolkit;
    using System.Net;
    namespace Northwind.Pages.CustomerPages
    {
       public class DetailsModel : PageModel
       {
          private readonly Northwind.ModelsDB.NorthwindContext _context;
          private readonly IOptions<MyConfig> config;
          public string BingMapKey { get; private set; }
          public double latitude { get; private set; }
          public double longitude { get; private set; }
          public string query { get; private set; }
          public VIndividualCustomer VIndividualCustomer { get; private set; }
          public DetailsModel(Northwind.ModelsDB.NorthwindContext context, IOptions<MyConfig> configg)
          {
             _context = context;
             this.config = configg;
             BingMapKey = config.Value.BingMapKey;
          }
          public async Task<IActionResult> OnGetAsync(int? id)
          {
             if (id == null)
             {
                return NotFound();
             }
             VIndividualCustomer = await _context.VIndividualCustomer
                .AsNoTracking()
                .FirstOrDefaultAsync(m => m.BusinessEntityId == id);
             if (VIndividualCustomer == null)
             {
                return NotFound();
             }
             query = "1 Microsoft Way, Redmond, WA";
             Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, BingMapKey));
             GetResponse(geocodeRequest, (x) =>
             {
                var location = (BingMapsRESTToolkit.Location)x.ResourceSets[0].Resources[0];
                latitude = location.GeocodePoints[0].Coordinates[0];
                longitude = location.GeocodePoints[0].Coordinates[1];
             });
             return Page();
          }
          private void GetResponse(Uri uri, Action<Response> callback)
          {
             System.Net.WebClient wc = new WebClient();
             wc.OpenReadCompleted += (o, a) =>
             {
                if (callback != null)
                {
                   DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
                   callback(ser.ReadObject(a.Result) as Response);
                }
             };
             wc.OpenReadAsync(uri);
            
          }
       }
    }

    Wednesday, November 28, 2018 6:06 PM