Saya tidak dapat menjalankan Situs Web Net Core 3.1 saya. Membaca file stdout dikatakan:

Pengecualian yang tidak tertangani. System.InvalidOperationException: Untuk menggunakan UseSpaStaticFiles, Anda harus terlebih dahulu mendaftarkan ISpaStaticFileProvider di penyedia layanan, biasanya dengan memanggil services.AddSpaStaticFiles.

Saya tidak pernah mengalami masalah dalam mempublikasikan proyek ini dan Startup.cs belum dimodifikasi untuk sementara waktu.

Mungkin saya lupa melakukan beberapa perubahan dalam migrasi dari 2.2 ke 3.1?

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddAuthorization();
    services.AddControllersWithViews();
    services.AddMvcCore().AddRazorPages().AddRazorRuntimeCompilation();
    services.AddJsReport(new LocalReporting().UseBinary(JsReportBinary.GetBinary()).KillRunningJsReportProcesses().AsUtility().Create());
    services.ConfigureAuthentication(Configuration.GetValue<string>("Secret"));

    services.AddAutoMapper(profileTypes);

    services.ConfigureCors();

    services.AddDbContext<TavoraContext>
        (options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
            mysqlOptions =>
            {
                mysqlOptions.ServerVersion(new Version(8, 0, 16), ServerType.MySql);
                mysqlOptions.MigrationsAssembly("TavoraCore");
            }));

    // Autofac
    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterModule<TavoraService.AutofacModule>();
    containerBuilder.Populate(services);
    var container = containerBuilder.Build();

    // In production, the Angular files will be served from this directory
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });

    return new AutofacServiceProvider(container);
}

// 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();

        //if (Regex.IsMatch(Configuration["ASPNETCORE_URLS"], @"https?://localhost[:/]")) // is local
        //{
        //    string tsDefinitionsPath = "./ClientApp/src/app/models/";
        //    string tsDefinitionsFullPath = Path.Combine(env.ContentRootPath, tsDefinitionsPath);

        //    app.GenerateTypeScriptInterfaces(tsDefinitionsFullPath);

        //    app.UseCors(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
        //}
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    
    app.UseStaticFiles();
    app.UseSpaStaticFiles();
    
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Resources")),
        RequestPath = new PathString("/Resources")
    });

    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Themes")),
        RequestPath = new PathString("/Themes")
    });

    app.UseRouting();
    app.UseCors();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller}/{action=index}/{id?}");
    });

    app.UseSpa(spa =>
    {
        // To learn more about options for serving an Angular SPA from ASP.NET Core,
        // see https://go.microsoft.com/fwlink/?linkid=864501

        spa.Options.SourcePath = "ClientApp";

        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "start");
        }
    });
}
1
Rafael Osuna Dominguez 10 Juli 2020, 20:18

1 menjawab

Jawaban Terbaik

Yah, saya harap jawaban ini membantu seseorang kapan-kapan.

Autofac dengan Net Core 3.0+ membutuhkan beberapa perubahan.

public IServiceProvider ConfigureServices -> public void ConfigureServices

Dan, jelas, hapus baris:

return new AutofacServiceProvider(container);

Sekarang situsnya sedang berjalan

0
Rafael Osuna Dominguez 10 Juli 2020, 20:52