Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hosting] Move to GenericHost #24297

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/Hosting/samples/SampleStartups/StartupBlockingOnStart.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

// Note that this sample will not run. It is only here to illustrate usage patterns.

Expand All @@ -27,20 +29,25 @@ public override void Configure(IApplicationBuilder app)
}

// Entry point for the application.
public static void Main(string[] args)
public static async Task Main(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args).Build();

var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseStartup<StartupBlockingOnStart>()
var host = new HostBuilder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this sample will hang if Stop/StopAsync isn't called?

.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config)
.UseKestrel()
.UseStartup<StartupBlockingOnStart>();
})
.Build();

using (host)
{
host.Start();
await host.StartAsync();
Console.ReadLine();
await host.StopAsync();
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/Hosting/samples/SampleStartups/StartupConfigureAddresses.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

// Note that this sample will not run. It is only here to illustrate usage patterns.

Expand All @@ -19,18 +21,22 @@ public override void Configure(IApplicationBuilder app)
}

// Entry point for the application.
public static void Main(string[] args)
public static Task Main(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args).Build();

var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseStartup<StartupConfigureAddresses>()
.UseUrls("http://localhost:5000", "http://localhost:5001")
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config)
.UseKestrel()
.UseStartup<StartupConfigureAddresses>()
.UseUrls("http://localhost:5000", "http://localhost:5001");
})
.Build();

host.Run();
return host.RunAsync();
}
}
}
16 changes: 11 additions & 5 deletions src/Hosting/samples/SampleStartups/StartupExternallyControlled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

// Note that this sample will not run. It is only here to illustrate usage patterns.

namespace SampleStartups
{
public class StartupExternallyControlled : StartupBase
{
private IWebHost _host;
private IHost _host;
private readonly List<string> _urls = new List<string>();

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -29,10 +30,15 @@ public StartupExternallyControlled()

public void Start()
{
_host = new WebHostBuilder()
.UseKestrel()
.UseStartup<StartupExternallyControlled>()
.Start(_urls.ToArray());
_host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseStartup<StartupExternallyControlled>()
.UseUrls(_urls.ToArray());
})
.Start();
}

public async Task StopAsync()
Expand Down
41 changes: 23 additions & 18 deletions src/Hosting/samples/SampleStartups/StartupFullControl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand All @@ -13,38 +14,42 @@ namespace SampleStartups
{
public class StartupFullControl
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.AddJsonFile("hosting.json", optional: true)
.AddCommandLine(args)
.Build();

var host = new WebHostBuilder()
.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902")
.UseEnvironment(Environments.Development)
.UseWebRoot("public")
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902")
.UseEnvironment(Environments.Development)
.UseWebRoot("public")
.Configure(app =>
{
// Write the application inline, this won't call any startup class in the assembly

app.Use(next => context =>
{
return next(context);
});
});
})
.ConfigureServices(services =>
{
// Configure services that the application can see
services.AddSingleton<IMyCustomService, MyCustomService>();
})
.Configure(app =>
{
// Write the application inline, this won't call any startup class in the assembly

app.Use(next => context =>
{
return next(context);
});
})
.Build();

host.Run();
return host.RunAsync();
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/Hosting/samples/SampleStartups/StartupHelloWorld.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

// Note that this sample will not run. It is only here to illustrate usage patterns.

Expand All @@ -18,14 +20,18 @@ public override void Configure(IApplicationBuilder app)
}

// Entry point for the application.
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<StartupHelloWorld>()
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseStartup<StartupHelloWorld>();
})
.Build();

host.Run();
return host.RunAsync();
}
}
}
24 changes: 15 additions & 9 deletions src/Hosting/samples/SampleStartups/StartupInjection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

// HostingStartup's in the primary assembly are run automatically.
[assembly: HostingStartup(typeof(SampleStartups.StartupInjection))]
Expand All @@ -17,18 +19,22 @@ public void Configure(IWebHostBuilder builder)
}

// Entry point for the application.
public static void Main(string[] args)
public static Task Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
// Each of these three sets ApplicationName to the current assembly, which is needed in order to
// scan the assembly for HostingStartupAttributes.
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")
// .Configure(_ => { })
.UseStartup<NormalStartup>()
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
// Each of these three sets ApplicationName to the current assembly, which is needed in order to
// scan the assembly for HostingStartupAttributes.
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")
// .Configure(_ => { })
.UseStartup<NormalStartup>();
})
.Build();

host.Run();
return host.RunAsync();
}
}

Expand Down