RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

Can't use NSwag.Build, because of cleaning of hosted services. #4722

Closed bielu closed 6 months ago

bielu commented 7 months ago

Hi! I was looking into generating Typecript services and models with usage of nswag, but noticed that it will not work because of : https://github.com/RicoSuter/NSwag/blob/116c636fb7491623eb6c70725b8fb20c450ec96c/src/NSwag.Commands/HostApplication.cs#L112C23-L119C26 I am trying understand why it was introduced when using GetServiceProviderWithHostFactoryResolver. As right now I can't generate models when using umbraco cms, as it will throw me missing services which are host services!

RicoSuter commented 6 months ago

See https://github.com/RicoSuter/NSwag/pull/4523

desjoerd commented 6 months ago

Which IHostedService is being resolved and gives the exception?

bielu commented 6 months ago

@desjoerd https://github.com/umbraco/Umbraco-CMS/blob/36402a149ad205f0bc9c55166c014bf68c8a8b8b/src/Umbraco.Core/Services/IRuntime.cs#L8 and it gives excaption as umbraco is trying to boot and create DI container but it requires IRuntime https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Web.Common/Extensions/WebApplicationExtensions.cs#L26

desjoerd commented 6 months ago

Hmm, I think the removal of IHostedService registrations is too greedy.

if(typeof(IHostedService).IsAssignableFrom(services[i].ServiceType)

That probably should be just an equals. I will check this on Monday whether this would solve the Umbraco issue and prevent background services from running.

desjoerd commented 6 months ago

I verified with a few tests that the removal of IHostedService registrations should only look at the specific interface and not too base interfaces.

Within Umbraco the IRuntime implements IHostedService and there is a registration just registering it as IRuntime. And a registration for the HostedService -> https://github.com/umbraco/Umbraco-CMS/blob/36402a149ad205f0bc9c55166c014bf68c8a8b8b/src/Umbraco.Web.Common/Hosting/HostBuilderExtensions.cs#L36-L37