Forum Discussion

baroo's avatar
baroo
Copper Contributor
Mar 19, 2025

.NET Core render modes

I moved my Blazor webassembly application form .NET 8 to .NET 9. Previously it was basicly two projects:

  • Client (static webassembly content served in docker by nginx on port 5007 with API address set to port 5007)
  • Server (database app with API on port 5009)

and the typical usage was that I connect in the browser to port 5007, webassembly content was downloaded to the browser and display data gathered form the API on port 5007. After migration to .NET 9 I can no loger download web assemby package (connection reset), but instead everything is served on port 5009 (old webassemby content and API). Moreover everything is working considerably faster but I`m confused:

  1. Why and how it is even working (how content from client appear in the server)?
  2. Which render mode I`m using currently?
  3. Do I still need my second container with nginx?
  4. How should I clean my Startup.cs in the Server project after taht migration?

 

namespace eTabelki.Server
{
    public class Startup
    {
        readonly string CorsOrigins = "CorsOrigins";

        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.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
            services.AddControllersWithViews();
            services.AddRazorPages().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

            services.AddCors(options =>
            {
                options.AddPolicy(CorsOrigins,
                    builder => builder.WithOrigins("http://10.0.10.3:5007", "https://10.0.10.3:5008")
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
            });

            services.AddTransient<IGenerateInvoiceService, GenerateInvoiceService>();
            services.AddTransient<IExportService, ExportService>();
        }

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

                using (IServiceScope scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
                {
                    var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                    context.Database.Migrate();
                    ApplicationDbContextExtensions.EnsureSeedData(context).GetAwaiter().GetResult();
                    ApplicationDbContextExtensions.EnsureSeedTemplates(context).GetAwaiter().GetResult();
                }
            }
            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();

                using (IServiceScope scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
                {
                    var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                    context.Database.Migrate();
                    ApplicationDbContextExtensions.EnsureSeedTemplates(context).GetAwaiter().GetResult();
                }
            }

            var cultureInfo = new CultureInfo("pl-PL");
            CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
            CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

            app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseCors(CorsOrigins);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToFile("index.html");
            });
        }
    }
}

 

 

No RepliesBe the first to reply

Resources