将侦听器在同一端口(例如虚拟应用程序)中乘以Kestrel



以下例外情况已经使用了端点:

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World 1");
        });
    }
}
public class Startup1
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
}
public class Program
{
    public static void Main(string[] args)
    {
        string serverUrl = $"http://localhost:4000/test1/";
        var _webHost = new WebHostBuilder().UseKestrel()
                                        .UseContentRoot(Directory.GetCurrentDirectory())
                                        .UseStartup<Startup>()
                                        .UseUrls(serverUrl)
                                         .UseIISIntegration()
                                        .Build();
        _webHost.Start();
        string serverUrl1 = $"http://localhost:4000/test2/";
        var _webHost1 = new WebHostBuilder().UseKestrel()
                                       .UseContentRoot(Directory.GetCurrentDirectory())
                                       .UseStartup<Startup1>()
                                       .UseIISIntegration()
                                       .UseUrls(serverUrl1)
                                       .Build();
        _webHost1.Start();
        Console.ReadLine();
    }
}

未经治疗的异常:system.AggregateException:发生一个或多个错误。(错误-4091 eaddrinuse地址已经在使用中)---> Microsoft.aspnetcore.server.kestrel.kestrel.networking.uvexception:错误-4091 eaddrinuse地址已经在使用 在microsoft.aspnetcore.server.kestrel.networking.libuv.check(INT32状态代码)上 在Microsoft.aspnetcore.server.kestrel.networking.uvstreamhandle.listen(INT32积压,Action 4 callback, Object state) at Microsoft.AspNetCore.Server.Kestrel.Http.TcpListenerPrimary.CreateListenSocket() at Microsoft.AspNetCore.Server.Kestrel.Http.Listener.<>c.<StartAsync>b__6_0(Object state) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Http.ListenerPrimary.<StartAsync>d__9.MoveNext() --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.KestrelEngine.CreateServer(ServerAddress address) at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[TContext](IHttpApplication 1应用程序) 在microsoft.aspnetcore.hosting.internal.webhost.start()上 在consoleapp1.program.main(string [] args)上 按任意键继续 。。。

有什么方法可以使用不同的虚拟路径与Owin和httplistener一样在同一端口上拥有两个listerner?

不,kestrel不支持这一点。weblistener和iis会这样做。

我知道它曾经在使用主机和不同应用程序时与DNX一起使用,因此它仍然应该像它一样工作。另一种选择是在Startup.Configure方法中使用.UseWhen(context => ... ),如本博客文章中所述。

public void Configure(IAppBuilder app)
{
    app.UseWhen(context => context.Request.Path.ToString().StartsWith("/test1"), testApp1 =>
    {
        app.UseMvc();
    });
    app.UseWhen(context => context.Request.Path.ToString().StartsWith("/test2"), testApp1 =>
    {
        app.UseMvc();
    });
}

public static class AppExtensions {
    public static IApplicationBuilder UseWhen(this IApplicationBuilder app
        , Func<Microsoft.AspNetCore.Http.HttpContext, bool> condition
        , Action<IApplicationBuilder> configuration)
    {
        if (app == null)
        {
            throw new ArgumentNullException(nameof(app));
        }
        if (condition == null)
        {
            throw new ArgumentNullException(nameof(condition));
        }
        if (configuration == null)
        {
            throw new ArgumentNullException(nameof(configuration));
        }
        var builder = app.New();
        configuration(builder);
        return app.Use(next => {
            builder.Run(next);
            var branch = builder.Build();
            return context => {
                if (condition(context))
                {
                    return branch(context);
                }
                return next(context);
            };
        });
    }
}

最新更新