0

The following documentation is not working for multiple reasons:

https://jasperfx.github.io/lamar/documentation/ioc/resolving/requesting-a-concrete-type/

First the documentation states that you can new up a Container with a default constructor which is not true. Secondly the following is not working for me.

var container = new Container(registry => { });
var testClass = container.GetInstance<TestClass>();

public class TestClass {}

Here's the error and stack trace from

var testClass = container.GetInstance<TestClass>();

Lamar.IoC.LamarMissingRegistrationException : No service registrations exist or can be derived for netcore.tests.web.UnitTests.ExtensionMethods.IServiceCollectionExtensionsTests.When_creating_a_new_instance_after_applying_the_AddTransientForAll_rule.TestClass at Lamar.IoC.Scope.GetInstance(Type serviceType) at Lamar.IoC.Scope.GetInstanceT at netcore.tests.web.UnitTests.ExtensionMethods.IServiceCollectionExtensionsTests.When_creating_a_new_instance_after_applying_the_AddTransientForAll_rule..ctor()

Any suggestions on how to get concrete types to auto resolve would be appreciated.

Gary Brunton
  • 1,840
  • 1
  • 22
  • 33

2 Answers2

0

Based on the source code, things have changed since that documentation.

Try using the constructor that takes a service collection

var services = new ServiceCollection();
var container = new Container(services);
var testClass = container.GetInstance<TestClass>();
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thank you for the suggestion but it's still not working. – Gary Brunton Feb 11 '20 at 23:49
  • @GaryBrunton Just to test a theory, does it work if you register `TestClass` in the collection first? – Nkosi Feb 11 '20 at 23:54
  • Registering it like this works services.AddTransient(); – Gary Brunton Feb 11 '20 at 23:57
  • @GaryBrunton I'll dig up in the source to see if it actually still auto resolves concrete classes but so far it looks like that it a no go. – Nkosi Feb 11 '20 at 23:58
  • I was starting to think (and maybe still am) that I was doing something completely wrong here. I found some tests in the source that appeared to be testing this but never ran them and didn't completely follow them. – Gary Brunton Feb 12 '20 at 00:01
0

I got this to work, was bugging me all night:

using Lamar;

namespace ConsoleApp2
{
    public class TestClass {}

    class Program
    {
        static void Main(string[] args)
        {
            var container = new Container(_ => { });

            var testClass = container.GetInstance<TestClass>();
        }
    }
}

This does not work:

class Program
{
    static void Main(string[] args)
    {
        var container = new Container(_ => { });

        var testClass = container.GetInstance<TestClass>();
    }

    public class TestClass {}
}
mxmissile
  • 11,464
  • 3
  • 53
  • 79
  • @mxmissle I created a bug for this that was closed because it couldn't be replicated. You might want to follow up with this there if you think there's an issue. I've since moved on from lamar: https://github.com/JasperFx/lamar/issues/235 – Gary Brunton Apr 22 '20 at 19:34
  • I did just that: https://github.com/JasperFx/lamar/issues/250 – mxmissile Apr 22 '20 at 20:34