Home > other >  The call is ambiguous between the following methods or properties during migration from Net Core 3.1
The call is ambiguous between the following methods or properties during migration from Net Core 3.1

Time:08-09

I am trying to migrate from .Net Core 3.1 to .Net 6. I updated TargetFramework and some dependencies.

And I have this piece of code

public CustomerSettingsDefaults(Microsoft.Extensions.Configuration.IConfigurationSection configuration)
{
  ....
  var personalizedUrls = configuration.GetRequiredSection(@"PersonalizedUrls");

However during project build it

Severity Code Description Project File Line Suppression State Error CS0121 The call is ambiguous between the following methods or properties: 'Microsoft.Extensions.Configuration.ConfigurationExtensions.GetRequiredSection(Microsoft.Extensions.Configuration.IConfiguration, string)' and 'Common.Extensions.ConfigurationExtensions.GetRequiredSection(Microsoft.Extensions.Configuration.IConfiguration, string)' Crm.Infrastructure C:...\Configuration\CustomerSettingsDefaults.cs 23 Active

My ConfigurationExtensions is defined like this

namespace Common.Extensions
{
    public static class ConfigurationExtensions
    {
        public static IConfigurationSection GetRequiredSection(this IConfiguration configuration, string sectionName)
        {
            var section = configuration.GetSection(sectionName);
            if (section is null)
            {
                throw new InvalidOperationException($@"The configuration is missing the {sectionName} section");
            }

            return section;
        }
    }
}

How to fix it?

CodePudding user response:

Well, there's a conflict between your own GetRequiredSection extension method and the GetRequiredSection that was added to Microsoft.Extensions.Configuration.Abstractions in .NET 6. Both extend the same interface and take the same parameters, so if both namespaces (your Common.Extensions and .NET's Microsoft.Extensions.Configuration) are in scope, of course the compiler doesn't know which one to pick.

So you could just delete your own extension method altogether, since it seems to do the same thing that the one provided by .NET does, and that means one less thing to maintain for you.

If you absolutely need to keep using your own method, then you need to call it explicitly like a static method:

  var personalizedUrls = Common.Extensions.ConfigurationExtensions.GetRequiredSection(configuration, "PersonalizedUrls");

CodePudding user response:

'Call is ambiguous' error is always for those function calls for which there are 2 similar function in the code in which neither functions are either overloading or overriding the other function. To get rid of the error and you want to call a specific function, either you can call out the whole function i.e., instead of calling GetRequiredSection(), call Common.Extensions.ConfigurationExtensions.GetRequiredSection()

  • Related