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

WithPerTenantOptions overload allowing dependency injection? #478

Closed
Bouke opened this issue Oct 11, 2021 · 7 comments
Closed

WithPerTenantOptions overload allowing dependency injection? #478

Bouke opened this issue Oct 11, 2021 · 7 comments

Comments

@Bouke
Copy link

Bouke commented Oct 11, 2021

In order to use dependency injected services while configuring services, you'd normally inherit from IConfigureNamedOptions or IConfigureOptions and register a singleton like this:

services.AddSingleton<IConfigureOptions<CookieAuthenticationOptions>, MyConfiguration>();

Inside MyConfiguration I can have dependencies injected:

    class MyConfiguration : IConfigureOptions<CookieAuthenticationOptions>
    {
        private IDataProtectionProvider dp;

        public GatewaySessionConfiguration(IDataProtectionProvider dp)
        {
            this.dp = dp;
        }

        public void Configure(CookieAuthenticationOptions options)
        {
            options.TicketDataFormat = TicketDataFormat(dp.CreateProtector("on purpose"));
        }
    }

With the current WithPerTenantOptions<CookieAuthenticationOptions> I cannot achieve the same. The data protector is only instantiate later on in the pipeline and I cannot grab a reference to it in ConfigureServices.

@AndrewTriesToCode
Copy link
Contributor

Hi, I think I see what you mean -- you want to access DI from within the lambda inside WithPerTenantOptions right?

I will have to add this but I have a workaround that will be available before .NET 6 comes out.

@AndrewTriesToCode
Copy link
Contributor

One thing you could try for now is to implement ITenantConfigureOptions<TOptions, TTenantInfo> and inject into the ctor there. Keep in mind it registers as singleton so any DI you do should not be for scoped services.

@Bouke
Copy link
Author

Bouke commented Oct 20, 2021

The interface is not public, so I cannot implement it:

interface ITenantConfigureOptions<TOptions, TTenantInfo>

@Bouke
Copy link
Author

Bouke commented Oct 20, 2021

OT: Are you interested in a PR adding support for a Named variant, similar to IConfigureNamedOptions<T>?

@AndrewTriesToCode
Copy link
Contributor

You are right it is not public! I will change it to public for the next release which will be for .Net 6 in November at the latest.

Regarding support for names options- yes this is on my todo list and probably will come shortly after the .NET 6 release. If you want to take a stab at it and submit a PR please do so.

@AndrewTriesToCode
Copy link
Contributor

I just released 6.4.2 on nuget.org which makes this a public interface. Thanks for your patience.

LamarLugli added a commit to LamarLugli/Finbuckle.MultiTenant that referenced this issue Feb 25, 2022
AndrewTriesToCode added a commit that referenced this issue Mar 6, 2022
* Added support for named options (#478)

* Updated documentation for named options.

* Added extra comment to documentation.

* chore: refactor to match .NET named options patterns

* chore: mark unnamed tenant options types obsolete

* chore: updated docs

* chore: minor docs update

Co-authored-by: Andrew White <andrew@finbuckle.com>
github-actions bot pushed a commit that referenced this issue Mar 6, 2022
## [6.7.0](v6.6.1...v6.7.0) (2022-03-06)

### Features

* Added support for named options ([#478](#478)) ([#534](#534)) ([6f9528d](6f9528d))
@AndrewTriesToCode
Copy link
Contributor

Named options added to the latest release. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants