Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServiceProvider callback for NATS DI configuration; #619

Merged
merged 6 commits into from
Sep 10, 2024

Conversation

william-liebert
Copy link
Contributor

@william-liebert william-liebert commented Aug 28, 2024

Issue

The NATS Client DependencyInjection package (NatsBuilder) does all configuration inline when the .AddNatsClient method is called. This does not allow consumers to build up a NATS instance across multiple packages, and all configuration must be defined at registration time, rather than runtime.

MyCompany.Nats.Core:

public static IServiceCollection AddMyCompanyNats(this IServiceCollection services)
{
    services.AddNatsClient(natsBuilder => natsBuilder
        .PoolSize(3)
        .ConfigureOptions(opts => {
            return opts; // How to configure??
         }))

    return services;
}

Proposal

Add IServiceProvider overloads to configuration methods, to enable resolution-time configuration:

MyCompany.Nats.Core:

interface IConfigurator
{
    NatsOpts Configure(NatsOpts opts);
}

public static IServiceCollection AddMyCompanyNats(this IServiceCollection services)
{
    services.AddNatsClient(natsBuilder => natsBuilder
        .PoolSize(3)
        .ConfigureOptions((serviceProvider, opts) => {
            IOptsConfigurator configurator = serviceProvider.GetRequiredService<IOptsConfigurator>();

            opts = configurator.Configure(opts);

            return opts; 
         }))

    return services;
}

MyCompany.Nats.HighSecurityCluster:

public class HighSecurityConfigurator : IConfigurator
{
    public NatsOpts Configure(NatsOpts opts) =>
        opts with {
            TlsOpts = opts.TlsOpts with {
                InsecureSkipVerify = false,
            }
        };
}

MyCompany.Nats.DemoCluster

public class DemoConfigurator : IConfigurator
{
    public NatsOpts Configure(NatsOpts opts) =>
        opts with {
            TlsOpts = opts.TlsOpts with {
                InsecureSkipVerify = true,
            }
        };
}

@william-liebert william-liebert force-pushed the feature/serviceprovider-di branch from 9d5b16e to 62724b1 Compare August 28, 2024 20:37
@william-liebert william-liebert force-pushed the feature/serviceprovider-di branch from 62724b1 to 99e96d2 Compare August 28, 2024 20:51
@william-liebert william-liebert marked this pull request as ready for review August 28, 2024 20:54
@william-liebert william-liebert marked this pull request as draft August 28, 2024 21:12
@william-liebert william-liebert marked this pull request as ready for review August 28, 2024 21:18
@mtmk
Copy link
Collaborator

mtmk commented Aug 28, 2024

I will fix the build in a minute ;)

edit: ok, build should work now!

@mtmk
Copy link
Collaborator

mtmk commented Aug 28, 2024

cc @rickdotnet

@mtmk
Copy link
Collaborator

mtmk commented Aug 28, 2024

thanks @william-liebert looks good 💯 I'll let @rickdotnet to have a look as well. We should be able to include this in 2.4.0.

@mtmk mtmk added this to the 2.4.0 milestone Aug 28, 2024
@mtmk mtmk requested a review from rickdotnet August 28, 2024 23:02
Copy link
Collaborator

@mtmk mtmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @william-liebert

@mtmk mtmk merged commit dc5fa62 into nats-io:main Sep 10, 2024
10 checks passed
mtmk added a commit that referenced this pull request Sep 11, 2024
* Fixed consume pending message calculation
* ServiceProvider callback for NATS DI configuration (#619)
* Nats web socket opts improvements (#623)
* Fix various disposable issues (#625)
* Make NuidWriter public (#618)
* NatsOpts.ConfigureWebSocketOpts callback handler (#605)
@mtmk mtmk mentioned this pull request Sep 11, 2024
mtmk added a commit that referenced this pull request Sep 11, 2024
* Fixed consume pending message calculation
* ServiceProvider callback for NATS DI configuration (#619)
* Nats web socket opts improvements (#623)
* Fix various disposable issues (#625)
* Make NuidWriter public (#618)
* NatsOpts.ConfigureWebSocketOpts callback handler (#605)
divyeshio pushed a commit to divyeshio/nats.net that referenced this pull request Sep 13, 2024
* ServiceProvider callback for NATS DI configuration;

* Recursion fix;

* Build fixes

* Build fixes

* Build fixes

* dotnet format

---------

Co-authored-by: Ziya Suzen <ziya@suzen.net>
divyeshio pushed a commit to divyeshio/nats.net that referenced this pull request Sep 13, 2024
* Fixed consume pending message calculation
* ServiceProvider callback for NATS DI configuration (nats-io#619)
* Nats web socket opts improvements (nats-io#623)
* Fix various disposable issues (nats-io#625)
* Make NuidWriter public (nats-io#618)
* NatsOpts.ConfigureWebSocketOpts callback handler (nats-io#605)
mtmk added a commit that referenced this pull request Sep 17, 2024
* Fixed consume pending message calculation (#626)
* ServiceProvider callback for NATS DI configuration (#619)
* Nats web socket opts improvements (#623)
* Fix various disposable issues (#625)
* Make NuidWriter public (#618)
* NatsOpts.ConfigureWebSocketOpts callback handler (#605)
* Simplified NATS client (#607)
* Update docs (#595)
* Add default timeout to initial commands (#594)
* Extensive logging for reconnect debugging (#593)
* Add clear next step navigation to API index doc (#592)
* Add NATS client implementation (#589)
@mtmk mtmk mentioned this pull request Sep 17, 2024
mtmk added a commit that referenced this pull request Sep 17, 2024
* Fixed consume pending message calculation (#626)
* ServiceProvider callback for NATS DI configuration (#619)
* Nats web socket opts improvements (#623)
* Fix various disposable issues (#625)
* Make NuidWriter public (#618)
* NatsOpts.ConfigureWebSocketOpts callback handler (#605)
* Simplified NATS client (#607)
* Update docs (#595)
* Add default timeout to initial commands (#594)
* Extensive logging for reconnect debugging (#593)
* Add clear next step navigation to API index doc (#592)
* Add NATS client implementation (#589)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants