1

I have been trying out Blazor WebAssembly and attempting to connect to an existing Kentico Kontent API.

I generated models using the DotNet Tool KontentModelGenerator which I then pass to Kentico's IDeliveryClient implementation.

@code{
    private IEnumerable<DinghyClass> dinghyClasses;
    protected override async Task OnInitializedAsync()
    {
        dinghyClasses = (await deliveryClient.GetItemsAsync<DinghyClass>()).Items;
    }
}

This then returns every item in the Kentico project (i.e. of all types of content model), rather than just those of the requested type.

Am I doing something wrong? Is this an issue with Kentico's DeliveryClient, or with how Blazor interprets it? The fact there are some half-populated objects created from the returned data suggests that the Javascript is populating the objects without any form of type-checking.

I have a example on GitHub which is deploying Here. The page should list the 5 dinghy classes, but rather has an (empty) object for every other content item as well. When looking at the Network tab on the chrome debugger, all items are being returned, not just the DinghyClass items.

Simply007
  • 444
  • 3
  • 14
Jonathan Twite
  • 932
  • 10
  • 24

1 Answers1

4

In looking at (and testing) the code in your GitHub repository, it seems you have generated a CustomTypeProvider but it is not registered in Program.cs. When I added:

builder.Services.AddSingleton<ITypeProvider, CustomTypeProvider>();

to Program.cs in your solution, the SDK returned the 5 dinghy class results.

Mike Berry
  • 56
  • 1
  • Yes that works. The KontentModelGenerator has a `withtypeprovider` parameter, it is set to `true` by default. I'm intrigued as to how the sdk worked at all if the type provider needs to be provided via DI. I assume the provided one overrides a default `ITypeProvider` implementation - although that default implementation breaks the SDK... I would have thought that the SDK should check that. – Jonathan Twite Sep 09 '20 at 08:11