'Dependency Injection in Startup.cs difficulties

I am trying to inject some Data into the Startup.cs file without success. For example, this injection works fine on my Email Class and I am able to read the _data

public class EmailSender : IEmailSender
{
        private readonly DataConfig _data;
        private readonly UserManager<ApplicationUser> _signManager;

        public EmailSender(UserManager<ApplicationUser> signManager, IOptions<DataConfig> data)
        {
            _data = data.Value;
            _signManager = signManager;

        }

 //  I am able to retrieve the _data.SG without issues from the SendEmailAsync method.

        public Task SendEmailAsync(string email, string subject, string message)
        {
            var sendGridKey = _data.SG;
            return Execute(sendGridKey, subject, message, email);
        }


}

However, when I try to perform the same operation from Startup.cs, the Program.cs crashes:

public class Startup
{
        public IConfiguration Configuration { get; }
        private readonly DataConfig _data;

        public Startup(IConfiguration configuration, IOptions<DataConfig> data)
        {
            Configuration = configuration;
            _data = data.Value;
        }
}

Then, Program.cs crashes :

An error occurred while starting the application.
InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Options.IOptions`1[JSRoles.Services.DataConfig]' while attempting to activate 'JSRoles.Startup'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)

InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Options.IOptions`1[JSRoles.Services.DataConfig]' while attempting to activate 'JSRoles.Startup'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters)
Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
Microsoft.AspNetCore.Hosting.GenericWebHostBuilder+<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
Microsoft.Extensions.Hosting.HostBuilder.Build()
JSRoles.Program.Main(string[] args) in Program.cs
-
namespace JSRoles
{
    public class Program
    {
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService<ILoggerFactory>();
                try
                {
JSRoles.Program.<Main>(string[] args)

How do I get around this issue, and still manage to get the data that I need in StartUp.cs?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source