seesharper / LightInject

An ultra lightweight IoC container
http://www.lightinject.net
MIT License
619 stars 121 forks source link

Invalid operation exception on simple test #52

Closed Mingism closed 10 years ago

Mingism commented 10 years ago

I am trying to run a simple test to see whether LightInject (NuGet package version 3.0.1.2) would be an appropriate solution for me, but I am getting an error on a basic test:

An unhandled exception of type 'System.InvalidOperationException' occurred in LightInject.dll

Additional information: Unable to resolve type: IoCContainer_Performance_Test.Program+Foobar, service name:

My code:

 public class Foobar
    {
        Dependency _dependency;
        public Foobar(Dependency dependecy)
        {
            _dependency = dependecy;
        }
    }

    public class Dependency
    {
        string test;

        public string Test
        {
            get { return test; }
            set { test = value; }
        }
    }

    static void LightInject()  
    {
        // 1. Create a new  container
        var container = new LightInject.ServiceContainer();

        // 2. Register type(s) to the container 
        container.Register<Foobar>(); 

        for (int i = 0; i < 10000; i++)
        {
            Foobar store =  container.GetInstance<Foobar>();//
        }
    }

Am I missing something in order to make it work properly?

seesharper commented 10 years ago

Hi Kay!

You need to register the "Dependency" class as well.

container.Register<Foobar>();
container.Register<Dependency>();

Best regards

Bernhard Richter

On Sun, Dec 15, 2013 at 2:50 PM, Kay Tsar notifications@github.com wrote:

I am trying to run a simple test to see whether LightInject (NuGet package version 3.0.1.2) would be an appropriate solution for me, but I am getting an error on a basic test:

An unhandled exception of type 'System.InvalidOperationException' occurred in LightInject.dll

Additional information: Unable to resolve type: IoCContainer_Performance_Test.Program+Foobar, service name:

My code:

public class Foobar { Dependency _dependency; public Foobar(Dependency dependecy) { _dependency = dependecy; } }

public class Dependency
{
    string test;

    public string Test
    {
        get { return test; }
        set { test = value; }
    }
}

static void LightInject()
{
    // 1. Create a new  container
    var container = new LightInject.ServiceContainer();

    // 2. Register type(s) to the container
    container.Register<Foobar>();

    for (int i = 0; i < 10000; i++)
    {
        Foobar store =  container.GetInstance<Foobar>();//
    }
}

Am I missing something in order to make it work properly?

— Reply to this email directly or view it on GitHubhttps://github.com/seesharper/LightInject/issues/52 .

Mingism commented 10 years ago

You are absolutely right. Just out of interest, would you ever implement some kind of auto register of dependencies?

seesharper commented 10 years ago

That is already implemented. You can register service by scanning one or many assemblies.

container.RegisterAssembly(typeof(Program).Assembly);

Best regards

Bernhard Richter

On Sun, Dec 15, 2013 at 3:36 PM, Kay Tsar notifications@github.com wrote:

You are absolutely right. Just out of interest, would you ever implement some kind of auto register of dependencies?

— Reply to this email directly or view it on GitHubhttps://github.com/seesharper/LightInject/issues/52#issuecomment-30607938 .

Mingism commented 10 years ago

'Nuff said. I'll start reading the docs :+1:

seesharper commented 10 years ago

:)

Sent from my iPhone

On 15. des. 2013, at 15:41, Kay Tsar notifications@github.com wrote:

'Nuff said. I'll start reading the docs [image: :+1:]

— Reply to this email directly or view it on GitHubhttps://github.com/seesharper/LightInject/issues/52#issuecomment-30608386 .