You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use TestContainers a lot in my integration tests with libs like alba that allow to build a test server.
Most of the time, the container life time is the same as the test server.
It would be great to have an easy way to setup a test container and run it as an hosted service in a generic host.
I can suggest something and make a PR if you are ok with this idea.
The basic idea would be something like that
/// <summary>/// Extension methods for <see cref="IServiceCollection"/>./// </summary>publicstaticclassGenericHostExtension{publicstaticIServiceCollectionAddTestContainer<T>(thisIServiceCollectionservices,Action<ContainerBuilder<T>>configureDelegate)whereT:IContainer{varbuilder=newContainerBuilder<T>();configureDelegate(builder);varcontainer=builder.Build();services.AddHostedService(s =>newContainerHostedService<T>(container));returnservices;}publicstaticIServiceCollectionAddAdoTestContainer<T>(thisIServiceCollectionservices,stringconnectionStringName,Action<ContainerBuilder<T>>configureDelegate)whereT:AdoNetContainer{varbuilder=newContainerBuilder<T>();configureDelegate(builder);varcontainer=builder.Build();services.AddHostedService(s =>newContainerHostedService<T>(container));//TODO Inject the connection string in the configuration with the key 'connectionStringName'returnservices;}}internalclassContainerHostedService<T>:IHostedServicewhereT:IContainer{privatereadonlyT_container;publicContainerHostedService(Tcontainer){_container=container;}publicasyncTaskStartAsync(CancellationTokencancellationToken){await_container.StartAsync(cancellationToken);}publicasyncTaskStopAsync(CancellationTokencancellationToken){await_container.StartAsync(cancellationToken);}}
It will also be possible to inject all the logging related stuff in the container
The text was updated successfully, but these errors were encountered:
I think this is a great idea!
However, I think we should not couple this into the main project.
Eg we can support this in a separate project specific to hosted services.
I use TestContainers a lot in my integration tests with libs like alba that allow to build a test server.
Most of the time, the container life time is the same as the test server.
It would be great to have an easy way to setup a test container and run it as an hosted service in a generic host.
I can suggest something and make a PR if you are ok with this idea.
The basic idea would be something like that
It will also be possible to inject all the logging related stuff in the container
The text was updated successfully, but these errors were encountered: