Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register an instance to the ServiceCollection in ASP.NET Core 1.0 RC2

Tags:

I'm migrating my web app from ASP.NET Core RC1 to RC2. In RC2 the IServiceCollection doesn't have the AddInstance method anymore. How do I get the Configuration registered?

Here how it was done in RC1

public class Startup
{
    public IConfiguration Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        // AddInstance doesn't exist
        services.AddInstance<IConfiguration>(Configuration);        
        .
        .       
    }
}
like image 490
stevo Avatar asked May 19 '16 06:05

stevo


People also ask

What is ServiceCollection C#?

The ServiceCollection class keeps a list of the services, and returns a IServiceProvider object that can be used to access the registered services. Several extension methods can be used to register services: AddSingleton, AddTransient, and AddScoped.

What is Ioption in ASP.NET Core?

IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies. IOptionsSnapshot is a Scoped service and provides a snapshot of the options at the time the IOptionsSnapshot<T> object is constructed.

What is dependency injection in ASP.NET Core?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.


1 Answers

try this:

services.AddSingleton<IConfiguration>(Configuration);

I had same problem like you and I solved it with this.

like image 174
Tomas Bako Avatar answered Sep 21 '22 13:09

Tomas Bako