Create an (opinionated) ASP.NET Core API service quickly.
There is a fair amount of boilerplate code required to set up a service. This is an attempt to not have to repeat the process over and over.
I'm hoping this will be helpful as a prototyping tool to get a working service up and running in very short order.
To get there, this library is opinionated on what dependencies to use.
The IoC container is AutoFac. And if you define Modules in your (launch) project they should be automatically loaded.
Serilog is used for logging.
With no configuration in appSettings.json logging should still work but will only log to Console. You can override the default behavour (i.e. use additional sinks) by adding configuraiton to appSettings.json.
Serilog plugs in very nicely to the Logging Extensions which decouples you nicely from the selected logging library.
- Create a NET Core Console application and target framework netcoreapp3.0.
- Update the project file to use the SDK "Microsoft.NET.Sdk.Web".
- Install the nuget package ThreeFourteen.QuickService (Nuget package still pending).
- Create a Startup.cs file (name it what you want) and inherit from ServiceStartup
- You'll have to create a constructor that takes IConfigurationManager and pass that to the base constructor.
- Add the below code to your Program class.
public static Task Main(string[] args)
{
var service = new ServiceHost();
return service.Run<Startup>(args);
}
- Run your service!
You'll then want to start adding "stuff".
- Add an appsettings.json file. This will allow you to further configure logging and add configuration for your service.
- Add Controllers. Otherwise your service is a not overly useful.
- Add Modules. This will allow you to configure Dependency Injection.
There are a few ways to customise the default behavour of the service.
- ServiceHost.Run() has an overload that allows you to inject an implementation of IConfigureHost which will allow for changes to be made to the IHostBuilder and IWebHostBuilder.
- In your class that inherits from ServiceStartup, you can override some methods.
- Configure() and ConfigureService().
- ConfigureMvcOptions().
- ConfigureContainer() if you want to manually handle the building of your Container.