C# 将Angular/Asp.Net Core 2.1应用程序部署到AWS:500错误

C# 将Angular/Asp.Net Core 2.1应用程序部署到AWS:500错误,c#,angular,amazon-web-services,asp.net-core,C#,Angular,Amazon Web Services,Asp.net Core,我可以在本地托管我的应用程序,它运行良好。我可以通过AWS的VS向导发布到Elastic Beanstalk,没有问题。但是当我尝试访问应用程序时,我得到一个500错误,没有加载任何内容。我发现的所有东西都引用了将文件从ClientApp/dist移动到wwwroot的过程,但有两件事 1。我没有dist文件夹?2。“public IConfiguration Configuration”中的最后一行将spa静态文件的根路径设置为dist (请原谅,这是我第一次部署angular应用程序。) S

我可以在本地托管我的应用程序,它运行良好。我可以通过AWS的VS向导发布到Elastic Beanstalk,没有问题。但是当我尝试访问应用程序时,我得到一个500错误,没有加载任何内容。我发现的所有东西都引用了将文件从ClientApp/dist移动到wwwroot的过程,但有两件事

    1。我没有dist文件夹?
    2。“public IConfiguration Configuration”中的最后一行将spa静态文件的根路径设置为dist
(请原谅,这是我第一次部署angular应用程序。)

Startup.cs
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(x=>x
.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),b=>
b、 迁移程序集((“MyApp.App”))
.ConfigureWarnings(warnings=>warnings.Ignore(CoreEventId.IncludeIgnoredWarning));
services.AddMvc()
.AddJsonOptions(opt=>
{
opt.SerializerSettings.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore;
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
AddSpaStaticFiles(配置=>{configuration.RootPath=“ClientApp/dist”;});
services.AddTransient();
services.AddCors();
services.AddAutoMapper();
services.addScope();
services.addScope();
var key=Encoding.ASCII.GetBytes(Configuration.GetSection(“AppSettings:Token”).Value);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(选项=>
{
options.TokenValidationParameters=新的TokenValidationParameters
{
ValidateSuersigningKey=true,
IssuerSigningKey=新对称性安全密钥(密钥),
validateisuer=false,
ValidateAudience=false
};
});
services.addScope();
services.AddSpaStaticFiles(配置=>
{
configuration.RootPath=“ClientApp/dist”;
});
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
app.UseAuthentication();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(生成器=>
{
运行(异步上下文=>
{
context.Response.StatusCode=(int)HttpStatusCode.InternalServerError;
var error=context.Features.Get();
if(错误!=null)
{
context.Response.AddApplicationError(error.error.Message);
wait context.Response.WriteAsync(error.error.Message);
}
});
});
}
app.UseCors(x=>x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.UseHttpsRedirection();
app.ConfigureSwagger(Assembly.getExecutionGassembly());
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller}/{action=Index}/{id?}”);
});
app.UseSpa(spa=>
{
spa.Options.SourcePath=“ClientApp”;
if(env.IsDevelopment())
{
spa.UseProxyToSpadDevelopmentServer(“http://localhost:4200");
}
});
}
}

我通过命令行创建了一个新项目,而不是在Visual Studio中使用“文件>新项目”,从而绕过了这个问题

我使用命令
dotnetnewangular


然后,我将旧项目中的所有代码复制到新项目中。新项目发布良好。

500表示未处理的异常。尝试注释掉
UseExceptionHandler
,并将调用保留到
UseDeveloperExceptionPage
,以便您可以看到实际的异常消息。我仍然在浏览器控制台中收到一个500错误。浏览器窗口显示“启动应用程序时出错”。我建议检查日志以获取更多信息。我从未使用过beanstalk,但我认为它在这里解释道:@ChristophLütjen,在查看日志文件后,我没有看到任何突出的东西。这表明部署和构建是成功的。但是,它没有提到我对服务器的任何请求。(我希望它至少会注意到我提出了一个请求。)
dist
目录是作为Angular构建过程的一部分创建的。您将在ASP.NET Core 2.1 angular模板生成的
/ClientApp/.angular cli.json
文件中找到它。我知道您提到过它在本地托管时可以工作,但是请确保使用
dotnet publish
到一个目录,然后从该目录测试部署。你可能会得到更多的信息。
public class Startup
{
    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.AddDbContext<DataContext>(x => x
            .UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b =>
                b.MigrationsAssembly(("MyApp.App")))
            .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.IncludeIgnoredWarning)));

        services.AddMvc()
            .AddJsonOptions(opt =>
            {
                opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });

        services.AddTransient<Seed>();

        services.AddCors();

        services.AddAutoMapper();

        services.AddScoped<IAuthRepository, AuthRepository>();

        services.AddScoped<IBaseRepository, BaseRepository>();

        var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value);

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });

        services.AddScoped<LogUserActivity>();

        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseAuthentication();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler(builder =>
            {
                builder.Run(async context =>
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                    var error = context.Features.Get<IExceptionHandlerFeature>();
                    if (error != null)
                    {
                        context.Response.AddApplicationError(error.Error.Message);
                        await context.Response.WriteAsync(error.Error.Message);
                    }
                });
            });
        }

        app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
        app.UseHttpsRedirection();
        app.ConfigureSwagger(Assembly.GetExecutingAssembly());

        app.UseDefaultFiles();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
            }
        });
    }
}