简体   繁体   中英

ASP.Net 5: Updating 1.0 Beta8 to RC1 - IServiceCollection does not contain definition for AddMvc

I have some code composed of an authorization api project,a common project and an ASP.net main application project, in a single solution. I have gone through the related project.json files and removed explicit beta8 references, and then made some other required corrections. For example, changing using Microsoft.Framework.Configuration to Microsoft.Extensions.Configuration, where necessary.

I understand from the linked potential duplicate that the first thing to suspect is an invalid mixture of references in my various project.json files. I have no beta8 references left anywhere, explicitly. Thus I do not think that the linked issue is the same.

Where I'm stuck seems like a really basic thing:

Error   CS1061  Build   'IServiceCollection' does not contain a
definition for 'AddMvc' and no extension method 'AddMvc' accepting a first 
argument of type 'IServiceCollection' could be found (are you missing a 
using directive or an assembly reference?)  

I have Microsoft.AspNet.Mvc referenced in the project.json, and it doesn't matter if I put "using Microsoft.AspNet.mvc" in the .cs file or not the error persists. I thought you add references in the new world order, just by adding them to project.json. If an assembly reference exists in the project.json, why does this error still occur?

My project.json:

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {

    "Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-*",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*",
    "Microsoft.AspNet.Mvc": "6.0.0-*",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
    "Microsoft.AspNet.StaticFiles": "1.0.0-*",
    "Microsoft.Framework.Logging": "1.0.0-*",
    "Microsoft.Framework.Logging.Console": "1.0.0-*",
    "Microsoft.Framework.Logging.Debug": "1.0.0-*",
    "System.IdentityModel.Tokens": "5.0.0-*",
    "MyCompany.Common": "1.0.0-*"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

The line of code that's breaking is the AddMvc one:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Dnx.Runtime;
using RamSoft.Authorization.Api.Common;
using System.IdentityModel.Tokens;
using Microsoft.AspNet.Authentication.JwtBearer;
using RamSoft.ITConsole.Common;
using Microsoft.Extensions.PlatformAbstractions;
namespace MyCompany.Authorization.Api
{
    public class Startup
    {
     ...
 public void ConfigureServices(IServiceCollection services)
        {
            services.AddInstance(_signingCredentials);
            services.AddInstance(_jwtBearerOptions);

            services.AddMvc();  // Fail.

        }
   ...
}

You need to change dependencies on your project. here is my project.json

"dependencies": {
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    }
  }

and adding using Microsoft.Extensions.Configuration; in startup.cs

we can use AddMvc() atleast working for Core 1.0 rc2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM