Skip to content

Console Application example with DI #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ngonzalezromero opened this issue Aug 18, 2015 · 5 comments
Closed

Console Application example with DI #849

ngonzalezromero opened this issue Aug 18, 2015 · 5 comments

Comments

@ngonzalezromero
Copy link

Hi
I have this code

 public class Program    {
    public IConfiguration Configuration { get; set; }
    public IServiceCollection Collection { get; set; }

    public Program(IApplicationEnvironment app)
    {
        Configuration = new ConfigurationBuilder()
        .AddJsonFile($"{app.ApplicationBasePath}/config.json")
        .AddEnvironmentVariables()
        .Build();

    }

    public void Main(string[] args)
    {

        var service = new ServiceCollection();
        service.AddTransient<IVirtualScreen, VirtualScreen>();
        Console.WriteLine("Test");

        Console.ReadKey();
    }
      }

}

But, i don't how i should implement DI in another class

Regards

@davidfowl
Copy link
Member

But, i don't how i should implement DI in another class?

What does that mean.

@ghost
Copy link

ghost commented Aug 23, 2015

Perhaps the following is what you are looking for? It displays "Hello World" to console, it uses constructor injection in the NoFooHere class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Framework.DependencyInjection;

namespace ConsolePrototype
{
    public class Program
    {
        public void Main(string[] args)
        {
            var services = new ServiceCollection();

            // Add our interfaces/implementation to collection
            services
                .AddTransient<IFoo, Foo>()
                .AddSingleton<INoFoo, NoFooHere>();

            // Build our service provider from collection
            var serviceProvider = services.BuildServiceProvider();

            // Resolve our interface - uses constructor injection
            var foo = serviceProvider.GetService<INoFoo>();

            Console.WriteLine(foo.Content);
        }

        public interface INoFoo {  string Content { get; set; } }
        public class NoFooHere : INoFoo
        {
            public string Content { get; set; }
            public NoFooHere(IFoo foo)
            {
                Content = foo.GetFoo();
            }
        }

        public interface IFoo  {  string GetFoo(); }
        public class Foo : IFoo
        {
            public string GetFoo()
            {
                return "Hello World";
            }                
        }
    }
}

@aspnet-hello
Copy link

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

@davidalpert
Copy link

davidalpert commented Dec 31, 2017 via email

@davidalpert
Copy link

davidalpert commented Dec 31, 2017 via email

ryanbrandenburg pushed a commit that referenced this issue Nov 22, 2018
…/2.2-to-release/2.2

[automated] Merge branch 'maestro/release/2.2' => 'release/2.2'
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants