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
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)
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: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:
But in .NET 5 or earlier it was like below:
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: