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

我正在创建基于 DDD 的示例项目。

  1. 我创建了 SharedKernel 项目,其中有我的 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);
            }
        }
     }

这是类 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. MyLib 项目中,我有此事件的类 AppointmentConfirmedEvent 和处理程序:
    public class EmailConfirmationHandler: IHandle<AppointmentConfirmedEvent>
    {
        public void Handle(AppointmentConfirmedEvent appointmentConfirmedEvent)
        {
            // TBD
        }
    }
  1. 我有一个临时的 rest api 控制器,我想在那里检查一切是否都正确注册,我正在这样做:
    [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" };
        }
    }

但是当调用 DomainEvents.Raise 时,不会处理事件,因为内部调用 Container.GetallInstances<IHandle<T>>() 返回空数组。

我用控制台应用程序做了类似的例子,一切正常。知道为什么它在 ASP.NET Core .NET 5 项目中不起作用吗?

-杰克

h402482070 回答:StructureMap 在 ASP.NET Core .NET5 中看不到类型

首先要做的是检查类型扫描诊断:

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

如果缺少程序集,类型扫描可能会有点脆弱。诊断程序可能会指出哪里出错了。另外,也试试你的 WhatDoIHave() 诊断。

此外,只需确保您知道 StructureMap 不再受支持并已被 Lamar 取代:

,

嗯,很奇怪,我使用了 WhatDidIScanWhatDoIHave 方法,报告中的一切看起来都很好,但我仍然无法从容器中获取这些数据(在屏幕上您可以看到有 0返回数组中的元素)

problem-with-di

,

AddAllTypesOf() 方法不适用于开放泛型。请参阅 StructureMap 文档中的 ConnectImplementationsToTypesClosing() 方法:http://structuremap.github.io/generics/

提醒一下,不再支持 StructureMap。此外,2.6.4.1 是 StructureMap 的“闹鬼”版本,公认存在缺陷。

本文链接:https://www.f2er.com/799404.html

大家都在问