Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'IServiceCollection' does not contain a definition for 'AddControllers'

I have a ASP.NET Core 3.0 project running fine with the following startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();            
    services.AddControllers();
}

Now I want to move this setup to another project, to serve as a Common dll to be used by dozens of other ASP.NET Core 3.0 projects where all of them will execute the startup with calling just services.Configure();, considering that Configure will be an extension method created by me.

For this, I've created another project with the following .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
  </ItemGroup>

</Project>

And the following class:

using System;
using Microsoft.Extensions.DependencyInjection;

namespace MyCommon.Extensions
{
    public static class MyIServiceCollectionExtensions
    {
        public static class IServiceCollectionExtensions
        {
            public static IServiceCollection Configure(this IServiceCollection services)
            {
                services.AddCors();
                services.AddControllers();
                return services;
            }
        }
    }
}

This gives me the following build error:

'IServiceCollection' does not contain a definition for 'AddControllers'

As I've already added the Microsoft.AspNetCore.Mvc package, I don't understand why the AddControllers method couldn't be found.

like image 452
cash Avatar asked Dec 09 '19 20:12

cash


People also ask

What does services addcontrollers() do?

Adds services for controllers to the specified IServiceCollection. This method will not register services used for views or pages.

What is IServiceCollection in .NET core?

AddScoped(IServiceCollection, Type, Type) Adds a scoped service of the type specified in serviceType with an implementation of the type specified in implementationType to the specified IServiceCollection.

What is AddMvc?

AddMvc(IServiceCollection) Adds MVC services to the specified IServiceCollection. AddMvc(IServiceCollection, Action<MvcOptions>) Adds MVC services to the specified IServiceCollection.

Where is IWebHostEnvironment?

IWebHostEnvironment is included in the Microsoft. AspNetCore. Hosting package, you simply need to add it as a reference to your project by right clicking on your project file and selecting 'Manage Nuget Packages' then searching for Microsoft.


2 Answers

I got the same with Asp .netcore 3.0. Resolved it by adding Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

like image 52
Manpreet Kaur Avatar answered Sep 17 '22 18:09

Manpreet Kaur


Services.AddControllers was added in 3.0 core not in 2.2 and in addition, it recommended using Newtonsoft.Json when using .NET Core 3.0+ due to problems with the serialization: Nuget.

like image 44
Prakash Avatar answered Sep 19 '22 18:09

Prakash