554

I created an .NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller. However, I am getting an error:

InvalidOperationException: Unable to resolve service for type 'WebApplication1.Data.BloggerRepository' while attempting to activate 'WebApplication1.Controllers.BlogController'.

Repository:

public interface IBloggerRepository { ... }

public class BloggerRepository : IBloggerRepository { ... }

Controller:

public class BlogController : Controller
{
    private readonly IBloggerRepository _repository;

    public BlogController(BloggerRepository repository)
    {
        _repository = repository;
    }

    public IActionResult Index() { ... }
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    
    services.AddScoped<IBloggerRepository, BloggerRepository>();
}

I'm not sure what I'm doing wrong. Any ideas?

4
  • 3
    I know this is an old question, but... You should not dispose the db context inside a service. The db context is automatically disposed by the scope resolver. If you dispose it inside a service, it might be disposed when calling a next service within the same request/scope. Commented Mar 19, 2019 at 13:49
  • 3
    Make sure the service(missing class) is added using ´services.AddTransient<YourClassOrInterface>();´ Commented Apr 16, 2019 at 1:53
  • 2
    This is a common error of Injecting the Wrong Service in the Constructor. Sometimes, we register the service correctly in the DI container but still get the same error. This might be caused by mistakenly injecting the wrong service into the constructor of the dependant. To solve this issue and others related to this, see code-maze.com/… Commented Feb 20, 2024 at 9:35
  • @ecemcy How does that link help more than the accepted answer below? Commented Feb 20, 2024 at 9:52

24 Answers 24

974

To break down the error message:

Unable to resolve service for type 'WebApplication1.Data.BloggerRepository' while attempting to activate 'WebApplication1.Controllers.BlogController'.

That is saying that your application is trying to create an instance of BlogController but it doesn't know how to create an instance of BloggerRepository to pass into the constructor.

Now look at your startup:

services.AddScoped<IBloggerRepository, BloggerRepository>();

That is saying whenever a IBloggerRepository is required, create a BloggerRepository and pass that in.

However, your controller class is asking for the concrete class BloggerRepository and the dependency injection container doesn't know what to do when asked for that directly.

I'm guessing you just made a typo, but a fairly common one. So the simple fix is to change your controller to accept something that the DI container does know how to process, in this case, the interface:

public BlogController(IBloggerRepository repository)
//                    ^
//                    Add this!
{
    _repository = repository;
}

Note that some objects have their own custom ways to be registered, this is more common when you use external Nuget packages, so it pays to read the documentation for them. For example if you got a message saying:

Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' ...

Then you would fix that using the custom extension method provided by that library which would be:

services.AddHttpContextAccessor();

For other packages - always read the docs.

Sign up to request clarification or add additional context in comments.

4 Comments

Similarly, I was inadvertently activating the wrong object in Startup.cs. I had services.AddTransient<FooService, FooService>(); instead of services.AddTransient<IFooService, FooService>();. Those pesky letters LOL. Thanks for pointing me in the right direction!
My issue was that I needed to look at the arguments of the constructors for the classes that I was injecting. Once I added the types for the arguments the error disappeared!
The catchy phrase from the answer above... "That is saying whenever a IBloggerRepository is required, create a BloggerRepository and pass that in"
I made the exact typo, I love this site.
109

I ran into this issue because in the dependency injection setup I was missing a dependency of a repository that is a dependency of a controller:

services.AddScoped<IDependencyOne, DependencyOne>();    //I was missing this line!
services.AddScoped<IDependencyTwoThatIsDependentOnDependencyOne, DependencyTwoThatIsDependentOnDependencyOne>();

1 Comment

Solved my problem, because I recognized my services were not in the correct "namespace".
45

In my case I was trying to do dependency injection for an object which required constructor arguments. In this case, during Startup I just provided the arguments from the configuration file, for example:

var config = Configuration.GetSection("subservice").Get<SubServiceConfig>();
services.AddScoped<ISubService>(provider => new SubService(config.value1, config.value2));

Comments

35

I was having a different problem, and yeah the parameterized constructor for my controller was already added with the correct interface. What I did was something straightforward. I just go to my startup.cs file, where I could see a call to register method.

public void ConfigureServices(IServiceCollection services)
{
   services.Register();
}

In my case, this Register method was in a separate class Injector. So I had to add my newly introduced Interfaces there.

public static class Injector
{
    public static void Register(this IServiceCollection services)
    {
        services.AddTransient<IUserService, UserService>();
        services.AddTransient<IUserDataService, UserDataService>();
    }
}

If you see, the parameter to this function is this IServiceCollection.

Comments

20

Only if anyone have the same situation like me, I am doing a tutorial of EntityFramework with existing database, but when the new database context is created on the models folders, we need to update the context in the startup, but not only in services.AddDbContext but AddIdentity too if you have users authentication

services.AddDbContext<NewDBContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<NewDBContext>()
                .AddDefaultTokenProviders();

Comments

16

You need to add a new service for DBcontext in the startup

Default

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

Add this

services.AddDbContext<NewDBContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("NewConnection")));

1 Comment

14
Public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IEventRepository, EventRepository>();           
}

You forgot to add "services.AddScoped" in startup ConfigureServices method.

Comments

8

I got this issue because of a rather silly mistake. I had forgotten to hook my service configuration procedure to discover controllers automatically in the ASP.NET Core application.

Adding this method solved it:

// Add framework services.
            services.AddMvc()
                    .AddControllersAsServices();      // <---- Super important

Comments

8

I had to add this line in the ConfigureServices in order to work.

services.AddSingleton<IOrderService, OrderService>();

Comments

8

Adding yet another answer to the fix, because I've been bitten by this one multiple times now. If you create an "Options" class that you're binding to configuration, you might be registering it like this:

// Your options class
public record MyOptions(string SomeSetting);

// ----- 8< -----

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MyOptions>(configuration.GetSection(configPath));
    }
}

If you now have a consumer of those options, you'll find that the following code will throw the exception above:

public class OptionsConsumer
{
    public OptionsConsumer(MyOptions options)
    {
    }
}

You have to ask for the "wrapped" version of your options instead:

public class OptionsConsumer
{
    public OptionsConsumer(IOptions<MyOptions> options)
    {
    }
}

Comments

7

I was getting below exception

        System.InvalidOperationException: Unable to resolve service for type 'System.Func`1[IBlogContext]' 
        while attempting to activate 'BlogContextFactory'.\r\n at 
        Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, ISet`1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider)\r\n at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey, TValue, TArg] (ConcurrentDictionary`2 dictionary, TKey key, Func`3 valueFactory, TArg arg)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)\r\n at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)\r\n at lambda_method(Closure , IServiceProvider , Object[] )\r\n at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()

Because I wanted register Factory to create instances of DbContext Derived class IBlogContextFactory and use Create method to instantiate instance of Blog Context so that I can use below pattern along with dependency Injection and can also use mocking for unit testing.

the pattern I wanted to use is

public async Task<List<Blog>> GetBlogsAsync()
        {
            using (var context = new BloggingContext())
            {
                return await context.Blogs.ToListAsync();
            }
        }

But Instead of new BloggingContext() I want to Inject factory via constructor as in below BlogController class

    [Route("blogs/api/v1")]

public class BlogController : ControllerBase
{
    IBloggingContextFactory _bloggingContextFactory;

    public BlogController(IBloggingContextFactory bloggingContextFactory)
    {
        _bloggingContextFactory = bloggingContextFactory;
    }

    [HttpGet("blog/{id}")]
    public async Task<Blog> Get(int id)
    {
        //validation goes here 
        Blog blog = null;
        // Instantiage context only if needed and dispose immediately
        using (IBloggingContext context = _bloggingContextFactory.CreateContext())
        {
            blog = await context.Blogs.FindAsync(id);
        }
        //Do further processing without need of context.
        return blog;
    }
}

here is my service registration code

            services
            .AddDbContext<BloggingContext>()
            .AddTransient<IBloggingContext, BloggingContext>()
            .AddTransient<IBloggingContextFactory, BloggingContextFactory>();

and below are my models and factory classes

    public interface IBloggingContext : IDisposable
{
    DbSet<Blog> Blogs { get; set; }
    DbSet<Post> Posts { get; set; }
}

public class BloggingContext : DbContext, IBloggingContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseInMemoryDatabase("blogging.db");
        //optionsBuilder.UseSqlite("Data Source=blogging.db");
    }
}

public interface IBloggingContextFactory
{
    IBloggingContext CreateContext();
}

public class BloggingContextFactory : IBloggingContextFactory
{
    private Func<IBloggingContext> _contextCreator;
    public BloggingContextFactory(Func<IBloggingContext> contextCreator)// This is fine with .net and unity, this is treated as factory function, but creating problem in .netcore service provider
    {
        _contextCreator = contextCreator;
    }

    public IBloggingContext CreateContext()
    {
        return _contextCreator();
    }
}

public class Blog
{
    public Blog()
    {
        CreatedAt = DateTime.Now;
    }

    public Blog(int id, string url, string deletedBy) : this()
    {
        BlogId = id;
        Url = url;
        DeletedBy = deletedBy;
        if (!string.IsNullOrWhiteSpace(deletedBy))
        {
            DeletedAt = DateTime.Now;
        }
    }
    public int BlogId { get; set; }
    public string Url { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime? DeletedAt { get; set; }
    public string DeletedBy { get; set; }
    public ICollection<Post> Posts { get; set; }

    public override string ToString()
    {
        return $"id:{BlogId} , Url:{Url} , CreatedAt : {CreatedAt}, DeletedBy : {DeletedBy}, DeletedAt: {DeletedAt}";
    }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

----- To Fix this in .net Core MVC project -- I did below changes on dependency registration

            services
            .AddDbContext<BloggingContext>()
            .AddTransient<IBloggingContext, BloggingContext>()
            .AddTransient<IBloggingContextFactory, BloggingContextFactory>(
                    sp => new BloggingContextFactory( () => sp.GetService<IBloggingContext>())
                );

In short in .net core developer is responsible to inject factory function, which in case of Unity and .Net Framework was taken care of.

Comments

7

For .NET 6.0

I just add this line on Program.cs

builder.Services.AddDbContext<DatabaseContext>();

Comments

6

For me it worked to add the DB context in the ConfigureServices as follows:

services.AddDBContext<DBContextVariable>();

Comments

4

If you are using AutoFac and getting this error, you should add an "As" statement to specify the service that the concrete implementation implements.

Ie. you should write:

containerBuilder.RegisterType<DataService>().As<DataService>();

instead of

containerBuilder.RegisterType<DataService>();

Comments

4

I received this error message with ILogger being injected into a .NET 5 class. I needed to add the class type to fix it.

ILogger logger --> ILogger <MyClass> logger

Comments

4

Had the same issue all I did was to register my DBContext in Startup.cs.

The problem is that you are calling a DBContext that the application has not registered with so it does not know what to do when your view tries to reference it.

Key part of the error message, "while attempting to activate"

 private readonly SmartPayDBContext _context;

Solution that worked for me

    public void ConfigureServices(IServiceCollection services)
    {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            
            services.AddDbContext<SmartPayDBContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
    }

1 Comment

How does this add to the existing answers?
4

Not sure if this will help anyone else, but I was correctly dependency injecting and got this error when trying to access my API controllers.

I had to shut down the project and rebuild after already adding them to my startup.cs class - for some reason a rebuild got Visual Studio to recognize the service class was properly registered when before it was getting an error.

Comments

3

I had the same issue and found out that my code was using the injection before it was initialized.

services.AddControllers(); // Will cause a problem if you use your IBloggerRepository in there since it's defined after this line.
services.AddScoped<IBloggerRepository, BloggerRepository>();

I know it has nothing to do with the question, but since I was sent to this page, I figure out it my be useful to someone else.

Comments

2

I replaced

services.Add(new ServiceDescriptor(typeof(IMyLogger), typeof(MyLogger)));

With

services.AddTransient<IMyLogger, MyLogger>();

And it worked for me.

Comments

2

I had problems trying to inject from my Program.cs file, by using the CreateDefaultBuilder like below, but ended up solving it by skipping the default binder. (see below).

var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureServices(servicesCollection => { servicesCollection.AddSingleton<ITest>(x => new Test()); });
    webBuilder.UseStartup<Startup>();
}).Build();

It seems like the Build should have been done inside of ConfigureWebHostDefaults to get it work, since otherwise the configuration will be skipped, but correct me if I am wrong.

This approach worked fine:

var host = new WebHostBuilder()
.ConfigureServices(servicesCollection =>
{
    var serviceProvider = servicesCollection.BuildServiceProvider();
    IConfiguration configuration = (IConfiguration)serviceProvider.GetService(typeof(IConfiguration));
    servicesCollection.AddSingleton<ISendEmailHandler>(new SendEmailHandler(configuration));
})
.UseStartup<Startup>()
.Build();

This also shows how to inject an already predefined dependency in .net core (IConfiguration) from

Comments

1

I was getting this issue when I was working with a 3rd party service - (MassTransit) using the IPublishEndpoint interface in a repo pattern.

End up being that I was calling my services before I was starting MassTransit.

Hope this helps someone...

Comments

0

Posting this response because the title doesn't specifically state how the code is deployed.

In an Azure Function, this error will occur if you forget to decorate the startup class with [assembly: FunctionsStartup(typeof(My.Functions.Startup))]

Startup.cs

[assembly: FunctionsStartup(typeof(My.Functions.Startup))]
namespace My.Functions
{
    public class Startup : FunctionsStartup
    {
     ......

1 Comment

The title isn’t the question. It isn’t intended to be. It’s just a summary. The body of the question should include clarifying details. Please don’t just respond to the title, but also acknowledge and respond to the specifics of the question as detailed in the question body.
-1

My error was more like this: System.InvalidOperationException: Unable to resolve service for type access class while attempting to activate controller class

It turns out that I was unnecessarily referencing my access class in the constructor of the controller. The controller really just needs a basic initialization of the access instance.

Comments

-7

I got the same error because of I was added the line of code below in program.cs file

builder.Services.AddScoped<IInvoiceService, InvoiceService>();
var app = builder.Build();

so I replaced the code like below.then issue fixed

builder.Services.AddScoped<IInvoiceService, InvoiceService>();
var app = builder.Build();

1 Comment

You do not explain why the second code is better. But mainly, those two blocks are identical.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.