c# - .NET Core program.cs 和 Startup.cs 未命中
问题描述
我的应用程序有一个大问题。当我使用 IIS Express 时,一切正常,但如果我使用 IIS 启动应用程序(无论是否使用 Visual Studio),Program.cs
和Startup.cs
都会被忽略,因此应用程序无法正常工作。
.NET Core 2.2 和 .NET Core 3.1 以及 Razor Pages 或 MVC 项目都会发生这种情况。
奇怪的是,IIS 一直工作到昨天,而我没有做任何更改,只是在两天之间重新启动了计算机。这在我的笔记本电脑和台式电脑中都有。
我不明白为什么,但这让我发疯。您对解决问题有什么建议吗?
程序.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
启动.cs
public const string GenericCookieScheme = "XXX";
public const string AuthSecret = "XXX";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(options =>
{
options.Conventions.AuthorizePage("/Pages");
options.Conventions.AllowAnonymousToFolder("/Pages/Login");
});
services.AddSession();
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddSingleton(Configuration.GetSection("AppSettings").Get<AppSettings>());
#region SERVER
services.AddEntityFrameworkSqlServer()
.AddDbContext<DbConfigContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ConfigContainer")));
services.AddEntityFrameworkSqlServer()
.AddDbContext<DbDataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DataContainer")));
services.AddScoped<ITenantProvider, TenantProvider>();
services.AddScoped<IUserProvider, UserProvider>();
services.AddTransient<IDbContextFactory, DbContextFactory>();
DbDataContext.Init();
#endregion
#region AUTHENTICATION
services.AddAuthentication(o =>
{
o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.AccessDeniedPath = new PathString("/Login");
options.LoginPath = new PathString("/Login");
});
#endregion
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = false;
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
var defaultCulture = new CultureInfo("it-IT");
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(defaultCulture),
SupportedCultures = new List<CultureInfo> { defaultCulture },
SupportedUICultures = new List<CultureInfo> { defaultCulture }
};
app.UseRequestLocalization(localizationOptions);
app.UseHttpsRedirection();
app.UseSession();
app.UseAuthentication();
app.UseStaticFiles();
app.UseRequestLocalization("it-IT");
app.UseRouting();
app.UseRouter(r =>
{
r.MapGet(".well-known/acme-challenge/{id}", async (request, response, routeData) =>
{
var id = routeData.Values["id"] as string;
var file = Path.Combine(env.WebRootPath, ".well-known", "acme-challenge", id);
await response.SendFileAsync(file);
});
});
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var t = serviceScope.ServiceProvider.GetService<IHttpContextAccessor>();
#region CONFIG CONTAINER
if (!serviceScope.ServiceProvider.GetService<DbConfigContext>().AllMigrationsApplied())
{
serviceScope.ServiceProvider.GetService<DbConfigContext>().Database.Migrate();
}
serviceScope.ServiceProvider.GetService<DbConfigContext>().EnsureSeeded(env.WebRootPath);
#endregion
#region DATA CONTAINER
var dbContextFactory = serviceScope.ServiceProvider.GetService<IDbContextFactory>();
//var allTenants = serviceScope.ServiceProvider.GetService<SigfridAppConfigContext>().Tenants.First();
var context = dbContextFactory.CreateDbContext(Configuration);
if (!context.AllMigrationsApplied())
{
context.Database.Migrate();
}
//serviceScope.ServiceProvider.GetService<DbDataContext>().EnsureSeeded(Guid.Parse("A2DDFB53-3221-41E7-AD27-F3CD70EC5BAF"));
#endregion
}
}
解决方案
你的清单:
- 如果 IIS 可以处理 ASP.NET Core 的请求?
- 如果 IIS 知道它需要处理对 ASP.NET Core 的请求?
对于第一项
请检查您的 IIS 是否已hosting bundle for ASP.NET Core
安装。
在这里检查:
如果没有,请在此处下载并安装:
第二项
请检查是否有一个名为的文件web.config
位于您网站的根文件夹中:
它的内容应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Aiursoft.Account.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
检查零件:<aspNetCore>
下<system.webServer>
。
推荐阅读
- c++ - 如何确定我已使用非编码文件达到 EOF?
- r - 因子不分解 x 轴标签的情节
- c++ - 具有相同基类的类的 std::variant
- python - Python中的家谱
- c# - Microsoft.CodeAnalysis 无法加载文件
- kubernetes - 如何为 Jenkins 设置未在 PodTemplate 中公开但以 YAML 格式提供的字段?
- java - Cloudera、Oracle JDK 和 Open JDK
- node.js - TypeError:无法读取未定义的属性“关闭”
- python - 是否可以使用 python 3.6 中的循环制作从 1 到 n 的数字列表,然后打印它?
- php - 如何在 Dompdf laravel 5.5 中使用分页符?