【问题标题】:Running from Visual Studio - Failed to load resource: net::ERR_CONNECTION_REFUSED - Angular, Net(5.0) app, Electron从 Visual Studio 运行 - 无法加载资源:net::ERR_CONNECTION_REFUSED - Angular、Net(5.0) app、Electron
【发布时间】:2021-11-15 11:04:48
【问题描述】:

我正在运行 Visual Studio 2019。我有一个 angular 应用程序,它在 Electron 不运行时完全可以正常工作。例如,如果我从调试列表中选择IIS Express一切正常(ApiControllers 运行正常并返回数据):

但是,如果我使用 Electron.Net 应用程序运行,网站会加载并显示页面,但我的 ApiController 无法联系。出现如下错误:

这是我的 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 服务器没有运行,但这会违背在 Electron 上运行自包含桌面应用程序的意义。我有什么选择?

我的目标:

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

  • 它将从本地文件系统读取和写入文件。
  • 它将向 API 发出请求
  • 它将有一个 Angular 前端和一个 C# 后端。

其他信息: 这可能是相关的:

【问题讨论】:

  • 有什么想法可以改善我的问题吗?不幸的是仍然卡住了。

标签: c# angular electron visual-studio-2019 .net-5


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2016-05-30
    • 2021-10-14
    相关资源
    最近更新 更多