skip to Main Content

I update my.NET Core Version from the previous version to .NET Core Version 6.0 in visual studio 2022 & update the required packages for my project. But in Program.cs I got the below warning.

Warning:’ApplicationInsightsWebHostBuilderExtensions.UseApplicationInsights(IWebHostBuilder)’
is obsolete: ‘This method is deprecated in favor of
AddApplicationInsightsTelemetry() extension method on
IServiceCollection.’Need to resolve these for .NET Core Version 6.0 in
visual studio 2022.

Program.cs

 using System.IO;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 namespace Demo.API
 {
 public class Program
     {
 public static void Main(string[] args)
         {
 var host = new WebHostBuilder()
                 .UseKestrel()
                 .UseContentRoot(Directory.GetCurrentDirectory())
                 .UseIISIntegration()
                 .UseStartup<Startup()
                 .UseApplicationInsights()
                 .Build();
 
 host.Run();
         }
     }
 }

2

Answers


  1. That warning message tells you the issue.

    In this case; in this new version .UseApplicationInsights() has been deprecated and instead .AddApplicationInsightsTelemetry() should be used.

    Application Insights for ASP.NET (.Net core 6.0)

    Login or Signup to reply.
  2. I update my.NET Core Version from the previous version to .NET Core
    Version 6.0 in visual studio 2022 & update the required packages for
    my project. But in Program.cs I got the below warning.

    Well, based on your compiler warning message, there might be two major issues within your Upgraded .NET 6 application.

    Correct Version:

    First of all, for .NET 6 or later version, your Microsoft.ApplicationInsights.AspNetCore version should be 2.21.0 which you can update from nuget library. You can have a look at the following screenshot:

    enter image description here

    Program.cs Configuration:

    Another important point is that, your program.cs file configuration, that is, in .NET 6 or later version, program.cs configuration should be:

    builder.Services.AddApplicationInsightsTelemetry();
    

    But in .NET 5 or earlier it was like below:

    services.AddApplicationInsightsTelemetry();
    

    enter image description here

    So, either of the above issues may ended up with the warning you are getting now.

    Note: Please refer to the following official document for details implementation and for latest nuget package:

    1. .NET 6 configuration
    2. Application Insights Updated version 2.21.0 Nuget Package
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search