簡體   English   中英

在IIS外部托管.NET Core應用程序

[英]Hosting .NET Core Application outside IIS

我正在嘗試使用不在IIS上的Kestrel托管.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.'

MISC

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外部運行?

首先, launchSettings.json僅用於開發。 部署應用程序后,它不會發布也不會使用。

其次, CreateDefaultBuilder()等添加了IIS集成服務。 但是,這並不意味着您此時必須托管在IIS中。 當然,您不應該因為包含IIS集成而收到錯誤消息,但您不是在IIS中托管。 如果我不得不冒險猜測的話,我想您正在運行與服務器上安裝的運行時不同的.NET Core SDK版本。 確保兩者相互對齊。

如果您想完全擺脫IIS集成,則需要停止使用CreateDefaultBuilder並手動執行它,當然要減去IIS集成。 您可以查看源以查看該方法要處理的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM