.net.net-core.net-6.0

Why in extension method I don't have access to some other IServiceCollection extensions in .net Core?


In program.cs I can do

services.Configure<Blah>(configuration.GetSection("foo"));

But when I create an extension method for IServiceCollection there is no Configure method to use

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceExtensions
{
    public static IServiceCollection AddMyServices(this IServiceCollection services, IConfiguration config)
    {
        // I am getting an error in this line 'IServiceCollection does not contain a definition for Configure' 
        services.Configure<Blah>(config.GetSection("foo"));

        return services;
    }
}

What is the reason?


Solution

  • You need to add https://nuget.org/packages/Microsoft.Extensions.Options package in the project where your extension method is. In the WebApi project, this package is added behind the scenes.