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.
Adds services for controllers to the specified IServiceCollection. This method will not register services used for views or pages.
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.
AddMvc(IServiceCollection) Adds MVC services to the specified IServiceCollection. AddMvc(IServiceCollection, Action<MvcOptions>) Adds MVC services to the specified IServiceCollection.
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.
I got the same with Asp .netcore 3.0. Resolved it by adding Microsoft.AspNetCore.Mvc.NewtonsoftJson package.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With