简体   繁体   English

在 ASP.NET Core 6 中的 Program.cs 中添加命名空间和 Main 方法会有什么负面影响吗?

[英]Will adding a namespace and a Main method to Program.cs in ASP.NET Core 6 have any negative effects?

I have an ASP.NET Core 6 Web Api application.我有一个 ASP.NET Core 6 Web Api 应用程序。

Currently, the Program.cs uses the new program style with top-level statements as described here .目前, Program.cs使用带有顶级语句的新程序样式,如此所述。 This means that it does not have a namespace.这意味着它没有命名空间。

There is a bug in SonarQube 9.3 which raises an error on this point. SonarQube 9.3 中有一个错误,在这一点上会引发错误。 This bug has been fixed with version 9.6, but it will take time until we update the version at our side.此错误已在 9.6 版中修复,但我们需要一段时间才能更新我们这边的版本。

Therefore, I need a temporary solution so avoid SonarQube errors.因此,我需要一个临时解决方案,以避免出现 SonarQube 错误。

I am thinking of converting Program.cs to the old coding style.我正在考虑将Program.cs转换为旧的编码风格。

namespace Aa;

public partial class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        //Add services , etc - the whole content of Program.cs
    }
}

I see here that in a console application they are equivalent.在这里看到在控制台应用程序中它们是等价的。

My question is: what about Web API?我的问题是:Web API 呢? Can I safely add a namespace and a Main method in an ASP.NET Core 6 Web API?我可以在 ASP.NET Core 6 Web API 中安全地添加namespaceMain方法吗? Will there be any side effects?会不会有什么副作用?

Yes you can safely add a Program.Main.是的,您可以安全地添加 Program.Main。

To minimize side-effects, just copy your top-level statements into a Main method declared in a namespace scope like this:为了尽量减少副作用,只需将顶级语句复制到命名空间 scope 中声明的 Main 方法中,如下所示:

namespace foo
{
    class Program
    {
        public static int Main()
        {
            //top level statements here
            return 0;
        }
    }
}

Then any other types declared in your Program.cs will remain in the global namespace.然后在 Program.cs 中声明的任何其他类型都将保留在全局命名空间中。 In any case possible consequences are limited to compilation errors from moving types to a different namespace.在任何情况下,可能的后果都仅限于将类型移动到不同名称空间的编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心程序.cs配置 - ASP.NET Core program.cs configuration 避免在ASP.NET Core Program.cs中使用静态值 - Avoid static value in ASP.NET Core Program.cs 如何返回在 Program.cs ASP.Net core 6 中找不到 - How to return not found in Program.cs ASP.Net core 6 在 ASP.NET Core 6 Program.cs 中配置 EF - Configuring EF in ASP.NET Core 6 Program.cs 在ASP.NET Core中,有没有办法从Program.cs设置中间件? - in ASP.NET Core, is there any way to set up middleware from Program.cs? ASP.NET 核心 Web API - 如何将 .NET 核心 5 中的 SetupSerilog 转换为 .NET 核心 6 Program.cs - ASP.NET Core Web API - How to convert SetupSerilog in .NET Core 5 to .NET Core 6 Program.cs 在 startup.cs ConfigureServices 或 program.cs Main 方法中在 .NET Core 3.+ 中注册 IHostedService 时有区别吗? - Is there a difference when registering an IHostedService in .NET Core 3.+ in the startup.cs ConfigureServices or in program.cs Main method? 只要ASP.NET Core中有可用的program.cs / startup.cs,即可获取当前URL - Get Current URL as soon as available program.cs / startup.cs in ASP.NET Core ASP.NET 内核在 Program.cs 和 Startup.cs 中捕获并显示错误 - ASP.NET Core catch and display error inside Program.cs and Startup.cs ASP.NET Core 2 中的 Startup.cs 与 Program.cs - Startup.cs vs Program.cs in ASP.NET Core 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM