ConfigurationBuilder in Startup.cs Causes Freeze #55

Closed
opened 2026-01-29 16:29:17 +00:00 by claunia · 5 comments
Owner

Originally created by @zlspjp on GitHub (Nov 5, 2017).

Originally assigned to: @robertmuehsig on GitHub.

I have the following code with my Startup.cs to load configuration settings from appsettings.json:
IConfigurationRoot config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json").Build();

If I comment this code out and run dotnet electronize start, everthing runs normally. But with this code in place, when I run dotnet electronize start, package manager starts working through compiling but then hangs here in package manager:

Invoke electron.cmd - in dir: C:\Users\TEST\Documents\Visual Studio 2017\Projects\TESTElecNet\TEST\TEST\obj\Host\node_modules.bin
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\Pete\Documents\Visual Studio 2017\Projects\TESTElecNet\TEST\TEST\obj\Host\node_modules.bin>electron.cmd "....\main.js"

stdout: Use Electron Port: 58008

Any ideas? Thanks for your help!

Originally created by @zlspjp on GitHub (Nov 5, 2017). Originally assigned to: @robertmuehsig on GitHub. I have the following code with my Startup.cs to load configuration settings from appsettings.json: ``IConfigurationRoot config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json").Build();`` If I comment this code out and run dotnet electronize start, everthing runs normally. But with this code in place, when I run dotnet electronize start, package manager starts working through compiling but then hangs here in package manager: Invoke electron.cmd - in dir: C:\Users\TEST\Documents\Visual Studio 2017\Projects\TESTElecNet\TEST\TEST\obj\Host\node_modules\.bin Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. C:\Users\Pete\Documents\Visual Studio 2017\Projects\TESTElecNet\TEST\TEST\obj\Host\node_modules\.bin>electron.cmd "..\..\main.js" stdout: Use Electron Port: 58008 Any ideas? Thanks for your help!
claunia added the question label 2026-01-29 16:29:17 +00:00
Author
Owner

@robertmuehsig commented on GitHub (Nov 5, 2017):

First guess: The SetBasePath might confuse our setup. I will take a look this evening :)

@robertmuehsig commented on GitHub (Nov 5, 2017): First guess: The SetBasePath might confuse our setup. I will take a look this evening :)
Author
Owner

@robertmuehsig commented on GitHub (Nov 5, 2017):

I just added the applicationSettings files in our demo and nothing stucks, but I use the new ASP.NET Core 2.0 "Style" - we just use the DefaultBuilder, just like this one here

Could you share your full Program.cs?

@robertmuehsig commented on GitHub (Nov 5, 2017): I just added the applicationSettings files in our demo and nothing stucks, but I use the new ASP.NET Core 2.0 "Style" - we just use the DefaultBuilder, just like this one [here](https://joonasw.net/view/aspnet-core-2-configuration-changes) Could you share your full Program.cs?
Author
Owner

@zlspjp commented on GitHub (Nov 5, 2017):

Thanks for helping Robert. I'm actually calling this code from within startup.cs - trying to grab my sql connection string to load my dbcontext.
Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using ElectronNET.API;
using ElectronNET.API.Entities;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using AssessorsToolbox.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace AssessorsToolbox
{
    public class Startup
    {

         

    // 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)
        {

            IConfigurationRoot config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json").Build();

            services.AddDbContext<ToolboxContext>(options => options.UseSqlServer(config.GetConnectionString("ToolboxDatabase")));


          
            services.AddMvc();

            

            

            
        }

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

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

            var options = new StaticFileOptions { RequestPath = "", FileProvider = new EmbeddedFileProvider(typeof(FormFactory.FF).GetTypeInfo().Assembly, nameof(FormFactory)) };

            app.UseStaticFiles();


         


            Bootstrap();
            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});
        }

        public async void Bootstrap()
        {
            var options = new BrowserWindowOptions
            {
                WebPreferences = new WebPreferences
                {
                    WebSecurity = false
                }
               
            };

            await Electron.WindowManager.CreateWindowAsync(options);
        }

    }
}

Program.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ElectronNET.API;

namespace AssessorsToolbox
{
    public class Program
    {
        public static void Main(string[] args)
        {
            
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseKestrel()
                .UseElectron(args)
                .UseStartup<Startup>()
                .Build();
    }
}

Thanks again!

@zlspjp commented on GitHub (Nov 5, 2017): Thanks for helping Robert. I'm actually calling this code from within startup.cs - trying to grab my sql connection string to load my dbcontext. Startup.cs: ``` using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using ElectronNET.API; using ElectronNET.API.Entities; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using AssessorsToolbox.Data; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System.IO; namespace AssessorsToolbox { public class Startup { // 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) { IConfigurationRoot config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json").Build(); services.AddDbContext<ToolboxContext>(options => options.UseSqlServer(config.GetConnectionString("ToolboxDatabase"))); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); var options = new StaticFileOptions { RequestPath = "", FileProvider = new EmbeddedFileProvider(typeof(FormFactory.FF).GetTypeInfo().Assembly, nameof(FormFactory)) }; app.UseStaticFiles(); Bootstrap(); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); } public async void Bootstrap() { var options = new BrowserWindowOptions { WebPreferences = new WebPreferences { WebSecurity = false } }; await Electron.WindowManager.CreateWindowAsync(options); } } } ``` Program.cs: ``` using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using ElectronNET.API; namespace AssessorsToolbox { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseKestrel() .UseElectron(args) .UseStartup<Startup>() .Build(); } } ``` Thanks again!
Author
Owner

@robertmuehsig commented on GitHub (Nov 7, 2017):

I enhanced my sample and access the appsettings, which is more or less the same thing as accessing the connectionString and it worked without problems, but I used the new ASP.NET Core 2.0 "style". Could you take a look at our sample and see if this helps?

@robertmuehsig commented on GitHub (Nov 7, 2017): I enhanced my sample and access the appsettings, which is more or less the same thing as accessing the connectionString and it worked without problems, but I used the new ASP.NET Core 2.0 "style". Could you take a look at our sample and see if this helps?
Author
Owner

@zlspjp commented on GitHub (Nov 8, 2017):

Hi Robert,

I tried your sample and it works! Thanks so much for your help. I really like what you guys have done here. Thank you for sharing it with us.

Regards,
Pete

@zlspjp commented on GitHub (Nov 8, 2017): Hi Robert, I tried your sample and it works! Thanks so much for your help. I really like what you guys have done here. Thank you for sharing it with us. Regards, Pete
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#55