简体   繁体   English

StructureMap 在 ASP.NET Core .NET5 中看不到类型

[英]StructureMap does not see types in ASP.NET Core .NET5

I am creating sample project based on DDD.我正在创建基于 DDD 的示例项目。

  1. I created SharedKernel project where I have my class for DomainEvents我创建了 SharedKernel 项目,其中有我的 class 用于 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这是 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:MyLib项目中,我有 class AppointmentConfirmedEvent和此事件的处理程序:
    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:我有临时 rest api controller 我想检查所有内容是否正确注册,我正在这样做:
    [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.但是当DomainEvents.Raise时,不会处理该事件,因为内部调用Container.GetAllInstances<IHandle<T>>()返回空数组。

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?知道为什么它在 ASP.NET Core .NET 5 项目的情况下不起作用吗?

-Jacek -雅切克

The first thing to do is to check out the type scanning diagnostics:首先要做的是检查类型扫描诊断:

http://structuremap.github.io/diagnostics/type-scanning/ . 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.另外,也试试你的WhatDoIHave()诊断。

And also, just making sure that you know that StructureMap is no longer supported and has been replaced by Lamar:而且,只要确保您知道不再支持 StructureMap 并已被 Lamar 取代:

hm it is strange, I used methods WhatDidIScan and WhatDoIHave and everything looks fine in the reports but I still cannot get these data from the container (on the screen you can see that there is 0 elements in the returned arrays)嗯,这很奇怪,我使用WhatDidIScanWhatDoIHave方法,报告中的一切看起来都很好,但我仍然无法从容器中获取这些数据(在屏幕上你可以看到返回的数组中有 0 个元素)

problem-with-di问题与-di

The AddAllTypesOf() method does not work with open generics. AddAllTypesOf()方法不适用于打开的 generics。 See the ConnectImplementationsToTypesClosing() method in the StructureMap docs: http://structuremap.github.io/generics/请参阅 StructureMap 文档中的ConnectImplementationsToTypesClosing()方法: http://structuremap.github.io/generics/

And just a reminder, StructureMap is no longer supported.提醒一下,不再支持 StructureMap。 Moreover, 2.6.4.1 was the "haunted" version of StructureMap that was admittedly buggy.此外,2.6.4.1 是 StructureMap 的“闹鬼”版本,它无疑是有缺陷的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM