Services.AddOptions() 模棱两可

Services.AddOptions() ambiguous

我正忙于将我们的 ASP.NET 核心 API 从 RC1 迁移到 RC2。

RC2 要求 we configure the following 使用 IOptions<T>:

services.AddOptions();

但是我得到以下编译错误:

Error CS0121 The call is ambiguous between the following methods or properties: 'Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptions(Microsoft.Extensions.DependencyInjection.IServiceCollection)' and 'Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptions(Microsoft.Extensions.DependencyInjection.IServiceCollection)' TransitApi.Api..NET Framework 4.5.2

这些是完全相同的扩展方法!我试过删除所有包和删除锁定文件,但无济于事。

project.json:

{
  "title": "MyProject.Api",
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "dependencies": {

    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authorization": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.Core": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.Abstractions": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0-rc2-final",
    "Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Http": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Newtonsoft.Json": "8.0.3",
    "WindowsAzure.Storage": "7.0.0",
    "Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Cors": "1.0.0-rc2-final"
  },
  "frameworks": {
    "net452": { }
  },
  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "appsettings.json",
      "web.config"
    ]
  }
}

检查项目中 "References" 下的依赖项。

您的一个依赖项正在获取旧库,我怀疑它是 "WindowsAzure.Storage": "7.0.0",因为它似乎是唯一的候选者。根据 GitHub 项目页面,我认为 7.0.2 preview 版本已准备好用于 rc2-final,请试试这个。

虽然这不是您的问题(我怀疑@Tseng 已正确识别依赖性问题),但您可以安全地省略对 AddOptions() 的调用。

WebHostBuilder class 在对 Build() 的调用中隐式调用 AddOptions,如 GitHub 中所示。假设您正在以通常的方式构建 Web 应用程序,AddOptions 将已经在服务集合

上注册

请参阅此 link 以了解有关 RC1 的旧版本引用如何最有可能成为问题的更多信息。