C# Specflow-使用.NET Core中的Microsoft依赖项注入导致BoDi.ObjectContainerException
我目前正在尝试使用Speflow设置一个新的.NET核心项目,并将默认的Microsoft依赖项注入连接到该项目 在研究了文档和/实现之后,我尝试实现我自己的运行时插件“SpecFlow.MSDependencyInjection.SpecFlowPlugin” 现在,我正在努力解决特定于Specflow的基础设施,比如ScenarioContextC# Specflow-使用.NET Core中的Microsoft依赖项注入导致BoDi.ObjectContainerException,c#,dependency-injection,.net-core,specflow,C#,Dependency Injection,.net Core,Specflow,我目前正在尝试使用Speflow设置一个新的.NET核心项目,并将默认的Microsoft依赖项注入连接到该项目 在研究了文档和/实现之后,我尝试实现我自己的运行时插件“SpecFlow.MSDependencyInjection.SpecFlowPlugin” 现在,我正在努力解决特定于Specflow的基础设施,比如ScenarioContext public static class DependencyInjectionExtension { privat
public static class DependencyInjectionExtension
{
private static void RegisterSpecflowDependecies(IObjectContainer objectContainer, ServiceCollection serviceCollection)
{
serviceCollection.AddSingleton(objectContainer);
serviceCollection.AddSingleton((x) =>
{
var specflowContainer = x.GetRequiredService<IObjectContainer>();
var scenarioContext = specflowContainer.Resolve<ScenarioContext>();
return scenarioContext;
});
serviceCollection.AddSingleton((x) =>
{
var specflowContainer = x.GetRequiredService<IObjectContainer>();
var scenarioContext = specflowContainer.Resolve<FeatureContext>();
return scenarioContext;
});
serviceCollection.AddSingleton((x) =>
{
var specflowContainer = x.GetRequiredService<IObjectContainer>();
var scenarioContext = specflowContainer.Resolve<TestThreadContext>();
return scenarioContext;
});
}
[ScenarioDependencies]
public static IServiceProvider CreateServiceCollection(IObjectContainer objectContainer)
{
var serviceCollection = new ServiceCollection();
RegisterSpecflowDependecies(objectContainer, serviceCollection);
var bindingAttributeClasses = typeof(DependencyInjectionExtension).Assembly.GetTypes()
.Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))).ToArray();
foreach (var bindingAttributeClass in bindingAttributeClasses)
{
serviceCollection.AddSingleton(bindingAttributeClass);
}
return serviceCollection.BuildServiceProvider();
}
}
RegisterSpecFlowDependences的代码与实现非常相似。所以我不明白为什么它在我的实现中不起作用。似乎其中一个构造函数有一个字符串参数 您可以将该构造函数包装到另一个类中,该类以另一种方式获取参数:
class MyWrapper : ClassThatHasAStringParameter
{
public MyWrapper()
: base("PARAMETER_VALUE")
{
}
}
据我所知,Specflow处理自己的依赖关系。因此,我不确定必须在ScenarioContext中插入哪些值,因为它是Specflow依赖项。这就是为什么我使用特定的Specflow Objectcontainer来解析它。至少我是这么想的。。。长话短说,我不知道什么字符串是预期的。
class MyWrapper : ClassThatHasAStringParameter
{
public MyWrapper()
: base("PARAMETER_VALUE")
{
}
}