Blazor Server Side against Net5 WebAPI - GetFromJsonAsync returns unexpected null RRS feed

  • Question

  • User379720387 posted

    This is a continuation of https://forums.asp.net/t/2174005.aspx?One+to+many+projection+in+a+Web+API+NET5+0+controller+how+to+do+it+

    In the above thread the end result was that the API call was successfully returned in Swagger.

    Now I am at the point where I need to present the results in Blazor Server page

    records returns null, however using swagger I can see that parameters 22, 54 and 2 (hard coded below), returns two records, all fields have values.

    With a breakpoint at return recordDetails at the end of this controller action I can see the same data that Swagger sees.

    Count = 2
        [0]: {BtApiEf5.Model.Custom.RecordDetail}
        [1]: {BtApiEf5.Model.Custom.RecordDetail}
        BDate: "Jan 24 2021 11:09PM"
        CName: "cname1"
        Initials: "JMC"
        IsBilled: false
        IsPaid: false
        ServiceItems: Count = 1
        SvcLevel: "R2"
        TDate: "Jan 24 2021 11:04PM"
        TransactionId: 1033
    Count = 1
        [0]: {BtApiEf5.Model.Custom.ServiceItem}
        BilledRate: 55.0000
        MainCat: "primary"
        ServiceId: 1025
        ServiceName: "service1 long"
        shortForm: "service1 short"

    private IEnumerable<RecordDetail> records;

    protected override async Task OnInitializedAsync(). { try { var httpClient = _clientFactory.CreateClient("ServerAPI"); httpClient.DefaultRequestHeaders.Add("Accept", "application/json") records = await httpClient.GetFromJsonAsync<RecordDetail[]>($"{baseUrl}/api/Record/DetailByClient/22/54/2"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }

    Swagger even tells me this is the url for this controller action:


    What am I missing here?

    Thursday, February 25, 2021 9:50 PM

All replies

  • User379720387 posted

    private IEnumerable<RecordDetail> recordDetails = new List<RecordDetail>();

    That is what it took, to get the data.

    This made my code run however it turns out that json data from the controller was not being populated.

    A Microsoft engineer (Ignite one-on-one consultation) has suggested a potential race condition and a remedy.

    His suggestion was to separate the data retrieval (controller) from the serialization. And this code is below.

    I can report now that data now shows what I was hoping to see in the first place. 

    However, the Deserialize statement now has a squiggle with a complaint about round hole, square peg:

    Cannot implicitly convert type System.Collections.Generic.List.Model.RecordGrid to Model.RecordGrid

    var httpClient = _clientFactory.CreateClient("ServerAPI");
                httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                var json = JsonSerializer.Serialize(records);
                //var y = JsonSerializer.Deserialize<Service>(json);
                var content = new StringContent(json);
                string errorMessage = null;
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                var response = await httpClient.GetAsync($"{baseUrl}/api/Record/ByProvider/22");
                var data = await response.Content.ReadAsStringAsync();
                RecordGrid result = new RecordGrid();
                //snackMsg = String.Format("{0} records retrieved", records.Count());
                if (response.IsSuccessStatusCode)
                    result = JsonSerializer.Deserialize<List<RecordGrid>>(data);
                    errorMessage = data;

    I suspect my problem lies here, but dont know how to remedy that.

    And here is RecordGrid:

    public class RecordGrid
            public int ClientId { get; set; }
            public int ProviderId { get; set; }
            public string CName { get; set; }
            public string Location { get; set; }
            public string State { get; set; }
            public string OName { get; set; }
            public int TxnCount { get; set; }

    Sunday, February 28, 2021 6:20 PM