SamProf / MatBlazor

Material Design components for Blazor and Razor Components
http://www.matblazor.com
MIT License
2.84k stars 386 forks source link

Blazor Server Side - Http Client doesn't exist by default #196

Open etcircle opened 5 years ago

etcircle commented 5 years ago

Hi, thanks for the hard work. It is just a note.

Table component injects Http Client. However, on blazor server-side, it doesn't exist. you may want to add in the installation steps, adding something like:

services.AddScoped<HttpClient>();

OR

services.AddScoped<HttpClient>(s =>
    {
        var uriHelper = s.GetRequiredService<IUriHelper>();
        return new HttpClient
        {
            BaseAddress = new Uri(uriHelper.GetBaseUri())
        };
    });
SamProf commented 5 years ago

Yes, sure. Also like I sad before MatTable will have big rewriting. So, in future this problem should be resolved.

jan-ai commented 4 years ago

Stumble upon the same problem. Rather then require an inject through services.AddScoped I would recommend changing the component itself to support situations where http is not available. To allow this the following code adaptation should work:

Change BaseMatComponent to use OwningComponentBase instead of ComponentBase (or similar to cover MatTable only and not "every" component) public abstract class BaseMatComponent : OwningComponentBase, IBaseMatComponent, IDisposable

Add to MatTable @using Microsoft.Extensions.DependencyInjection

private System.Net.Http.HttpClient Http { get; set; }

protected override async Task OnInitializedAsync()
{
    Http = ScopedServices.GetService<System.Net.Http.HttpClient>();
…

Remove from MatTable @inject System.Net.Http.HttpClient Http

Also the functions SearchData and SearchPagedData have to be adapated to reflect that http might be NULL.