0

I have come into an already-written app that uses Dependency Injection and I've successfully added to one of the existing projects in the solution, now trying to add a NEW project and can't really seem to get things figured out.

Because it's a CONSOLE app and not an MVC app, I'm really thrown with how to setup the Program.cs to work.

Basically, I need to know how to use the ProjectSearchManager (via IProjectSearchManager) from a console app.

These are the relevant (I think) sections of my Startup.cs...

public class Startup
    {

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DataConnection")));

            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

            ConfigureRepositories(services);
            ConfigureManagers(services);
           
        }

        private static void ConfigureRepositories(IServiceCollection services)
        {
            services.AddScoped<IAdoRepository, AdoRepository>();
            services.AddScoped<IRepository<ProjectSearchResults>, Repository<ProjectSearchResults>>();
        }

        private static void ConfigureManagers(IServiceCollection service)
        {
            service.AddScoped<IProjectSearchManager, ProjectSearchManager>();
        }

    }

What is the syntax that works to do something similar in Program.cs?

Currently I have this...

    public class Program
    {
        public static void Main(string[] args)
        {
            var services = new ServiceCollection();
            ConfigureRepositories(services);
            ConfigureManagers(services);

            // NOW I WANT TO RUN METHODS INSIDE ProjectSearchManager...
            // HOW?

        }
        
        public static void ConfigureRepositories(IServiceCollection services)
        {
            services.AddScoped<IAdoRepository, AdoRepository>();
            services.AddScoped<IRepository<ProjectSearchManager>, Repository<ProjectSearchManager>>();
        }
        public static void ConfigureManagers(IServiceCollection service)
        {
            service.AddScoped<IProjectSearchManager, ProjectSearchManager>();
        }
    }

And the code seems to run but I can't access the methods inside ProjectSearchManager... I can't figure out what I'm missing or where I'm going wrong.

This may be very basic dependency injection stuff, I don't know. I was able to get it to work in MVC easily but in the console app, seems like the setup is more challenging or something.

Any help greatly appreciated, thanks!

UPDATE...

I reviewed the post at --> How to run .NET Core Console app using generic host builder

I tried updating the code to read as such...

            var host = Host.CreateDefaultBuilder(args)
                .ConfigureServices(services => { services.AddScoped<ProjectSearchManager>(); })
                .Build();

            var my = host.Services.GetRequiredService<ProjectSearchManager>();
            my.SearchAsync();

I get the following error... Unable to resolve service for type 'Solution.DataLayer.Abstracts.IRepository`1[Solution.DataLayer.Entities.ProjectSearchResults]' while attempting to activate 'Solution.Managers.ProjectSearchManager'.

gotmike
  • 1,515
  • 4
  • 20
  • 44
  • 1
    You need to use `IHost`, which handles the `ServiceCollection` and service container for you. See here: https://stackoverflow.com/questions/68392429/how-to-run-net-core-console-app-using-generic-host-builder – Dai Apr 08 '22 at 16:13
  • 1
    That error *should* be plainly obvious to you what it means. It's telling you that you haven't registered something to resolve the dependency on IRepository that ProjectSearchManager class depends on. It's that easy. All you need to do is add something to the service collection to resolve that dependency, just like you did with `services.AddScoped, Repository>();` – mason Apr 08 '22 at 17:24
  • @mason -- i hear u. i'm sure it's simple but i have been staring at this for a week and having trouble with what i'm sure is an easy solution. i can't seem to figure out what the `something` part is... – gotmike Apr 08 '22 at 17:27
  • 1
    Look at what you did for the `Repository`. Now find the equivalent class for `Repository`. Rinse, wash, repeat. Do you understand what that services.AddScoped method call does? Why it's necessary? – mason Apr 08 '22 at 17:29
  • and if i update that, i now get... `No service for type 'Solution.Managers.ProjectSearchManager' has been registered.` – gotmike Apr 08 '22 at 17:35
  • 1
    Your code directly depends on ProjectSearchManager? However, you haven't registered anything to resolve a dependency on ProjectSearchManager, only on IProjectSearchManager. Therefore, you must either change how you register that dependency (not recommended) or instead start depending on IIProjectSearchManager. Change `var my = host.Services.GetRequiredService();` to `var my = host.Services.GetRequiredService();` You may then run into an error because you registered it as a Scoped, but you're not in a scope where you're trying to resolve it. – mason Apr 08 '22 at 18:02
  • Probably all your AddScoped calls should be AddTransient. – mason Apr 08 '22 at 18:04

0 Answers0