Tag: unity container

注入包含所有已注册的接口实现的Enumerable

给定以下界面: public interface IMyProcessor { void Process(); } 我希望能够注册多个实现并让我的DI容器将其中的可枚举注入到这样的类中: public class MyProcessorLibrary { private readonly IMyProcessor[] _processors; public MyProcessingThing(IMyProcessor[] processors) { this._processors = processors; } public void ProcessAll() { foreach (var processor in this._processors) { processor.Process(); } } } 这可能吗? 我当前的IMyProcessor实现静态查找所有IMyProcessor实现,但如果可以,我宁愿通过容器来实现它。 我正在使用Unity,但我很好奇其他容器是否支持它。 编辑: 谢谢你到目前为止的答案; 要清楚我想将MyProcessorLibrary注入另一个类,并将其构建为依赖对象树的连接的一部分,例如 public class MyProcessorRepository { public MyProcessorRepository(MyProcessorLibrary processorLibrary) { } } […]

IUserStore类型`1没有可访问的构造函数

我想用Unity 3设置MVC5应用程序。我从标准模板创建了一个默认的web mvc5应用程序,然后添加了unity 当我在AccountController中访问Register操作时,我得到以下exception: IUserStore类型`1没有可访问的构造函数。 从这篇文章如何将MVC 5身份validation添加到Unity IoC? 我知道问题是Unity选择具有更长参数列表的构造函数。 解决方案是通过以下方式注册要与默认构造函数一起使用的Account控制器: container.RegisterType(new InjectionConstructor()); 我想要做的是在配置文件中注册no代码中是否可以在web.config中执行相同的操作? 最诚挚的问候,塞巴斯蒂安

Unity Container Resolve

我刚开始使用Unity Container,我的注册看起来像这样: static void UnityRegister() { _container = new UnityContainer(); _container.RegisterType(); _container.RegisterType(“Book”); _container.RegisterType(); _container.RegisterType(“Database”); } 现在当我尝试解决这个问题: var service = _container.Resolve(“Database”); 我收到以下错误: 解析依赖关系失败,type =“UnityConsoleEx.IBookService”,name =“Database”。 在解决时发生exception: 例外情况是:InvalidOperationException – 当前类型UnityConsoleEx.IBookService是一个接口,无法构造。 你错过了类型映射吗? At the time of the exception, the container was: Resolving UnityConsoleEx.IBookService,Database 谁能指出我做错了什么?

有条件地解决Unity中的命名实现

Unity允许您命名同一接口的不同实现,然后按名称解析它们: var container = new UnityContainer(); // register container.Register(“One”); container.Register(“Two”); // resolve var twoFish = container.Resolve(“Two”); 现在假设我有一个依赖于IFish并实现ITank的类: class Tank : ITank { public Tank(IFish fish) {…} } 我如何解决ITank并指定要获得哪些IFish实现? 这不起作用: container.Register(); var tank = container.Resolve(“One”); 这有效: var fish = container.Resolve(“One”); var tank = container.Resolve(new DependencyOverride(typeof(IFish), fish); 但它只处理简单的情况(例如在这个例子中),而不是可能有许多名为“One”的实现的一般情况。 我想要的是能够告诉Unity: “在解析名为”One“的使用实现时,如果没有这样的实现,则寄存器将回退到未命名的实现” 是否有可以通过此行为插入Unity的自定义解析程序?

Unity解决循环依赖

在学习Unity(C#中的DI框架)的过程中,我遇到了一个类具有ClassB的setter注入的情况 class ClassA : IClassA { … [Dependency] public IClassB ClassB { get { return _classB; } set { if (value == null) throw new ArgumentNullException(“value”); _classB = value; } } 另一个有ClassA的构造函数注入 class ClassB : IClassB { … [InjectionConstructor] public ClassB(IClassA classA) { _classA = classA; } } 我无法在容器中正确解析这两个类。 var container = new UnityContainer(); container.RegisterType(); […]

使用Unity 2和MVC 3将类注入Authentication属性

我读了很多文章,指出如何做到这一点,即: 堆栈溢出解决方案 Brad Wilsons出色的教程 这些看起来效果很好,但是当我遵循这里的一些指导原则时 保护ASP.NET MVC 3应用程序 我好像来了一个收割机。 对我来说,问题是当我将AuthorizationAttribute添加为GlobalFilter而不仅仅是装饰控制器/动作时。 虽然依赖解析器似乎被调用并设置我公开的[Dependancy]属性,当它实际到达我覆盖AuthorizeAttribute的OnAuthorization()方法的代码部分时,我的public [Dependency]属性为null。 当我从全局filter中删除它并装饰一个控制器时,它似乎工作。 如果需要更多信息,我可以发布代码。 编辑:为了进一步扩展这里我的一些代码: 的global.asax.cs public static void RegisterGlobalFilters(GlobalFilterCollection filters) { // If I have this here the my [Dependency] attribute does not keep it’s value // If I remove it and decorate the controller the [Dependency] attribute keeps it value filters.Add(new BasicAuthenticationAttribute()); filters.Add(new […]

UnityContainer和内部构造函数

我有一个内部构造函数的类,并希望从Unity(2.0)解析它。 public class MyClass { internal MyClass(IService service) { } } 然后我在做 _container.Resolve(); 当我这样做时,我有一个例外 Exception is: InvalidOperationException – The type MyClass cannot be constructed. IService已注册,唯一的问题是构造函数是内部的。 我真的希望这个类是公开的,但我希望它只能通过工厂创建(我实际上是在调用container.Resolve() )。 有没有办法让Unity看到内部构造函数? 像InternalsVisibleTo或者什么?

在Unity App.Config文件中包含generics类

我有一个类型ISimpleCache ,我想在ISimpleCache中添加为类型别名(然后是类型) 这条线 <typeAlias alias="ISimpleCacheOfIBrokeredDataObject" type="MyApplication.ISimpleCache, MyApplication” /> 由于显然是错误的,但我不相信逃避它们; 也是正确的。 我目前正在将我的代码拆分为使用Unity,因此距离可编译的代码库太远而无法快速测试,并希望在此处得到一些确认。

如何在PRISM模块中使用ReactiveUI

我们有一个现有的WPF应用程序,它使用PRISM(6.x)和Unity(3.5.x)进行DI。 我们将为此应用程序添加更多function,并希望开始将ReactiveUI用于我们需要实现的任何新模块。 我们希望尽量减少学习曲线并继续为ViewModels使用Unity和DI; 但是,ReactiveUI使用Splat for View位置,到目前为止,我无法让我的ReactiveUI模块实际显示在我们的主应用程序中的PRISM区域,无论我尝试过什么。 我真正找到的唯一一篇文章就是这篇文章在ReactiveUI中的IMutableDependencyResolver和Structuremap问题,其中有人正在编写自己的集成。 所以给出了一些ViewModel: public class HelloWorldViewModel : ReactiveObject { string title = “Hello ReactiveUI”; public string Title { get { return title; } set { this.RaiseAndSetIfChanged(ref title, value); } } } 及其相应的观点: 以及实现IViewFor接口背后的视图代码: public partial class HelloWorldView : UserControl, IViewFor { public HelloWorldView() { InitializeComponent(); this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => […]

获取在Unity中实现接口的所有类型

如果您想了解解决方案,请跳至更新: 我有一个应用程序,它使用以下代码来获取和运行许多工作方法 var type = typeof(IJob); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(x => x.GetTypes()) .Where(x => x.IsClass && type.IsAssignableFrom(x)); foreach (Type t in types) { IJob obj = Activator.CreateInstance(t) as IJob; obj.Run(); } 此代码完美无缺。 但是,一些较新的作业利用dependency injection来填充其构造函数,因此这种方法将无法继续使用。 所以我想知道是否有办法以统一的方式做到这一点? 我最初的想法是,我将继续上半部分,然后用解决方案替换foreach逻辑,使其看起来像下面这样。 var type = typeof(IJob); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(x => x.GetTypes()) .Where(x => x.IsClass && type.IsAssignableFrom(x)); foreach (Type […]