【问题标题】:After hosting asp.net core web api as Window Service, https (https://localhost:5001/api/values) - - not accessible将 asp.net 核心 web api 托管为窗口服务后,https (https://localhost:5001/api/values) - - 无法访问
【发布时间】:2019-06-17 21:57:44
【问题描述】:

我使用配置了HTTPS 的默认模板创建了一个全新的asp.net core web api 应用程序。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }

当我以console 应用程序运行应用程序时,https 站点 (https://localhost:5001/api/values) 可以访问并给出API 结果。

现在,当我将此 Web API 部署为 Window Service 时,可以访问站点 http 站点,但无法访问 https 站点 (https://localhost:5001/api/values)?什么原因?谢谢!

【问题讨论】:

    标签: asp.net-core-webapi asp.net-core-2.1


    【解决方案1】:

    创建证书后

    dotnet dev-certs https -ep "localhost.pfx" -p Password1 --trust

    我在下面添加了代码,

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            // Configure the Url and ports to bind to
            // This overrides calls to UseUrls and the ASPNETCORE_URLS environment variable, but will be 
            // overridden if you call UseIisIntegration() and host behind IIS/IIS Express
            .UseKestrel(options =>
            {
                options.Listen(IPAddress.Loopback, 5000);
                options.Listen(IPAddress.Loopback, 5001, listenOptions =>
                {
                    listenOptions.UseHttps("localhost.pfx", "Password1");
                });
            })
    

    即使在将 web api 作为窗口服务托管之后,它也能正常工作。

    【讨论】:

      猜你喜欢
      • 2022-12-22
      • 2016-05-31
      • 1970-01-01
      • 2011-11-01
      • 2022-01-08
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      相关资源
      最近更新 更多