【问题标题】:Hosting .NET Core Application outside IIS在 IIS 之外托管 .NET Core 应用程序
【发布时间】:2018-11-12 13:41:52
【问题描述】:

我正在尝试使用Kestrel 而不是IIS 托管.NET Core 应用程序。我似乎收到以下错误:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.AspNetCore.Server.IISIntegration, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'

杂项

class Address {
        public string ProtocolType { get; set; } = "http";
        public string Host { get; set; }
        public long Port { get; set; }
        public static Address Default = new Address { Host = "localhost", Port = 9300, ProtocolType = "http" };
    }


    static class AddressExtensions {
        public static string ToUrl(this Address @this) {
            return $"{@this.ProtocolType}//{@this.Host}:{@this.Port}";
        }
    }

计划

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
            var builder=WebHost.CreateDefaultBuilder(args); //crashes here !!

            builder.UseStartup("Startup");
            var url = Address.Default.ToUrl();
            builder.UseKestrel().UseUrls(url);
            return builder;

        }

    }

启动

public class Startup {
        public void ConfigureServices(IServiceCollection services) {
            services.AddMvcCore();

        }
        public void Configure(IApplicationBuilder app) {
            app.Map("/http", x => x.UseMvc());
            app.Map("/ws", x => {

            });
            app.UseMvc();
        }
    }

为什么它会尝试将其托管在 IIS 上?我必须指定我没有launchSettings.json 文件。它没有从.NET Core App 模板创建一个。我是否必须手动执行才能在IIS 之外运行?

我怎样才能让这个服务器在IIS之外工作和运行?

【问题讨论】:

    标签: asp.net-core hosting kestrel


    【解决方案1】:

    首先,launchSettings.json 仅用于开发。部署应用后,它不会发布,也不会使用。

    其次,CreateDefaultBuilder() 增加了 IIS 集成服务。但是,这并不意味着您当时必须在 IIS 中进行托管。您当然不应该因为包含 IIS 集成而出现错误,但您不是在 IIS 中托管。如果我不得不冒险猜测,我会想象您运行的 .NET Core SDK 版本与安装在服务器上的运行时不同。确保两者一致。

    如果您想完全摆脱 IIS 集成,则需要停止使用 CreateDefaultBuilder 并手动执行它所做的事情,当然要减去添加 IIS 集成。你可以view the source看看这个方法在处理什么。

    【讨论】:

    • 那么我可以跳过UseKestrel,UseUrls..我如何Start服务器..等等。
    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2016-07-25
    相关资源
    最近更新 更多