Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在IDependencyResolver中获取当前WebAPI请求_C#_Asp.net Web Api_Dependency Injection_Structuremap - Fatal编程技术网

C# 正在IDependencyResolver中获取当前WebAPI请求

C# 正在IDependencyResolver中获取当前WebAPI请求,c#,asp.net-web-api,dependency-injection,structuremap,C#,Asp.net Web Api,Dependency Injection,Structuremap,我正在使用StructureMap作为WebAPI项目中的IoC容器。我希望根据请求中的值(查询字符串或http头)提供某些依赖项的不同实现 StructureMap有配置文件的概念,这是绝对完美的,我可以像这样覆盖依赖项: For<ISomeDependency>().Use<StandardImplementation>(); Profile("AlternateProfile", pr => { pr.For<ISomeDependency>

我正在使用
StructureMap
作为WebAPI项目中的IoC容器。我希望根据请求中的值(查询字符串或http头)提供某些依赖项的不同实现

StructureMap有配置文件的概念,这是绝对完美的,我可以像这样覆盖依赖项:

For<ISomeDependency>().Use<StandardImplementation>();
Profile("AlternateProfile", pr =>
{
   pr.For<ISomeDependency>().Use<AlternateImplementation>();
});
WebAPI中的DI设置看起来是半生不熟的,但我希望我错过了一些愚蠢的东西


注意:我使用的是WebAPI自托管,因此无法使用如下解决方法:

请参阅。另外,请看一下,使用策略模式结合具有不同依赖项列表的抽象工厂,寻找实现相同目标的替代方法。感谢@NightOwl888-我希望避免将工厂注入控制器。需要这样做似乎很愚蠢。我确实找到了一种方法,重新实现了一些WebAPI类,但这也不是很好。有趣的是,对我来说,尝试使用DI容器解析运行时引用似乎很愚蠢,因为DI容器是为解决问题而设计的。我以前的建议的另一个替代方法是,让您实际使用容器来解析引用。@NightOwl888够公平的,我后悔使用了“愚蠢”这个词。在我的场景中,使用工厂函数可能是最简单的方法。感谢您的意见。
public IDependencyScope BeginScope()
{
    var request = ??!?
    if (/* Check an HTTP Header for the request or something here? */)
        child = _container.GetNestedContainer("AlternateProfile");
    else
        child = _container.GetNestedContainer();
    return new WebApiStructureMapDependencyResolver(child);
}