IvanJosipovic / BlazorApplicationInsights

Application Insights for Blazor web applications

Home Page:https://BlazorApplicationInsights.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

IvanJosipovic opened this issue · comments

Discussed in #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<string, object?>()
                        {
                            { "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'.)

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'.

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.