运行 来自 Visual Studio - 无法加载资源:net::ERR_CONNECTION_REFUSED - Angular、Net(5.0) 应用、Electron

Running from Visual Studio - Failed to load resource: net::ERR_CONNECTION_REFUSED - Angular, Net(5.0) app, Electron

我 运行宁 Visual Studio 2019 年。我有一个 angular 应用程序 在 Electron 时完全正常不是运行宁。例如,如果我从调试列表中选择 IIS Express一切正常(ApiControllers 运行 正确并且 return数据):

但是,如果我 运行 使用 Electron.Net App,网站会加载并显示页面,但 我的 ApiControllers 不会可联系。出现以下错误:

这是我的 Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
    public IServiceProvider serviceProvider { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });

        // Registers a few services with their interfaces
        IOCContainer iocContainer = new IOCContainer();
        iocContainer.ConfigureServices(services);
        //

        // Is this needed?
        services.AddCors(options =>
            options.AddPolicy("DefaultCorsPolicy", builder => builder
            .AllowAnyOrigin()
            .AllowAnyHeader()
            .AllowAnyMethod()));

        // This is very important for some of the services I register in the iocContainer
        services.AddHttpClient();
    }

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

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        if (!env.IsDevelopment())
        {
            app.UseSpaStaticFiles();
        }

        app.UseRouting();
        app.UseCors();
        //app.UseAuthorization();

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

            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");
            }
        });

        ElectronBootstrap();
    }

    public async void ElectronBootstrap()
    {
        BrowserWindowOptions options = new BrowserWindowOptions
        {
            Show = false,
            //WebPreferences = new WebPreferences() {
            //    AllowRunningInsecureContent = true,
            //    ContextIsolation = false
            //}
        };
        BrowserWindow mainWindow = await Electron.WindowManager.CreateWindowAsync();
        mainWindow.OnReadyToShow += () =>
        {
            mainWindow.Show();
        };
        mainWindow.SetTitle("App Name here");
        mainWindow.WebContents.OpenDevTools();

        MenuItem[] menu = new MenuItem[]
        {
            new MenuItem
            {
                Label = "File",
                Submenu=new MenuItem[]
                {
                    new MenuItem
                    {
                        Label ="Exit",
                        Click =()=>{Electron.App.Exit();}
                    }
                }
            },
            new MenuItem
            {
                Label = "Info",
                Click = async ()=>
                {
                    await Electron.Dialog.ShowMessageBoxAsync("Welcome to App");
                }
            }
        };

        Electron.Menu.SetApplicationMenu(menu);
    }
}

这是我的 Program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseElectron(args);
                    //webBuilder.UseSetting("https_port", "8080");
                });

        //public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        //    WebHost.CreateDefaultBuilder(args)
        //.UseStartup<Startup>()
        //
    }

对我可能做错的事情有什么想法吗?我假设 IIS 服务器不是 运行ning,但这将违背在 Electron 上拥有自包含桌面应用程序 运行ning 的意义。我有哪些选择?

我的目标:

拥有一个其他人可以使用的独立桌面应用程序。

附加信息: 这可能是相关的:

请注意 Visual Studio 中为 Angular 个应用程序设置的默认应用程序。 Angular/Typescript 代码模块中有一个“getBaseUrl”常量。如果您在注入服务时使用它,它将始终引导您完成完整的 URL,这在打包您的应用程序后可能是正确的,也可能是不正确的。相反,您可以将其清空或干脆不使用它。