本文介绍了"IServiceCollection"不包含"AddControllers"的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core 3.0项目,在以下启动过程中运行良好:

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

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

现在,我想将此设置移到另一个项目中,以用作Common dll,供其他数十个ASP.NET Core 3.0项目使用,其中所有这些项目都将仅通过调用 services执行启动.(); ,考虑到 Configure 将是我创建的扩展方法.

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.

为此,我用以下.csproj文件创建了另一个项目:

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>

以及以下课程:

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:

我已经添加了 Microsoft.AspNetCore.Mvc ,我不明白为什么找不到 AddControllers 方法.

推荐答案

我对Asp .netcore 3.0有所了解.通过添加Microsoft.AspNetCore.Mvc.NewtonsoftJson软件包来解决此问题.

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

这篇关于"IServiceCollection"不包含"AddControllers"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:17