Search code examples
c#azuredependency-injection

Why is this "call is ambiguous between the following methods or properties" when calling ValidateOnStart?


Error CS0121 : The call is ambiguous between the following methods or properties: "Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions.ValidateOnStart(Microsoft.Extensions.Options.OptionsBuilder)" and "Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions.ValidateOnStart(Microsoft.Extensions.Options.OptionsBuilder)"

(For Reference: CS0121 )

Now, for the life of me, I cannot see the difference between

Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions.ValidateOnStart(Microsoft.Extensions.Options.OptionsBuilder)

and

Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions.ValidateOnStart(Microsoft.Extensions.Options.OptionsBuilder)

It is used like this:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace SomeNamespace;

internal static class ServiceCollectionExtensions
{
    public static IServiceCollection AddServiceBusMessaging(this IServiceCollection serviceCollection)
    {
        serviceCollection.AddOptions<SomeOptions>()
            .Configure<IConfiguration>((s, c) =>
                c.GetSection("SectionName").Bind(s))
            .ValidateDataAnnotations()
            .ValidateOnStart();

        // Some more AddXyz() ...

        return serviceCollection;
    }
}

And I also cannot seem to find an additional overload.

Dotnet Version in use is 9.0102

It is used in a Function App targetting net7.0, v4, isolated


Solution

  • Research led me to this post in a github issue:

    https://github.com/Azure/azure-sdk-for-net/issues/43206#issuecomment-2038403235

    [...] OptionsBuilderExtensions was moved from Microsoft.Extensions.Hosting into Microsoft.Extensions.Options. If you have a newer Microsoft.Extensions.Options and an older Microsoft.Extensions.Hosting you will run into this issue. Try doing this...

    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
    

    [...]

    (Emphasis by me)

    That fixed the issue for me.