StructureMap does not see types in ASP.NET Core .NET5

286 views Asked by At

I am creating sample project based on DDD.

  1. I created SharedKernel project where I have my class for DomainEvents
    public static class DomainEvents
    {
        public static IContainer Container { get; set; }

        static DomainEvents()
        {
            Container = StructureMap.Container.For<GenericTypesScanning>();
        }

        public static void Raise<T>(T args) where T : IDomainEvent
        {
            foreach (var handler in Container.GetAllInstances<IHandle<T>>())
            {
                handler.Handle(args);
            }
        }
     }

and this is class GenericTypesScanning

    public class GenericTypesScanning : Registry
    {
        public GenericTypesScanning()
        {
            Scan(scan =>
            {
                // 1. Declare which assemblies to scan
                scan.Assembly("MyLib");

                // 2. Built in registration conventions
                scan.AddAllTypesOf(typeof(IHandle<>));
                scan.WithDefaultConventions();

            });          

        }
    }
  1. In MyLib project I have class AppointmentConfirmedEvent and handler for this event:
    public class EmailConfirmationHandler: IHandle<AppointmentConfirmedEvent>
    {
        public void Handle(AppointmentConfirmedEvent appointmentConfirmedEvent)
        {
            // TBD
        }
    }
  1. I have temporary rest api controller where I wanted to check if everything is correctly registered and I am doing this:
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET: api/<ValuesController>
        [HttpGet]
        public IEnumerable<string> Get()
        {                        
            var appointmentConfirmedEvent = new AppointmentConfirmedEvent();
            DomainEvents.Raise(appointmentConfirmedEvent);

            return new string[] { "value1", "value2" };
        }
    }

but when DomainEvents.Raise is called the event is not handled because internal call Container.GetAllInstances<IHandle<T>>() returns empty array.

I did analogous example with Console app and there everything works fine. Any idea why it does not work in case of ASP.NET Core .NET 5 project?

-Jacek

2

There are 2 answers

0
Jeremy D. Miller On BEST ANSWER

The AddAllTypesOf() method does not work with open generics. See the ConnectImplementationsToTypesClosing() method in the StructureMap docs: http://structuremap.github.io/generics/

And just a reminder, StructureMap is no longer supported. Moreover, 2.6.4.1 was the "haunted" version of StructureMap that was admittedly buggy.

0
Jeremy D. Miller On

The first thing to do is to check out the type scanning diagnostics:

http://structuremap.github.io/diagnostics/type-scanning/.

The type scanning can be a little brittle if there are missing assemblies. The diagnostics might point out where things are going wrong. Also, try your WhatDoIHave() diagnostics too.

And also, just making sure that you know that StructureMap is no longer supported and has been replaced by Lamar: