Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Core program.cs和Startup.cs未命中_C#_.net_Asp.net Core - Fatal编程技术网

C# .NET Core program.cs和Startup.cs未命中

C# .NET Core program.cs和Startup.cs未命中,c#,.net,asp.net-core,C#,.net,Asp.net Core,我的申请有个大问题。当我使用IIS Express时,一切正常,但如果我使用IIS启动应用程序(无论是否使用Visual Studio),则会忽略程序.cs和启动.cs,因此应用程序无法运行 这在.NETCore2.2和.NETCore3.1中都会发生,在Razor页面或MVC项目中也会发生 奇怪的是,IIS一直工作到昨天,我没有做任何更改,只是在两天之间重新启动了计算机。这在我的笔记本电脑和台式电脑中都有 我不明白为什么,但这让我抓狂。你对解决这个问题有什么建议吗 Program.cs pub

我的申请有个大问题。当我使用IIS Express时,一切正常,但如果我使用IIS启动应用程序(无论是否使用Visual Studio),则会忽略
程序.cs
启动.cs
,因此应用程序无法运行

这在.NETCore2.2和.NETCore3.1中都会发生,在Razor页面或MVC项目中也会发生

奇怪的是,IIS一直工作到昨天,我没有做任何更改,只是在两天之间重新启动了计算机。这在我的笔记本电脑和台式电脑中都有

我不明白为什么,但这让我抓狂。你对解决这个问题有什么建议吗

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>();
            });
}
    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
        }
    }
公共类程序
{
公共静态void Main(字符串[]args)
{
CreateHostBuilder(args)
.Build()
.Run();
}
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
});
}
Startup.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>();
            });
}
    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
        }
    }
public const string GenericCookieScheme=“XXX”;
public const string AuthSecret=“XXX”;
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddRazorPages(选项=>
{
选项.约定.授权页(“/页”);
options.Conventions.AllowAnonymousToFolder(“/Pages/Login”);
});
services.AddSession();
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddSingleton(Configuration.GetSection(“AppSettings”).Get();
#区域服务器
services.AddEntityFrameworkSqlServer()
.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“ConfigContainer”));
services.AddEntityFrameworkSqlServer()
.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“数据容器”));
services.addScope();
services.addScope();
services.AddTransient();
DbDataContext.Init();
#端区
#区域认证
services.AddAuthentication(o=>
{
o、 DefaultChallengeScheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
o、 DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(选项=>
{
options.AccessDeniedPath=新路径字符串(“/Login”);
options.LoginPath=新路径字符串(“/Login”);
});
#端区
配置(选项=>
{
options.AutomaticAuthentication=false;
});
services.AddSingleton();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
var defaultCulture=new CultureInfo(“it”);
var localizationOptions=newrequestlocalizationoptions
{
DefaultRequestCulture=新的RequestCulture(defaultCulture),
SupportedCultures=新列表{defaultCulture},
SupportedCultures=新列表{defaultCulture}
};
app.UseRequestLocalization(localizationOptions);
app.UseHttpsRedirection();
app.UseSession();
app.UseAuthentication();
app.UseStaticFiles();
app.UseRequestLocalization(“it”);
app.UseRouting();
app.UseRouter(r=>
{
r、 MapGet(“.well-known/acme challenge/{id}”),异步(请求、响应、路由数据)=>
{
变量id=路由数据。值[“id”]作为字符串;
var file=Path.Combine(env.WebRootPath,“.well-known”,“acme challenge”,id);
等待响应。SendFileAsync(文件);
});
});
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
});
使用(var serviceScope=app.ApplicationServices.GetRequiredService().CreateScope())
{
var t=serviceScope.ServiceProvider.GetService();
#区域配置容器
如果(!serviceScope.ServiceProvider.GetService().AllMigrationsApplied())
{
serviceScope.ServiceProvider.GetService().Database.Migrate();
}
serviceScope.ServiceProvider.GetService().EnsureSeed(env.WebRootPath);
#端区
#区域数据容器
var dbContextFactory=serviceScope.ServiceProvider.GetService();
//var allTenants=serviceScope.ServiceProvider.GetService().Tenants.First();
var context=dbContextFactory.CreateDbContext(配置);
如果(!context.AllMigrationsApplied())
{
Migrate();
}
//serviceScope.ServiceProvider.GetService().ensureseed(Guid.Parse(“A2DDFB53-3221-41E7-AD27-F3CD70EC5BAF”);
#端区
}
}
您的清单:
  • IIS是否可以处理ASP.NET核心的请求
  • 如果IIS知道它