chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.98k stars 280 forks source link

Chromely + AspNetCore #305

Closed Kumhy closed 3 years ago

Kumhy commented 3 years ago

I am using Chromely + AspNetCore, This is my code: ` var appName = Assembly.GetEntryAssembly()?.GetName().Name; var webPort = ServerAppUtil.AvailablePort;

        if (webPort == -1)
        {
            throw new Exception("启动失败:系统端口被占用!");
        }

        CreateHostBuilder(args, webPort).Build().RunAsync();

        #region Test
        //Application.SetHighDpiMode(HighDpiMode.SystemAware);
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Union.FormJdLogin(ret =>
        //{

        //}));
        //return;
        #endregion

        #region Chromely
        var config = DefaultConfiguration.CreateForRuntimePlatform();

        config.UrlSchemes.Add(new Chromely.Core.Network.UrlScheme("https", "shimo.im", "https://shimo.im/docs", Chromely.Core.Network.UrlSchemeType.ExternalBrowser, true));
        config.UrlSchemes.Add(new Chromely.Core.Network.UrlScheme("https", "www.fadandi.com", "https://www.fadandi.com", Chromely.Core.Network.UrlSchemeType.ExternalBrowser, true));
        config.CustomSettings["locale"] = "zh-CN";
        config.CustomSettings["CachePath"] = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cache");
        config.DebuggingMode = true;
        //config.WindowOptions.WindowFrameless = true;
        config.WindowOptions.Size = new WindowSize(1024, 768);
        config.WindowOptions.MinimumSize = new System.Drawing.Size(800, 600);
        config.WindowOptions.Title = "Chromely";
        config.WindowOptions.RelativePathToIconFile = "fdd.ico";
        config.AppName = "Chromely";
        #endregion

        config.StartUrl = "http://localhost:8080/index.html#/login";
        //config.StartUrl = $"http://localhost:{webPort}";

        AppBuilder.Create()
            .UseConfig<DefaultConfiguration>(config)
            //.UseServices(services)
            .UseApp<FddApp>()
            .Build()
            .Run(args);`

It is normal to use Application.Run for a window Application.Run(new Union.FormJdLogin());

Using Chromely.Run a window is abnormal, and my incoming port is 5050, and the wrong port is 5051 2021-03-22T19:01:40.6826810+08:00 [FTL] Unable to start Kestrel. (fc6a528d) System.IO.IOException: Failed to bind to address http://127.0.0.1:5051: address already in use. ---> Microsoft.AspNetCore.Connections.AddressInUseException: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 ---> System.Net.Sockets.SocketException (10048): 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName) at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind() --- End of inner exception stack trace --- at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<>c__DisplayClass21_01.<<StartAsync>g__OnBind|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context) --- End of inner exception stack trace --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func2 createBinding) at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication1 application, CancellationToken cancellationToken)

mattkol commented 3 years ago

@Kumhy

Not quite sure, I understand the question here. There is no formal support for Asp.net Core, but there are base demos that should get you started with Asp.Net Core - web-chromely.

A basic approach is to ensure it runs out of Chromely first. Also note the use of FreeTcpPort: https://github.com/chromelyapps/demo-projects/blob/1cac58ffb95098223e206043ad9748c3f80dcea1/web-chromely/web-chromely-mvc/Program.cs#L78

Kumhy commented 3 years ago

@mattkol Thank you very much, this makes my code work!