Skip to content

Use WebApplicationBuilder #1

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<configuration>
<packageSources>
<add key="aspnet-contrib" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
<add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.100-preview.5.21302.13",
"version": "6.0.100-preview.6.21324.1",
"allowPrerelease": false,
"rollForward": "latestMajor"
}
Expand Down
71 changes: 63 additions & 8 deletions src/TodoApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
// Copyright (c) Martin Costello, 2021. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NodaTime;
using TodoApp;
using TodoApp.Data;
using TodoApp.Services;

namespace TodoApp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<IClock>((_) => SystemClock.Instance);
builder.Services.AddScoped<ITodoRepository, TodoRepository>();
builder.Services.AddScoped<ITodoService, TodoService>();

builder.Services.AddRouting();

builder.Services.AddGitHubAuthentication();

builder.Services.AddRazorPages();

builder.Services.AddDbContext<TodoContext>((serviceProvider, builder) =>
{
public static class Program
{
public static void Main(string[] args)
=> CreateHostBuilder(args).Build().Run();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var environment = serviceProvider.GetRequiredService<IHostEnvironment>();
var dataDirectory = configuration["DataDirectory"];

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults((p) => p.UseStartup<Startup>());
if (string.IsNullOrEmpty(dataDirectory) || !Path.IsPathRooted(dataDirectory))
{
dataDirectory = Path.Combine(environment.ContentRootPath, "App_Data");
}

var databaseFile = Path.Combine(dataDirectory, "TodoApp.db");

builder.UseSqlite("Data Source=" + databaseFile);
});

builder.Services.AddHttpClient();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePages();
}

app.UseHsts();
app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints((endpoints) =>
{
endpoints.MapApiRoutes();
endpoints.MapAuthenticationRoutes();
endpoints.MapRazorPages();
});

app.Run();
87 changes: 0 additions & 87 deletions src/TodoApp/Startup.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/TodoApp/TodoApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="6.0.0-preview.5.21319.40" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-preview.5.21301.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-preview.6.21324.1" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.3.2" PrivateAssets="all" />
<PackageReference Include="NodaTime" Version="3.0.5" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions tests/TodoApp.Tests/HttpServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace TodoApp
{
public sealed class HttpServerFixture : WebApplicationFactory<Startup>, IAsyncLifetime, ITestOutputHelperAccessor
public sealed class HttpServerFixture : WebApplicationFactory<Services.ITodoService>, IAsyncLifetime, ITestOutputHelperAccessor
{
private IHost? _host;
private bool _disposed;
Expand All @@ -40,7 +40,7 @@ public HttpServerFixture()

public string ServerAddress => ClientOptions.BaseAddress.ToString();

public override IServiceProvider? Services => _host?.Services;
public override IServiceProvider Services => _host?.Services!;

public void ClearOutputHelper()
=> OutputHelper = null;
Expand Down Expand Up @@ -162,7 +162,7 @@ private async Task EnsureHttpServerAsync()

private async Task CreateHttpServer()
{
var builder = CreateHostBuilder().ConfigureWebHost(ConfigureWebHost);
var builder = CreateHostBuilder()!.ConfigureWebHost(ConfigureWebHost);

_host = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion tests/TodoApp.Tests/TodoApp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="JustEat.HttpClientInterception" Version="3.1.0" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0-preview.5.21301.17" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0-preview.6.21323.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.12.2" />
<PackageReference Include="Shouldly" Version="4.0.3" />
Expand Down