У меня большая проблема с приложением. Когда я использую IIS Express, все работает нормально, но если я запускаю приложение с IIS (как с Visual Studio, так и без нее), Program.cs
и Startup.cs
игнорируются, поэтому приложение не работает.
Это происходит как с NET Core 2.2 и. NET Core 3.1, так и с Razor Pages или MVC проектами.
Странно то, что IIS работал до вчерашнего дня, и я не внес никаких изменений, только перезагрузка компьютера между двумя днями. Это как в моем ноутбуке, так и на рабочем столе. P C.
Я не понимаю почему, но это сводит меня с ума. Есть ли у вас какие-либо предложения по решению проблемы?
Program.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>();
});
}
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
}
}