.NET Core 6.0 Windows部署

var options = new WebApplicationOptions
{
    Args = args,
    //这是因为从Windows中调用GetCurrentDirectory会返回:C:\WINDOWS\system32
    //需要注意使用了WindowsService部署,就不能使用Console类,否则会报错
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

if (WindowsServiceHelpers.IsWindowsService())
{
    builder.Host.UseWindowsService();
}

// Add services to the container.
builder.Services.AddControllers(options =>
{
    options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true; //兼容可为空类型的模型校验问题
}).AddNewtonsoftJson(options =>
{
    options.UseMemberCasing();
});

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
posted @ 2024-01-26 18:13  猫探长  阅读(74)  评论(0编辑  收藏  举报