Service Class
Base class for HTTP services.
public abstract class Service : System.IAsyncDisposableInheritance System.Object 🡒 Service
Derived
↳ Service<TSettingsClass,TSettingsInterface>
Implements System.IAsyncDisposable
Remarks
Section titled “Remarks”This class is provided to take some of the common boiler-plate code off the developers hands.
Override any of the virtual methods to modify the services behavior.
The methods are called in the following order:
- ConfigureBuilder(WebApplicationBuilder)
- ConfigureHakomServices(IContainer, IConfiguration)
- ConfigureLogging(ILoggingBuilder)
- ConfigureMvc(IMvcBuilder)
- ConfigureApplication(WebApplication)
Please note that ConfigureBuilder(WebApplicationBuilder) invokes ConfigureHealthChecks(IHealthChecksBuilder).
Constructors
Section titled “Constructors”Service() Constructor
Section titled “Service() Constructor”Initializes a new instance of Service.
protected Service();Fields
Section titled “Fields”Service.DEFAULT_URLS Field
Section titled “Service.DEFAULT_URLS Field”The base URL to host the service under if nothing else is configured.
public const string DEFAULT_URLS = "http://localhost:5000";Field Value
Section titled “Field Value”Service.HEALTHCHECK_LIVE_TAG Field
Section titled “Service.HEALTHCHECK_LIVE_TAG Field”Tag that identifies a health check as part of the liveness probe.
protected const string HEALTHCHECK_LIVE_TAG = "live";Field Value
Section titled “Field Value”Service.HEALTHCHECK_READY_TAG Field
Section titled “Service.HEALTHCHECK_READY_TAG Field”Tag that identifies a health check as part of the readiness probe.
protected const string HEALTHCHECK_READY_TAG = "ready";Field Value
Section titled “Field Value”Properties
Section titled “Properties”Service.ConfigurationRoot Property
Section titled “Service.ConfigurationRoot Property”The root node of the configuration used.
protected virtual Microsoft.Extensions.Configuration.IConfiguration ConfigurationRoot { protected get; }Property Value
Section titled “Property Value”Microsoft.Extensions.Configuration.IConfiguration
Service.HealthCheckSettings Property
Section titled “Service.HealthCheckSettings Property”The settings that determine health check behavior (e.g. timeout of graceful shutdown)
protected HAKOM.Http.Core.Net.Api.IHealthCheckSettings HealthCheckSettings { protected get; }Property Value
Section titled “Property Value”Service.HostingSettings Property
Section titled “Service.HostingSettings Property”The settings that determine basic/common hosting behavior (URLs, CORS, SSL, etc).
protected HAKOM.Http.Core.Net.Api.IHostingSettings HostingSettings { protected get; }Property Value
Section titled “Property Value”Service.Name Property
Section titled “Service.Name Property”Gets the name of this service, used to locate its configuration section.
public abstract string Name { get; }Property Value
Section titled “Property Value”Service.ServiceConfiguration Property
Section titled “Service.ServiceConfiguration Property”The configuration section that holds all the settings for the service.
protected virtual Microsoft.Extensions.Configuration.IConfigurationSection ServiceConfiguration { protected get; }Property Value
Section titled “Property Value”Microsoft.Extensions.Configuration.IConfigurationSection
Service.Urls Property
Section titled “Service.Urls Property”Once the service is running, this property will return the URLs under which it can be accessed.
public System.Collections.Generic.IEnumerable<string> Urls { get; }Property Value
Section titled “Property Value”System.Collections.Generic.IEnumerable<System.String>
Methods
Section titled “Methods”Service.ConfigureApplication(WebApplication) Method
Section titled “Service.ConfigureApplication(WebApplication) Method”Configures the Microsoft.AspNetCore.Builder.WebApplication middleware pipeline.
protected virtual void ConfigureApplication(Microsoft.AspNetCore.Builder.WebApplication application);Parameters
Section titled “Parameters”application Microsoft.AspNetCore.Builder.WebApplication
The Microsoft.AspNetCore.Builder.WebApplication to configure.
Remarks
Section titled “Remarks”Override to add custom middleware. Always call base.ConfigureApplication to apply HAKOM defaults (Accept header injection, forwarded headers, compression, localisation, HTTPS redirect, exception handling).
Service.ConfigureBuilder(WebApplicationBuilder) Method
Section titled “Service.ConfigureBuilder(WebApplicationBuilder) Method”Takes care of setting up configuration, logging, being able to run the service as a daemon/Windows service, and a few other things.
protected virtual Microsoft.Extensions.DependencyInjection.IMvcBuilder? ConfigureBuilder(Microsoft.AspNetCore.Builder.WebApplicationBuilder builder);Parameters
Section titled “Parameters”builder Microsoft.AspNetCore.Builder.WebApplicationBuilder
The Microsoft.AspNetCore.Builder.WebApplicationBuilder used to build the service.
Returns
Section titled “Returns”Microsoft.Extensions.DependencyInjection.IMvcBuilder
The instance of Microsoft.Extensions.DependencyInjection.IMvcBuilder used to configure the MVC framework. This will be passed to ConfigureMvc(IMvcBuilder)
Remarks
Section titled “Remarks”When overriding this method, make sure to call the base method!
Service.ConfigureHakomServices(IContainer, IConfiguration) Method
Section titled “Service.ConfigureHakomServices(IContainer, IConfiguration) Method”Registers HAKOM services and resolves hosting configuration from the DI container.
protected virtual void ConfigureHakomServices(HAKOM.DependencyInjection.Core.Net.Api.IContainer container, out Microsoft.Extensions.Configuration.IConfiguration? hostConfiguration);Parameters
Section titled “Parameters”container IContainer
The DI container to register services into.
hostConfiguration Microsoft.Extensions.Configuration.IConfiguration
When this method returns, contains the resolved Microsoft.Extensions.Configuration.IConfiguration for the hosting section, or null if no host configuration was found.
Remarks
Section titled “Remarks”Override this method in derived classes to register additional services. Always call base.ConfigureHakomServices.
Service.ConfigureHealthChecks(IHealthChecksBuilder) Method
Section titled “Service.ConfigureHealthChecks(IHealthChecksBuilder) Method”Override to register additional Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck implementations.
Always call base.ConfigureHealthChecks to retain the built-in checks.
Tag a check with HEALTHCHECK_READY_TAG to have it evaluated by /health/ready,
or HEALTHCHECK_LIVE_TAG for /health/live.
protected virtual void ConfigureHealthChecks(Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder healthChecksBuilder);Parameters
Section titled “Parameters”healthChecksBuilder Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder
The Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder used to register Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck implementations.
Service.ConfigureLogging(ILoggingBuilder) Method
Section titled “Service.ConfigureLogging(ILoggingBuilder) Method”Configures the logging pipeline for the service.
protected virtual void ConfigureLogging(Microsoft.Extensions.Logging.ILoggingBuilder loggingBuilder);Parameters
Section titled “Parameters”loggingBuilder Microsoft.Extensions.Logging.ILoggingBuilder
The Microsoft.Extensions.Logging.ILoggingBuilder to configure.
Remarks
Section titled “Remarks”Override to customise log providers. The default implementation reads the {Name}:Http:Logging configuration section.
Service.ConfigureMvc(IMvcBuilder) Method
Section titled “Service.ConfigureMvc(IMvcBuilder) Method”Configures MVC application parts for the service.
protected virtual void ConfigureMvc(Microsoft.Extensions.DependencyInjection.IMvcBuilder mvcBuilder);Parameters
Section titled “Parameters”mvcBuilder Microsoft.Extensions.DependencyInjection.IMvcBuilder
The Microsoft.Extensions.DependencyInjection.IMvcBuilder to configure.
Remarks
Section titled “Remarks”Override to add additional application parts or adjust MVC options. Always call base.ConfigureMvc to register the service assembly.
Service.DisposeAsync() Method
Section titled “Service.DisposeAsync() Method”Releases resources held by the service, including the underlying Microsoft.AspNetCore.Builder.WebApplication.
public System.Threading.Tasks.ValueTask DisposeAsync();Implements DisposeAsync()
Returns
Section titled “Returns”System.Threading.Tasks.ValueTask
A System.Threading.Tasks.ValueTask that completes once all resources have been disposed.
Service.Run() Method
Section titled “Service.Run() Method”Starts the service and blocks until it is stopped.
public System.Threading.Tasks.Task Run();Returns
Section titled “Returns”System.Threading.Tasks.Task
A task that completes when the service shuts down.
Service.Setup(string[]) Method
Section titled “Service.Setup(string[]) Method”Initializes the service.
public void Setup(string[] args);Parameters
Section titled “Parameters”args System.String[]
Command line parameters.
Exceptions
Section titled “Exceptions”System.InvalidOperationException
Thrown if no application parts where registered. Make sure to call base.ConfigureMvc from its overridden implementation in order to use controllers from the current assembly or manually add one or more application parts manually (i.e. by calling AddPrefixedApplicationPart(this IMvcBuilder, IEnumerable<TypeInfo>, string, string).
Service.Start() Method
Section titled “Service.Start() Method”Starts the service without blocking, allowing the caller to continue execution.
public System.Threading.Tasks.Task Start();Returns
Section titled “Returns”System.Threading.Tasks.Task
A task that completes once the service has started listening.
Service.Stop() Method
Section titled “Service.Stop() Method”Gracefully stops a service that was started with Start().
public System.Threading.Tasks.Task Stop();Returns
Section titled “Returns”System.Threading.Tasks.Task
A task that completes once the service has stopped.