StructureMap -> Lamar .NET Core 3.1 服务创建不起作用
StructureMap -> Lamar .NET Core 3.1 service creation not working
我正尝试在最新的 dotnet 核心版本 (3.1) 上从 StructureMap 迁移到 Lamar (4.1.0)
这个项目在切换到 Lamar 之前是有效的,但是发生了太多变化,我失去了联系。
有问题吗?
扫描发生后,如何使用已扫描对象的构造函数创建对象实例。我知道 StructureMap 有点不同,但这段代码以前有效。
代码如下:
Startup.cs(尝试新拉马尔)
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssembliesAndExecutablesFromApplicationBaseDirectory(a => a.FullName.Contains("Project.Name.Here"));
scanner.WithDefaultConventions();
scanner.SingleImplementationsOfInterface();
});
cfg.For<IServerInformationDataAccess>()
.Use(new ServerInformationDataAccess(Configuration.GetConnectionString(DbConnectionKey), Container.GetInstance<IMapper>()));
Startup.cs(旧的有效)
New Lamar (4.1) 不再允许我创建无参数
尝试了所有的实例化,但他们没有工作......所以我在这里
var container = new Container();
container.Configure(cfg =>
{
cfg.Scan(s =>
{
s.AssembliesAndExecutablesFromApplicationBaseDirectory(a => a.FullName.Contains("Project.Name.Here"));
s.WithDefaultConventions();
s.SingleImplementationsOfInterface();
});
cfg.For<IServerInformationDataAccess>()
.Use<ServerInformationDataAccess>(sida => new ServerInformationDataAccess(Configuration.GetConnectionString(DbConnectionKey), container.GetInstance<IMapper>()));
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseLamar()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
在这种情况下,Use
充当工厂代表
//...
cfg.For<IServerInformationDataAccess>()
.Use<ServerInformationDataAccess>(c => { //<-- c in this case is a container context
var connectionString = Configuration.GetConnectionString(DbConnectionKey);
var mapper = c.GetInstance<IMapper>();
return new ServerInformationDataAccess(connectionString, mapper);
});
//...
我正尝试在最新的 dotnet 核心版本 (3.1) 上从 StructureMap 迁移到 Lamar (4.1.0)
这个项目在切换到 Lamar 之前是有效的,但是发生了太多变化,我失去了联系。
有问题吗?
扫描发生后,如何使用已扫描对象的构造函数创建对象实例。我知道 StructureMap 有点不同,但这段代码以前有效。
代码如下:
Startup.cs(尝试新拉马尔)
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssembliesAndExecutablesFromApplicationBaseDirectory(a => a.FullName.Contains("Project.Name.Here"));
scanner.WithDefaultConventions();
scanner.SingleImplementationsOfInterface();
});
cfg.For<IServerInformationDataAccess>()
.Use(new ServerInformationDataAccess(Configuration.GetConnectionString(DbConnectionKey), Container.GetInstance<IMapper>()));
Startup.cs(旧的有效)
New Lamar (4.1) 不再允许我创建无参数
尝试了所有的实例化,但他们没有工作......所以我在这里
var container = new Container();
container.Configure(cfg =>
{
cfg.Scan(s =>
{
s.AssembliesAndExecutablesFromApplicationBaseDirectory(a => a.FullName.Contains("Project.Name.Here"));
s.WithDefaultConventions();
s.SingleImplementationsOfInterface();
});
cfg.For<IServerInformationDataAccess>()
.Use<ServerInformationDataAccess>(sida => new ServerInformationDataAccess(Configuration.GetConnectionString(DbConnectionKey), container.GetInstance<IMapper>()));
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseLamar()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
在这种情况下,Use
充当工厂代表
//...
cfg.For<IServerInformationDataAccess>()
.Use<ServerInformationDataAccess>(c => { //<-- c in this case is a container context
var connectionString = Configuration.GetConnectionString(DbConnectionKey);
var mapper = c.GetInstance<IMapper>();
return new ServerInformationDataAccess(connectionString, mapper);
});
//...