ElectronNET / Electron.NET

:electron: Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).
https://gitter.im/ElectronNET/community
MIT License
7.32k stars 725 forks source link

CreateWindowAsync ConnectionException. Cannot connect to server 'http://localhost:8000' #760

Closed AlphaZeroUno closed 1 year ago

AlphaZeroUno commented 1 year ago

Hi, I have a problem running an electronic application with the latest version. When I launch the application, I get the error in image. With the previous version 13.5.1 I didn't have this error

Steps to Reproduce:

  1. electronize build /target win
  2. Open application

image

Program.cs

using System; using ElectronNET.API; using ElectronNET.API.Entities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.DependencyInjection; using MvcPwa.Models; using MvcPwa.Hubs; using Microsoft.AspNetCore.SignalR; using NLog.Targets; using Microsoft.AspNetCore.Http.Connections; using Microsoft.EntityFrameworkCore;

namespace MvcPwa { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseElectron(args);

        ConfigurationManager configuration = builder.Configuration; // allows both to access and to set up the config
        IWebHostEnvironment environment = builder.Environment;

        // LoginLogout
        //builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

        //builder.Services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<AppDbContext>();

        builder.Services.ConfigureApplicationCookie(options => {
            options.Cookie.Name = ".AspNet.SharedCookie";
        });

        builder.Services.AddControllersWithViews().AddSessionStateTempDataProvider();
        builder.Services.AddRazorPages();
        builder.Services.AddSingleton<IDoStuff, DoStuff>();

        builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
            builder
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials()
            .SetIsOriginAllowed(_ => true);
        }));

        builder.Services.AddSignalR();
        builder.Services.AddSession();
        builder.Services.AddSingleton<BaseDataAccess>();
        builder.Services.AddSingleton<BinanceProvider>();

        builder.Services.AddMvc().AddSessionStateTempDataProvider();

        var app = builder.Build();

        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }else
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseCors("CorsPolicy");
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseSession();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<MyHub>("/hub", options =>
            {
                options.Transports =
                    HttpTransportType.WebSockets |
                    HttpTransportType.LongPolling;
            });
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();

        });

        NotificationHub.Current = app.Services.GetService<IHubContext<NotificationHub>>();

        Console.WriteLine($"port is null : {BridgeSettings.SocketPort == null}");

        if (HybridSupport.IsElectronActive)
        {
            ElectronBootstrap();
        }

        app.Run();
    }

    public static async void ElectronBootstrap()
    {
        // electronize build / target win
        WebPreferences wp = new WebPreferences();
        wp.NodeIntegration = false;

        var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
        {
            Width = 1152,
            Height = 940,
            Show = false,
            Title = "TTrade",
            WebPreferences = wp,
            Icon = System.Environment.CurrentDirectory + "\\Images\\Icon\\ttrade.ico"
        });
        browserWindow.SetTitle("A Binance tool");

        await browserWindow.WebContents.Session.ClearCacheAsync();

        // For the gracefull showing of the Electron Window when ready
        browserWindow.OnReadyToShow += () =>
        {
            //browserWindowSplash.Close();
            browserWindow.Show();
            browserWindow.Maximize();
        };
        Electron.Menu.SetApplicationMenu(new MenuItem[] { });

        browserWindow.OnClosed += () =>
        {
            Electron.App.Quit();
        };

    }

}

}

FlorianRappl commented 1 year ago

Can you provide the MWE in a GitHub repository? Just code fragments is unfortunately not sufficient...

GregorBiswanger commented 1 year ago

Did you also update the Electron CLI?

Then simply delete the obj/host directory from your project and try again.

AlphaZeroUno commented 1 year ago

I tried to install electronNET.CLI but it is not supported by my application in NET 6.0 I tried deleting obj/host directory , but still get the same error

AlphaZeroUno commented 1 year ago

image

FlorianRappl commented 1 year ago

Please follow the README. You install the CLI via dotnet tool - not as a normal NuGet package.

AlphaZeroUno commented 1 year ago

I updated as said and now it works, thanks.