首页 > 解决方案 > 带有 ServiceRegistry 的 Lamar AddInstances

问题描述

我有一个用 Lamar 设置的 dotnet 核心网站,我在 Startup.cs 中有以下方法

public void ConfigureContainer(ServiceRegistry services)
{
  ...
}

我想按照https://jasperfx.github.io/lamar/documentation/ioc/registration/registry-dsl/上的文档中所述使用 AddInstances()

使用 For().AddInstances() 添加许多注册

// registry is a StructureMap Registry object
registry.For<IService>().AddInstances(x =>
{
    // Equivalent to For<IService>().Add<ColorService>().....
    x.Type<ColorService>().Named("Red").Ctor<string>("color").Is("Red");

    // Equivalent to For<IService>().Add(new ColorService("Yellow"))......
    x.Object(new ColorService("Yellow")).Named("Yellow");

    // Equivalent to For<IService>().Use(() => new ColorService("Purple"))....
    x.ConstructedBy(() => new ColorService("Purple")).Named("Purple");

    x.Type<ColorService>().Named("Decorated").Ctor<string>("color").Is("Orange");
});

但是, AddInstances() 不存在,我收到以下错误

“ServiceRegistry.InstanceExpression”不包含“AddInstances”的定义,并且找不到接受“ServiceRegistry.InstanceExpression”类型的第一个参数的可访问扩展方法“AddInstances”(您是否缺少 using 指令或程序集引用?)

services.For<IService>().AddInstances(x =>
{
 ...

 });

这是在另一个命名空间中吗?根据示例,它是否仅存在于 StructureMap Registry 对象中,如果是,我如何从注入的 ServiceRegistry 中获取它?

标签: c#asp.net-coredependency-injectionioc-containerlamar

解决方案


我猜该方法已被弃用,因为您可以简单地做......

services.For<IService>().Add<Service1>().Named("1");
services.For<IService>().Add<Service2>().Named("2");

推荐阅读