使用 kestrel 在同一端口(如虚拟应用程序)上增加监听器
Multiply listeners on same port (like virtual apps) with 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();
}
}
Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -4091 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -4091 EADDRINUSE address already in use
at Microsoft.AspNetCore.Server.Kestrel.Networking.Libuv.Check(Int32 statusCode)
at Microsoft.AspNetCore.Server.Kestrel.Networking.UvStreamHandle.Listen(Int32 backlog, Action4 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 application)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
at ConsoleApp1.Program.Main(String[] args)
Press any key to continue . . .
有没有什么方法可以像使用 owin 和 httplistener 那样使用不同的虚拟路径在同一个端口上有两个侦听器?
我知道这曾经在使用主机和不同的应用程序时与 dnx 一起工作,所以它应该仍然可以正常工作。另一种方法是在 Startup.Configure
方法中使用 .UseWhen(context => ... )
,如 blog post.
中所述
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);
};
});
}
}
不,Kestrel 不支持此功能。 WebListener 和 IIS 可以。
以下将抛出端点已被使用的异常:
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();
}
}
Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -4091 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -4091 EADDRINUSE address already in use at Microsoft.AspNetCore.Server.Kestrel.Networking.Libuv.Check(Int32 statusCode) at Microsoft.AspNetCore.Server.Kestrel.Networking.UvStreamHandle.Listen(Int32 backlog, 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 application) at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start() at ConsoleApp1.Program.Main(String[] args) Press any key to continue . . .
有没有什么方法可以像使用 owin 和 httplistener 那样使用不同的虚拟路径在同一个端口上有两个侦听器?
我知道这曾经在使用主机和不同的应用程序时与 dnx 一起工作,所以它应该仍然可以正常工作。另一种方法是在 Startup.Configure
方法中使用 .UseWhen(context => ... )
,如 blog post.
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);
};
});
}
}
不,Kestrel 不支持此功能。 WebListener 和 IIS 可以。