我为什么在将Asp.NET Core升级到.Net 6.0后收到Application Insights警告?

huangapple go评论77阅读模式
英文:

Why am I receiving Application Insights warning in Asp.NET Core after upgrading to .Net 6.0

问题

我将为您翻译以下部分:

在 Visual Studio 2022 中,我将我的.NET Core版本从先前版本升级到.NET Core版本6.0,并更新了我的项目所需的包。但在 Program.cs 中,我收到了以下警告。

警告:'ApplicationInsightsWebHostBuilderExtensions.UseApplicationInsights(IWebHostBuilder)'
已弃用:'此方法已弃用,应使用
IServiceCollection 上的 AddApplicationInsightsTelemetry() 扩展方法。'需要解决这些问题以适配.NET Core版本6.0
在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();
        }
    }
}

请注意,我已经将代码部分保留在原样,只翻译了您提供的文本。

英文:

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&lt;Startup()
                 .UseApplicationInsights()
                 .Build();
 
 host.Run();
         }
     }
 }

答案1

得分: 4

那个警告消息告诉您问题所在。

在这种情况下,对于这个新版本,.UseApplicationInsights()已被弃用,而应使用.AddApplicationInsightsTelemetry()

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

英文:

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)

答案2

得分: 2

> 我将.NET Core版本从之前的版本升级到.NET Core
> 2022年Visual Studio 6.0版本,并更新了我的项目所需的包。但是在Program.cs中,我收到了以下警告。

根据您的编译器警告消息,您的升级后的.NET 6应用程序可能存在两个主要问题。

正确版本:

首先,对于.NET 6或更高版本,您的Microsoft.ApplicationInsights.AspNetCore版本应为2.21.0,您可以从NuGet库更新此版本。您可以查看以下截图:

我为什么在将Asp.NET Core升级到.Net 6.0后收到Application Insights警告?

Program.cs配置:

另一个重要的点是,在.NET 6或更高版本中,您的program.cs文件配置应为:

builder.Services.AddApplicationInsightsTelemetry();

但在.NET 5或更早版本中,它应该如下:

services.AddApplicationInsightsTelemetry();

我为什么在将Asp.NET Core升级到.Net 6.0后收到Application Insights警告?

因此,上述任何一个问题都可能导致您现在收到的警告。

注意: 有关详细实施和最新的NuGet包,请参阅以下官方文档:

  1. .NET 6配置
  2. Application Insights更新版本2.21.0 NuGet包
英文:

> 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:

我为什么在将Asp.NET Core升级到.Net 6.0后收到Application Insights警告?

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();

我为什么在将Asp.NET Core升级到.Net 6.0后收到Application Insights警告?

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

huangapple
  • 本文由 发表于 2023年7月27日 18:30:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定