Base classes, Autofac modules and extensions methods to help configure ASP.Net Core Web API apps.
AspNetCoreApi.Boilerplate is a .Net Standard 2.1 class library that has the following dependencies.
This is not a full-dependency tree, but just the major dependencies listed as close to the top of the tree as possible.
- AspNetCoreApi.Infrastructure: Serilog, Mediatr, Swagger, Problem Details and global exception filter configuration
- MediatR
- EntityManagement:
EntityFramework.Core
repository pattern- Microsoft.EntityFrameworkCore
- EntityManagement.Core: Base entites and interfaces for
EntityManagment
written in a DDD-style- FluentValidation: Used for validation rules in domain entities
- Serilog
- Swashbuckle.AspNetCore
-
Create a new ASP.Net Core 3.1 API website using the "blank" template.
-
Add the
AspNetCoreApi.Boilerplate
package reference. -
Add the other non-analyser package references shown in the sample applications.
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="AspNetCoreApi.Boilerplate" Version="0.2.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" /> </ItemGroup> </Project>
-
Configure
Program.cs
public static class Program { public static void Main(string[] args) { var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => { logging.ClearProviders(); logging.AddSerilog(); }) .UseSerilog() .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>(); }) .Build(); host.Run(); } }
-
For your
Startup.cs
there areabstract
memebers for items that need to be explicitly specified andprotected virtual
memmbers for optional configuration. -
Add the settings to your
appsettings.json
.{ "ApplicationSettings": { "Name": "My.App", "Environment": "Dev", "Version": "0.0.1" }, "SeqSettings": { "Uri": "http://localhost:5341", "Key": "" }, "ConnectionStrings": { "DefaultConnection": "Server=.;Initial Catalog=My.App.Db;Trusted_Connection=True" }, "AllowedOrigins": "http://localhost:4200" }
-
Implement the rest of your application.