IvanJosipovic / BlazorApplicationInsights

Application Insights for Blazor web applications
https://BlazorApplicationInsights.netlify.app
MIT License
189 stars 26 forks source link

Blazor Server -- updated to latest package, how to initialize... #248

Closed IvanJosipovic closed 11 months ago

IvanJosipovic commented 11 months ago

Discussed in https://github.com/IvanJosipovic/BlazorApplicationInsights/discussions/247

Originally posted by **kfinke70** November 29, 2023 I have a .NET 7 Blazor server app, still using Startup.cs. I had been using the prior version of this package fine. I upgraded, and now I'm not sure how to use the new initialization. Part of my Startup is shown: ` public void ConfigureServices(IServiceCollection services) { RegisterCognito(services); services.AddCloudRoleNameInitializer(Configuration["ApplicationInsights:RoleName"]); services.AddApplicationInsightsTelemetry(); services.AddBlazorApplicationInsights( appInsights => { appInsights.ConnectionString = Configuration["ApplicationInsights:ConnectionString"]; }, async ai => { try { { var telemetryItem = new TelemetryItem() { Tags = new Dictionary() { { "ai.cloud.role", "SPA" }, { "ai.cloud.roleInstance", "Blazor Wasm" }, } }; await ai.AddTelemetryInitializer(telemetryItem); }; } catch (Exception e) { Console.WriteLine("Could not initialize ApplicationInsights"); Console.WriteLine(e); } } );` I get an error from DI saying: Lifetime: Singleton ImplementationType: BlazorApplicationInsights.ApplicationInsights': Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'BlazorApplicationInsights.Interfaces.IApplicationInsights'.)
philippedurocher commented 11 months ago

I have the same issue using dotnet 8.0.100

[07:16:07 DBG] Starting web application...
[07:16:10 FTL] Application terminated unexpectedly
System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BlazorApplicationInsights.Interfaces.IApplicationInsights Lifetime: Singleton ImplementationType: BlazorApplicationInsights.ApplicationInsights': Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'BlazorApplicationInsights.Interfaces.IApplicationInsights'.)
 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: BlazorApplicationInsights.Interfaces.IApplicationInsights Lifetime: Singleton ImplementationType: BlazorApplicationInsights.ApplicationInsights': Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'BlazorApplicationInsights.Interfaces.IApplicationInsights'.
 ---> System.InvalidOperationException: Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'BlazorApplicationInsights.Interfaces.IApplicationInsights'.
IvanJosipovic commented 11 months ago

Please try, https://github.com/IvanJosipovic/BlazorApplicationInsights/releases/tag/v3.0.1

philippedurocher commented 11 months ago

Hello Ivan, the new version works great. Thank you for your quick response, I appreciate it.

I will leave the ticket open since I'm not the creator of it.