0

I am working on an web application (.net core 5) and trying to replace a singleton dependency. I know, it is possible to replace dependency inside Startup.cs

(ConfigureServices(IServiceCollection services) method)

. But problem is I cant access "IServiceCollection " in controller. Do you guys have an idea how to achive it?

It seems this work for replacing dependency, But the problem is I dont have access to services in controller: Replace service registration in ASP.NET Core built-in DI container?

I red this, but the answer does not works: .NET Core - Changing dependency in Controller

UPDATE :

Scenario : I am developing a insta API app with InstaSharper first time user want to use this web app, my instaAPI service implementation stores null username and password for this service. Now user is going to add instagram username and password. Now I need to reset instaAPI service and set new username and password. this can be happen every time user update his/her username or password.

Hatef.
  • 534
  • 4
  • 18
  • Your question is missing some context. What are you trying to accomplish and why do you need access to the IServiceCollection from within a controller? Typically you would replace a registration within the `ConfigureServices` method. Why doesn't that work for you? Please update your question and add these details. – Steven Dec 28 '20 at 21:26
  • @Steven Thankyou for your response, I updated question and added scenario. If any additional information or code needed tell me please. – Hatef. Dec 28 '20 at 21:34
  • You must understand how the built-in DI container works, which is: after it is built, you can't change its registrations. This means that, instead of replacing the singleton, try replacing *its values*. – Steven Dec 28 '20 at 22:24
  • @Steven can you give me a link of a tutorial or something that I know how to do that? Thank you – Hatef. Dec 28 '20 at 22:29

1 Answers1

0

Like with other modern DI Containers (e.g. Autofac and Simple Injector) its impossible to change the configuration of the Container once the application is started. The problems and risks with being able to change the configuration while the application is running is explained, for instance, in this section of the documentation of a different DI Container.

These constraints, however, do force you to take a different approach. There are likely multiple solutions, but most of the solutions will boil down to conceptually the same thing, which is: don't replace a component, but instead update the state of that component.

Here's an example:

Data container for credentials:

// This data structure is immutable.
public sealed class InstaCredentials
{
    public string UserName { get; }
    public string Password { get; }

    public InstaCredentials(string userName, string password)
    {
        this.UserName = userName;
        this.Password = password;
    }
}

Abstraction over the Insta service:

public interface IInstaService
{
    // Method to apply credentials coming from the user
    void SetCredentials(InstaCredentials credentials);
}

Controller using the IInstaService:

public sealed class HomeController
{
    private readonly IInstaService instaService;

    public HomeController(IInstaService instaService)
    {
        this.instaService = instaService;
    }

    public void LogIn(LogInModel model)
    {
        // Apply credentials
        var credentials = new InstaCredentials(model.UserName, model.Password);
        this.instaService.SetCredentials(credentials);
    }
}

Insta service implementation:

public sealed class InstaServiceIMpl : IInstaService
{
    private InstaCredentials credentials = new InstaCredentials(null, null);

    public void SetCredentials(InstaCredentials credentials)
    {
        this.credentials = credentials ?? throw new ArgumentNullException(nameof(credentials));
    }
}
Steven
  • 166,672
  • 24
  • 332
  • 435
  • Did you see instasharper library on github? this does not work. I need to register instasharper IInstaAPI that gets username and password when web application runs. Second problem is everytime iis stops it needs login again and insta alerts. instasharper is a console app and saves cridentials in a file. is there anyway to do this in webapplication? – Hatef. Jan 01 '21 at 10:04