简体   繁体   English

Blazor WASM MudBlazor UI 自动完成大数据集/虚拟化

[英]Blazor WASM MudBlazor UI Autocomplete large data set/virtualization

I have a Blazor WASM project that I'm trying to get a MudBlazor Autocomplete component to work with a very large data set.我有一个 Blazor WASM 项目,我正在尝试让 MudBlazor 自动完成组件处理非常大的数据集。 The api endpoint can return up to 10,000 results, so I do not want to load all the results in the OnInitializedAsync() method. api 端点最多可以返回 10,000 个结果,因此我不想在 OnInitializedAsync() 方法中加载所有结果。 I want to use the MudBlazor Autocomplete SearchFunc to go out to the api and return a list, then filter the results.我想使用 MudBlazor Autocomplete SearchFunc 将 go 输出到 api 并返回一个列表,然后过滤结果。

Program.cs程序.cs

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddMudServices();
builder.Services.AddScoped<CustomAuthorizationMessageHandler>();
//TEST CONFIGURATION
builder.Services.AddHttpClient("ServerAPI",
      client => client.BaseAddress = new Uri("https://localhost:7083"))
    .AddHttpMessageHandler<CustomAuthorizationMessageHandler>();

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
  .CreateClient("ServerAPI"));

await builder.Build().RunAsync();

AppPage.razor AppPage.razor

public ProductMaster selectedProduct { get; set; } = new ProductMaster();

<MudAutocomplete 
            T="ProductMaster" 
            Label="Product" 
            @bind-Value="selectedProduct" 
            SearchFunc="@ProductSearch1"
            ToStringFunc="@(e => e == null ? "EMPTY" : $"{e.ProdDesc} {e.ProdCode}")"
            Variant="Variant.Outlined" 
            ShowProgressIndicator="true"
            ProgressIndicatorColor="Color.Default" /> 

private async Task<IEnumerable<ProductMaster>> ProductSearch1(string value)
        {
            var productList = await Http.GetFromJsonAsync<IList<ProductMaster>>($"/api/Orders/GetProducts/?companyNumber={Order.Company}&prodDesc={value}");

            if (string.IsNullOrEmpty(value))
                return productList;

            return productList.Where(x => x.ProdDesc.Contains(value, StringComparison.InvariantCultureIgnoreCase));
        }

The issue is, within the ProductSearch1 method, the http request never goes out.问题是,在 ProductSearch1 方法中,http 请求永远不会消失。 I cannot figure out why it won't execute.我不明白为什么它不会执行。 If I load all the results from the api on the protected override async Task OnInitializedAsync() method, then filter the results as the user types it works fine.如果我在受保护的覆盖异步任务 OnInitializedAsync()方法上加载 api 的所有结果,然后在用户键入时过滤结果,它工作正常。 However, that won't work in my case, the data set is way too large.但是,这对我来说行不通,数据集太大了。

Any thoughts on this?对此有什么想法吗?

Thanks谢谢

I was able to resolve this issue by changing the dependency injection from Scoped to Singleton for the IHttpClientFactory:我能够通过将 IHttpClientFactory 的依赖项注入从 Scoped 更改为 Singleton 来解决此问题:

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
  .CreateClient("ServerAPI"));

to

builder.Services.AddSingleton(sp => sp.GetRequiredService<IHttpClientFactory>()
  .CreateClient("ServerAPI"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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