This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
Reacting to Hosting changes #307
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNet.Hosting; | ||
using Microsoft.AspNet.Hosting.Server; | ||
using Microsoft.AspNet.Http; | ||
using Microsoft.AspNet.Http.Features; | ||
using Microsoft.AspNet.Server.Kestrel.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNet.Server.Kestrel | ||
{ | ||
public class KestrelServer : IServer | ||
{ | ||
private Stack<IDisposable> _disposables; | ||
private readonly IApplicationLifetime _applicationLifetime; | ||
private readonly ILogger _logger; | ||
private readonly IHttpContextFactory _httpContextFactory; | ||
|
||
public KestrelServer(IFeatureCollection features, IApplicationLifetime applicationLifetime, ILogger logger, IHttpContextFactory httpContextFactory) | ||
{ | ||
if (features == null) | ||
{ | ||
throw new ArgumentNullException(nameof(features)); | ||
} | ||
|
||
if (applicationLifetime == null) | ||
{ | ||
throw new ArgumentNullException(nameof(applicationLifetime)); | ||
} | ||
|
||
if (logger == null) | ||
{ | ||
throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
if (httpContextFactory == null) | ||
{ | ||
throw new ArgumentNullException(nameof(httpContextFactory)); | ||
} | ||
|
||
_applicationLifetime = applicationLifetime; | ||
_logger = logger; | ||
Features = features; | ||
_httpContextFactory = httpContextFactory; | ||
} | ||
|
||
public IFeatureCollection Features { get; } | ||
|
||
public void Start(RequestDelegate requestDelegate) | ||
{ | ||
if (_disposables != null) | ||
{ | ||
// The server has already started and/or has not been cleaned up yet | ||
throw new InvalidOperationException("Server has already started."); | ||
} | ||
_disposables = new Stack<IDisposable>(); | ||
|
||
try | ||
{ | ||
var information = (KestrelServerInformation)Features.Get<IKestrelServerInformation>(); | ||
var dateHeaderValueManager = new DateHeaderValueManager(); | ||
var engine = new KestrelEngine(new ServiceContext | ||
{ | ||
AppLifetime = _applicationLifetime, | ||
Log = new KestrelTrace(_logger), | ||
HttpContextFactory = _httpContextFactory, | ||
DateHeaderValueManager = dateHeaderValueManager, | ||
ConnectionFilter = information.ConnectionFilter, | ||
NoDelay = information.NoDelay | ||
}); | ||
|
||
_disposables.Push(engine); | ||
_disposables.Push(dateHeaderValueManager); | ||
|
||
if (information.ThreadCount < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(information.ThreadCount), | ||
information.ThreadCount, | ||
"ThreadCount cannot be negative"); | ||
} | ||
|
||
engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount); | ||
var atLeastOneListener = false; | ||
|
||
foreach (var address in information.Addresses) | ||
{ | ||
var parsedAddress = ServerAddress.FromUrl(address); | ||
if (parsedAddress == null) | ||
{ | ||
throw new FormatException("Unrecognized listening address: " + address); | ||
} | ||
else | ||
{ | ||
atLeastOneListener = true; | ||
_disposables.Push(engine.CreateServer( | ||
parsedAddress, | ||
requestDelegate)); | ||
} | ||
} | ||
|
||
if (!atLeastOneListener) | ||
{ | ||
throw new InvalidOperationException("No recognized listening addresses were configured."); | ||
} | ||
} | ||
catch | ||
{ | ||
Dispose(); | ||
throw; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposables != null) | ||
{ | ||
while (_disposables.Count > 0) | ||
{ | ||
_disposables.Pop().Dispose(); | ||
} | ||
_disposables = null; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to be settable given that it is initialized in constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we use object initializers. Like in https://github.com/aspnet/KestrelHttpServer/blob/dev/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs#L58-L62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍