Convert IServerInformation to IFeatureCollection.

This commit is contained in:
Chris R 2015-08-31 16:46:13 -07:00
Родитель 195e06970a
Коммит 0603a69b2c
7 изменённых файлов: 37 добавлений и 95 удалений

Просмотреть файл

@ -1,7 +1,7 @@
using System; using System;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Server.WebListener; using Microsoft.AspNet.Http.Features;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
namespace HotAddSample namespace HotAddSample
@ -15,8 +15,7 @@ namespace HotAddSample
{ {
loggerfactory.AddConsole(LogLevel.Information); loggerfactory.AddConsole(LogLevel.Information);
var server = (ServerInformation)app.Server; var listener = app.ServerFeatures.Get<Microsoft.Net.Http.Server.WebListener>();
var listener = server.Listener;
listener.UrlPrefixes.Add("http://localhost:12346/pathBase/"); listener.UrlPrefixes.Add("http://localhost:12346/pathBase/");
app.Use(async (context, next) => app.Use(async (context, next) =>

Просмотреть файл

@ -4,7 +4,7 @@ using System.Text;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Server.WebListener; using Microsoft.AspNet.Http.Features;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Net.Http.Server; using Microsoft.Net.Http.Server;
@ -14,8 +14,8 @@ namespace SelfHostServer
{ {
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
{ {
var info = (ServerInformation)app.Server; var listener = app.ServerFeatures.Get<WebListener>();
info.Listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.AllowAnonymous; listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.AllowAnonymous;
loggerfactory.AddConsole(LogLevel.Verbose); loggerfactory.AddConsole(LogLevel.Verbose);

Просмотреть файл

@ -64,11 +64,14 @@ namespace Microsoft.AspNet.Server.WebListener
/// </summary> /// </summary>
/// <param name="properties"></param> /// <param name="properties"></param>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed by caller")] [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed by caller")]
public IServerInformation Initialize(IConfiguration configuration) public IFeatureCollection Initialize(IConfiguration configuration)
{ {
Microsoft.Net.Http.Server.WebListener listener = new Microsoft.Net.Http.Server.WebListener(_loggerFactory); Microsoft.Net.Http.Server.WebListener listener = new Microsoft.Net.Http.Server.WebListener(_loggerFactory);
ParseAddresses(configuration, listener); ParseAddresses(configuration, listener);
return new ServerInformation(new MessagePump(listener, _loggerFactory)); var serverFeatures = new FeatureCollection();
serverFeatures.Set(listener);
serverFeatures.Set(new MessagePump(listener, _loggerFactory));
return serverFeatures;
} }
/// <summary> /// <summary>
@ -76,27 +79,25 @@ namespace Microsoft.AspNet.Server.WebListener
/// <param name="app">The per-request application entry point.</param> /// <param name="app">The per-request application entry point.</param>
/// <param name="server">The value returned </param> /// <param name="server">The value returned </param>
/// <returns>The server. Invoke Dispose to shut down.</returns> /// <returns>The server. Invoke Dispose to shut down.</returns>
public IDisposable Start(IServerInformation server, AppFunc app) public IDisposable Start(IFeatureCollection serverFeatures, AppFunc app)
{ {
if (server == null) if (serverFeatures == null)
{ {
throw new ArgumentNullException("server"); throw new ArgumentNullException("serverFeatures");
} }
if (app == null) if (app == null)
{ {
throw new ArgumentNullException("app"); throw new ArgumentNullException("app");
} }
var serverInfo = server as ServerInformation; var messagePump = serverFeatures.Get<MessagePump>();
if (serverInfo == null) if (messagePump == null)
{ {
throw new ArgumentException("server"); throw new InvalidOperationException("messagePump");
} }
// TODO: var capabilities = new Dictionary<string, object>(); messagePump.Start(app);
return messagePump;
serverInfo.MessagePump.Start(app);
return serverInfo.MessagePump;
} }
private void ParseAddresses(IConfiguration config, Microsoft.Net.Http.Server.WebListener listener) private void ParseAddresses(IConfiguration config, Microsoft.Net.Http.Server.WebListener listener)

Просмотреть файл

@ -1,60 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc.
// All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
// WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF
// TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR
// NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing
// permissions and limitations under the License.
using System.Reflection;
using Microsoft.AspNet.Hosting.Server;
namespace Microsoft.AspNet.Server.WebListener
{
public class ServerInformation : IServerInformation
{
private MessagePump _messagePump;
internal ServerInformation(MessagePump messagePump)
{
_messagePump = messagePump;
}
internal MessagePump MessagePump
{
get { return _messagePump; }
}
// Microsoft.AspNet.Server.WebListener
public string Name
{
get { return GetType().GetTypeInfo().Assembly.GetName().Name; }
}
public Microsoft.Net.Http.Server.WebListener Listener
{
get { return _messagePump.Listener; }
}
public int MaxAccepts
{
get { return _messagePump.MaxAccepts; }
set { _messagePump.MaxAccepts = value; }
}
public bool EnableResponseCaching
{
get { return _messagePump.EnableResponseCaching; }
set { _messagePump.EnableResponseCaching = value; }
}
}
}

Просмотреть файл

@ -174,14 +174,15 @@ namespace Microsoft.AspNet.Server.WebListener
server.Dispose(); server.Dispose();
var rootUri = new Uri(root); var rootUri = new Uri(root);
var factory = new ServerFactory(loggerFactory: null); var factory = new ServerFactory(loggerFactory: null);
var serverInfo = (ServerInformation)factory.Initialize(configuration: null); var serverFeatures = factory.Initialize(configuration: null);
var listener = serverFeatures.Get<Microsoft.Net.Http.Server.WebListener>();
foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" }) foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" })
{ {
serverInfo.Listener.UrlPrefixes.Add(UrlPrefix.Create(rootUri.Scheme, rootUri.Host, rootUri.Port, path)); listener.UrlPrefixes.Add(UrlPrefix.Create(rootUri.Scheme, rootUri.Host, rootUri.Port, path));
} }
return factory.Start(serverInfo, app); return factory.Start(serverFeatures, app);
} }
private async Task<string> SendRequestAsync(string uri) private async Task<string> SendRequestAsync(string uri)

Просмотреть файл

@ -253,17 +253,17 @@ namespace Microsoft.AspNet.Server.WebListener
[Fact] [Fact]
public async Task Server_SetQueueLimit_Success() public async Task Server_SetQueueLimit_Success()
{ {
// TODO: This is just to get a dynamic port // This is just to get a dynamic port
string address; string address;
using (Utilities.CreateHttpServer(out address, env => Task.FromResult(0))) { } using (Utilities.CreateHttpServer(out address, env => Task.FromResult(0))) { }
var factory = new ServerFactory(loggerFactory: null); var factory = new ServerFactory(loggerFactory: null);
var serverInfo = (ServerInformation)factory.Initialize(configuration: null); var serverFeatures = factory.Initialize(configuration: null);
serverInfo.Listener.UrlPrefixes.Add(UrlPrefix.Create(address)); var listener = serverFeatures.Get<Microsoft.Net.Http.Server.WebListener>();
listener.UrlPrefixes.Add(UrlPrefix.Create(address));
listener.SetRequestQueueLimit(1001);
serverInfo.Listener.SetRequestQueueLimit(1001); using (factory.Start(serverFeatures, env => Task.FromResult(0)))
using (factory.Start(serverInfo, env => Task.FromResult(0)))
{ {
string response = await SendRequestAsync(address); string response = await SendRequestAsync(address);
Assert.Equal(string.Empty, response); Assert.Equal(string.Empty, response);

Просмотреть файл

@ -17,6 +17,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http.Features;
using Microsoft.Net.Http.Server; using Microsoft.Net.Http.Server;
namespace Microsoft.AspNet.Server.WebListener namespace Microsoft.AspNet.Server.WebListener
@ -61,12 +62,13 @@ namespace Microsoft.AspNet.Server.WebListener
root = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port; root = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port;
baseAddress = prefix.ToString(); baseAddress = prefix.ToString();
var serverInfo = (ServerInformation)factory.Initialize(configuration: null); var serverFeatures = factory.Initialize(configuration: null);
serverInfo.Listener.UrlPrefixes.Add(prefix); var listener = serverFeatures.Get<Microsoft.Net.Http.Server.WebListener>();
serverInfo.Listener.AuthenticationManager.AuthenticationSchemes = authType; listener.UrlPrefixes.Add(prefix);
listener.AuthenticationManager.AuthenticationSchemes = authType;
try try
{ {
return factory.Start(serverInfo, app); return factory.Start(serverFeatures, app);
} }
catch (WebListenerException) catch (WebListenerException)
{ {
@ -85,10 +87,9 @@ namespace Microsoft.AspNet.Server.WebListener
internal static IDisposable CreateServer(string scheme, string host, int port, string path, AppFunc app) internal static IDisposable CreateServer(string scheme, string host, int port, string path, AppFunc app)
{ {
var factory = new ServerFactory(loggerFactory: null); var factory = new ServerFactory(loggerFactory: null);
var serverInfo = (ServerInformation)factory.Initialize(configuration: null); var serverFeatures = factory.Initialize(configuration: null);
serverInfo.Listener.UrlPrefixes.Add(UrlPrefix.Create(scheme, host, port, path)); serverFeatures.Get<Microsoft.Net.Http.Server.WebListener>().UrlPrefixes.Add(UrlPrefix.Create(scheme, host, port, path));
return factory.Start(serverFeatures, app);
return factory.Start(serverInfo, app);
} }
} }
} }