IServiceCollection not found in web API with MVC 6

20,682

Solution 1

Add a reference to the Microsoft.Extensions.DependencyInjection NuGet package, and then I recommend doing what is explained in this link.

Solution 2

I stumbled to this question looking for help with 4.6+ (as it seems a few others were looking as well) and creating a DefaultDependencyResolver to make fully compatible with MVC 5, so I hope this helps others who may do the same.

The first answer is correct for this question to add "Microsoft.Extensions.DependencyInjection" (since the IServiceCollection is an interface defined within that package).

If you want to use the "Microsoft.Extensions.DependencyInjection" framework with MVC5 or .NET Framework 4.6+, you need to create a custom dependency resolver.

public class DefaultDependecyResolver : IDependencyResolver
{
    public IServiceProvider ServiceProvider { get; }

    public DefaultDependecyResolver(IServiceProvider serviceProvider)
        => this.ServiceProvider = serviceProvider;

    public IDependencyScope BeginScope() => this;

    public object GetService(Type serviceType)
        => this.ServiceProvider.GetService(serviceType);

    public IEnumerable<object> GetServices(Type serviceType)
        => this.ServiceProvider.GetServices(serviceType);

    public void Dispose() { }
}

Then, you can create the service provider and dependency resolver required. You can even wrap this into an "IocConfig" class to follow MVC5 conventions:

public static class IocConfig
{
    public static void Register(HttpConfiguration config)
    {
        var services = new ServiceCollection()
            .AddSingleton<ISearchService, SearchService>() // Add your dependencies
            .BuildServiceProvider();
        config.DependencyResolver = new DefaultDependecyResolver(services);
    }
}

Then you can just update the Application_Start of your global.asax:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configure(IocConfig.Register); // Added
    }
}

Note: The dependency resolver was taken mostly from here.

Share:
20,682
Banwari Yadav
Author by

Banwari Yadav

I am working since 2012, as a software developer for IT industry, where I developing web application ,software and Microsoft Dynamic CRM Customization. More than 3.5 years of diverse experience in the IT industry.

Updated on September 26, 2020

Comments

  • Banwari Yadav
    Banwari Yadav over 3 years

    I am working with web API with MVC 6, here I am going in order to inject the repository into the controller, we need to register it with the DI container. Open the Startup.cs file.

    In the ConfigureServices method, going to add the highlighted code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Owin;
    using Owin;
    using TodoApi.Models;
    
    [assembly: OwinStartup(typeof(TodoApi.Startup))]
    
    namespace TodoApi
    {
        public partial class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                ConfigureAuth(app);
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
                // Add our repository type
                services.AddSingleton<ITodoRepository, TodoRepository>();
            }
        }
    }
    

    Its showing the error...

    The type or namespace name 'IServiceCollection' could not be found (are you missing a using directive or an assembly reference?)

    • mhtsbt
      mhtsbt almost 8 years
      This looks like a dependencies issue, could you post your project.json and your full startup.cs file?
    • Banwari Yadav
      Banwari Yadav almost 8 years
      please see my updated question above with full startup.cs file.
    • kamil-mrzyglod
      kamil-mrzyglod almost 8 years
      @BanwariYadav how about project.json?
    • mhtsbt
      mhtsbt almost 8 years
      Could it be that you are not using the new project-template required to run the new version?
    • nevanom
      nevanom over 7 years
      Did you manage to solve this issue ?
  • Nkosi
    Nkosi almost 7 years
    The link provided is very useful and accurate. You should however add and explain the content here in case the link goes dead. That is why SO does not like link only answers.
  • aggsol
    aggsol about 5 years
    The link is dead.