dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.2k stars 9.94k forks source link

blazor client side freeze on loading - canot inject httpservice to LoggerProvider #46794

Open d00lar opened 1 year ago

d00lar commented 1 year ago

Is there an existing issue for this?

Describe the bug

hi

I want log client blazor errors
via custom ilogger / ilogger provider like it should be but i want to sent it to api via httpclient. so i have in program.cs

     builder.Services.AddHttpClient("BlazorApp_issue_ilogger.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress));
     builder.Services.AddSingleton<ILoggerProvider, DbLoggerProvider>();

so now i want to build my LoggerProvider

public class DbLoggerProvider : ILoggerProvider
{
  private readonly IOptions<DbLoggerProvider> _options;
  private readonly IServiceProvider _isp;
  private readonly IHttpService _httpService;

     public class DbLoggerProvider : ILoggerProvider
     {
        private readonly IOptions<DbLoggerProvider> _options;
        private readonly IServiceProvider _isp;
          private readonly HttpClient _httpService;

       public DbLoggerProvider(IOptions<DbLoggerProvider> options, IServiceProvider isp)
     {
         _options = options;
        _isp = isp;
         _httpService = _isp.CreateScope().ServiceProvider.GetRequiredService<HttpClient>();
     }

and this compile fine but freaze app on 'loading'

if i simply comment out this _http part

      _httpService = _isp.CreateScope().ServiceProvider.GetRequiredService<HttpClient>();

then it compiles fine also and works as usual - without any freeze

i checked also

      public DbLoggerProvider(IOptions<DbLoggerProvider> options, IServiceProvider isp, IHttpClientFactory httpClientFactory)

or

      public DbLoggerProvider(IOptions<DbLoggerProvider> options, IServiceProvider isp, HttpClient httpClient)

but result is the same - same loading freeze if i want IHttpClientFactory / Httpclient in ctor.

why this is happening ? is it some .net bug? why i canot inject it here? thanks and regards

Expected Behavior

it should allow to inject or create somehow this http service instance here also and not 'hang' ?

Steps To Reproduce

https://github.com/d00lar/BlazorApp-issue-ilogger

Exceptions (if any)

none

.NET Version

7

Anything else?

if this is not allowed because of something then some error message should be thrown ?

thanks and regards

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

d00lar commented 1 year ago

anything? some temp fix ? idea how to do it 'diferent way' ? ;P regards !

d00lar commented 1 year ago

anything? did someone even looked into it ?

d00lar commented 1 year ago

??

Andrey10183 commented 1 year ago

Hello, I don't know if that question is still actual. I'm fighting with exactly same problem. I suppose the app freezing is because you are trying to inject a scoped service (Http) to a singleton service (LoggerProvider). It can't resolve that. Instead you can try to inject in your provider IServiceScopeFactory and then give it to your logger. After that you can use it like this: using (var scope = _serviceScopeFactory.CreateScope()) { var http = scope.ServiceProvider.GetService();

            var response = await http.PostAsJsonAsync($"{EndPoints.SendUiLogs}", logs);

            response.EnsureSuccessStatusCode();
            return true;
        }

It's not a beautiful solution, but it works. Hope it helps. This is article to help understand that issue: https://samwalpole.com/using-scoped-services-inside-singletons

MackinnonBuck commented 9 months ago

@d00lar, does @Andrey10183's suggestion solve the problem you're experiencing?

ghost commented 9 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

d00lar commented 1 month ago

it does not solve this / it is the same thing as in my code / he only replaced GetRequiredService with GetService / this work when injecting scope to singelton YES but NOT when i want do do this in ILoggerProvider / ILoggerProvider is special somehow in this case. please resolve this somehow / add some message that not allowed somehow or it should work i think