As part of my application I have a .Net Core API project. Unlike most cases where this project would run as its own process, I intend to have the API run in a thread, among others, in a single process. The reason for this is that the API acts as a web interface, operating on the same level as a local console interface. Both interfaces share a singleton object and perform operations on it (asynchronously). At least that is the plan. However, I have run into a problem.
I have this Startup.cs
for the API:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{.
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
..which is the default with a few minor changes, and this method used to start the API:
public static void Run()
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
What I cannot figure out is how to pass my instantiated object to the Startup
so that I can then register it as a singleton and have it work with the controllers.
Is there a way to do this? If not, then what other approach could I take?