cash
cash

Reputation: 565

'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.

Upvotes: 16

Views: 24245

Answers (2)

Manpreet Kaur
Manpreet Kaur

Reputation: 221

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

Upvotes: 20

Prakash
Prakash

Reputation: 69

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.

Upvotes: 6

Related Questions