6 ms·
This project might have helped me when I needed to implement a console app that might or not start a web server. Asp.net is very overbearing (even using minima
by maushu 2y ago
This project might have helped me when I needed to implement a console app that might or not start a web server.
Asp.net is very overbearing (even using minimal APIs) when you want to use other Microsoft utilities like DI, logging or config since it wants to be the main entry of the application.
Never found an easy way to use the host feature with a optional web application where they both shared the DI. Note that this is more a problem with the generic host than asp.net itself.
- Gluber 2y agoIt is actually possible, to seperate those things, but it's tricky. Our current product can run in several modes, one with a web ui and api and one without. If running without there is no trace of the ASP.NET Core Pipeline ( and Kestrel is also not running ) We're using ASP.NET Core Minimal APIS for both API and UI (if configured to run in that mode )
- noworriesnate 2y agoYeah we have a similar product, where we spin up multiple web servers. Code looks something like this: var builder = WebApplication.CreateBuilder([]); builder.WebHost.UseUrls($"http://127.0.0.1:0"); builder.Services.AddSingleton(...); var app = builder.Build(); app.Map... await app.StartAsync(cancellationToken); We run multiple of these at a time and have no problems.
- shireboy 2y agoIf I understand the problem, just move all your DI registrations to a shared extension method: public static ConfigurationExtensions{ public static AddMyApp(this IServiceCollection services){ services.AddScoped<IFooService,FooService>(); } } //In console app: var consoleBuilder = Host.CreateApplicationBuilder(args); consoleBuilder.Services.AddMyApp(); ... //pseudocode - in real world you'd put this in another class or method called by the commandline code: if(webHostCommandLineSwitch){ var webBuilder = WebApplication.CreateBuilder(args); webBuilder.Services.AddMyApp(); ... }