aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Call to AddMvc is ambiguous #4128

Closed christiannagel closed 8 years ago

christiannagel commented 8 years ago

I'm getting the error that the call to AddMvc is ambiguous. The error doesn't really help as ambiguity is between the same types:

C:\Source\ProfessionalCSharp6\WebAPI\BooksServiceSample\src\BooksServiceSample\Startup.cs(31,22): error CS0121: The call is ambiguous between the following methods or properties: 'Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(Microsoft.Extensions.DependencyInjection.IServiceCollection)' and 'Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(Microsoft.Extensions.DependencyInjection.IServiceCollection)'

Did the package where MvcServiceCollectionExtensions is defined change, and I included the old and the new one? What else could be the reason?

davidfowl commented 8 years ago

Can you upload your sample project to github?

christiannagel commented 8 years ago

The BooksServiceSample project has this problem: https://github.com/ProfessionalCSharp/ProfessionalCSharp6/tree/master/WebAPI/BooksServiceSample/src/BooksServiceSample

Using dotnet tools 1.0.0-beta-001494 dotnet restore dotnet build

dotnet build shows this error.

kevinchalet commented 8 years ago

Your issue is likely caused by the fact you're using Swashbuckle (whose last public version targets RC1) with the latest ASP.NET Core packages. Sadly, you can't reference both RC1 and RC2 packages in the same project.

davidfowl commented 8 years ago

You need to update everything to RC2 (if that's even possible)

dbogatov commented 8 years ago

Hi all,

This issue is still happening to me. Here is the build output

Startup.cs(55,13): error CS0121: The call is ambiguous between the following methods or properties: 'Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(Microsoft.Extensions.DependencyInjection.IServiceCollection)' and 'Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(Microsoft.Extensions.DependencyInjection.IServiceCollection)'

Here are my usings for Startup.cs:

using System;  
using Microsoft.AspNetCore.Builder;  
using Microsoft.AspNetCore.Hosting;  
using Microsoft.Extensions.Configuration;  
using Microsoft.Extensions.Logging;  
using EShop.Services;  
using Microsoft.AspNetCore.Http;  
using System.IO;  
using Microsoft.Extensions.DependencyInjection  

I feel like it sees also Microsoft.AspNet (not Core) library, but how shall I fix it?

kevinchalet commented 8 years ago

I feel like it sees also Microsoft.AspNet (not Core) library, but how shall I fix it?

Maybe you can ping the author of the offending library and offer him some help to port it to ASP.NET Core RC2?

dbogatov commented 8 years ago

I don't think that this library is offending. It is a core component, I am sure it works. I just don't know why it does not work in my particular situation.

kevinchalet commented 8 years ago

AFAICT, there's no official version for RC2 on NuGet.org: https://www.nuget.org/packages/Swashbuckle/6.0.0-rc1-final

Trying to use a package relying on RC1 bits will bring incompatible packages in your project, explaining the error you're seeing.

dbogatov commented 8 years ago

I do not use Swashbuckle library (see my list of usings in Startup.cs).

kevinchalet commented 8 years ago

Many projects use the Microsoft.AspNetCore.Builder namespace for their IApplicationBuilder extensions to make them easier to find so I'm not sure it's really relevant :smile:

Instead, you should post your project.json. I bet there's a package referencing RC1 bits somewhere (it's easy to confirm by looking at the project.lock.json file)

dbogatov commented 8 years ago

It might be true... My project.lock.json is really large and I am not sure what it is and how to see anything from it.

However, let me post my project.json file. Thank you for your help!

{
    "userSecretsId": "aspnet5-EShop-58a1b196-f710-4cff-9654-b95ee66598f8",
    "version": "1.0.0-*",
    "dependencies": {
        "Microsoft.NETCore.Platforms": "1.0.1-rc2-*",
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview1-final",
        "EntityFramework7.Npgsql": "3.1.0-rc1-3",
        "EntityFramework7.Npgsql.Design": "3.1.0-*",
        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-rc2-final",
        "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0-rc2-final",
        "Microsoft.EntityFrameworkCore.Sqlite.Design": "1.0.0-rc2-final",
        "Microsoft.EntityFrameworkCore.InMemory": "1.0.0-rc2-final",
        "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
        "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-*",
        "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-*",
        "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-*",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-*",
        "Microsoft.AspNetCore.Mvc": "1.0.0-*",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
        "Microsoft.AspNetCore.Razor.Tools": "1.0.0-*",
        "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
        "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-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",
        "NetTelegramBotApi": "3.7.50926"
    },
    "frameworks": {
        "netcoreapp1.0": {
            "imports": [
                "dnxcore50",
                "portable-net45+win8"
            ]
        }
    },
    "runtimes": {
        "osx.10.11-x64": {},
        "ubuntu.14.04-x64": {},
        "win7-x64": {}
    },
    "runtimeOptions": {
        "gcServer": false,
        "gcConcurrent": true
    },
    "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true,
        "compile": {
            "exclude": [
                "wwwroot",
                "node_modules"
            ]
        }
    },
    "publishOptions": {
        "include": [
            "wwwroot",
            "Views"
        ],
        "exclude": [
            "**.user",
            "**.vspscc"
        ]
    },
    "scripts": {
        "prepublish": [
            "npm install",
            "bower install  --allow-root"
        ]
    }
}

P.S. It is the first time I am trying to migrate to RC2. Sorry for silly mistakes :)

kevinchalet commented 8 years ago

As expected, you're mixing RC1 and RC2 packages, which can't work. Try to update these packages:

"EntityFramework7.Npgsql": "3.1.0-rc1-3",
"EntityFramework7.Npgsql.Design": "3.1.0-*",
"Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
"Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final"
dbogatov commented 8 years ago

Thank you! I will try.

dbogatov commented 8 years ago

Let me confirm that your suggestion actually does fix the issue!

In case somebody needs it, here are the updated dependencies:

"dependencies": {
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*",
        "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0-*",
        "Npgsql.EntityFrameworkCore.PostgreSQL.Design": "1.0.0-*",
        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-*",
        "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0-*",
        "Microsoft.EntityFrameworkCore.Sqlite.Design": "1.0.0-*",
        "Microsoft.EntityFrameworkCore.InMemory": "1.0.0-*",
        "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-*",
        "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-*",
        "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-*",
        "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-*",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-*",
        "Microsoft.AspNetCore.Mvc": "1.0.0-*",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
        "Microsoft.AspNetCore.Razor.Tools": "1.0.0-*",
        "Microsoft.Extensions.Configuration.Json": "1.0.0-*",
        "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-*",
        "Microsoft.Extensions.Logging": "1.0.0-*",
        "Microsoft.Extensions.Logging.Console": "1.0.0-*",
        "Microsoft.Extensions.Logging.Debug": "1.0.0-*",
        "NetTelegramBotApi": "3.7.50926"
    }

@PinpointTownes thank you!

kevinchalet commented 8 years ago

You're welcome :clap:

nhtbtmz commented 8 years ago

If you use RC2 solution with nuget packages which are using RC1 you may get this error. I got this error with Kendo UI

pranavkm commented 8 years ago

@nhtbtmz we don't recommend against mixing packages from earlier releases with the current one.

sophisma commented 8 years ago

@nhtbtmz were you able to solve the ambiguity in AddMvc() because of kendo ui? I'm having the same problem, if I remove the reference to Kendo.Mvc form Project.json the ambiguity disappears.

nhtbtmz commented 8 years ago

@sophisma nope I couldn't. You should wait for kendo ui rc2 support discussion here http://www.telerik.com/forums/asp-net-core-1-0-rc2-support track progress here -> https://github.com/telerik/kendo-ui-core/issues/1682

sophisma commented 8 years ago

@nhtbtmz Thank you very much.