unity-container

Property Dependency Injection used in Constructor using Unity

落爺英雄遲暮 提交于 2019-12-07 06:33:23
问题 Ok, I have a dependent property defined in a base class and I'm trying to use it inside of the constructor of its derived class but that does not work, the property appears as null. Unity resolves the dependent property AFTER resolving an instance with container.Resolve(); One alternative I have is to add a IUnityContainer parameter to my MyViewModel class constructor and set the ILogger property my self with something like: public MyViewModel(IUnityContainer container) { Logger = container

what are pitfalls of making UnityContainer not thread safe?

非 Y 不嫁゛ 提交于 2019-12-07 04:51:16
问题 I am adding dependency injection to my library and I use Unity for that. And I am wondering if I need to take some additional steps to make Unity Container thread-safe. I found couple of articles that are talking about Thread-safe container (example: http://www.fascinatedwithsoftware.com/blog/post/2012/01/04/A-Thread-Safe-Global-Unity-Container.aspx ) but I don't understand if I really need it in my project. From one side I don't want to have some nasty bugs due to race conditions from the

Unity framework - reusing instance

好久不见. 提交于 2019-12-07 04:38:10
问题 nobody loved my first question about this: Creating Entity Framework objects with Unity for Unit of Work/Repository pattern so I've managed to rephrase it to something you can read without falling asleep/losing the will to live. I'm creating an object, DataAccessLayer, that takes 2 interfaces in the constructor: IUnitOfWork, and IRealtimeRepository: public DataAccessLayer(IUnitOfWork unitOfWork, IRealtimeRepository realTimeRepository) { this.unitOfWork = unitOfWork; this.realTimeRepository =

Unity with factory method who need parameters

吃可爱长大的小学妹 提交于 2019-12-07 03:27:12
问题 I want to register a type in an Unity container with a factory method who needs paramters. These parameters are to be resolved by unity but only at runtime. Factory method code : public static IApp Create(IOne, ITwo) {...} Register code : container.RegisterType(typeof(IApp), new InjectionFactory(f => App.Create(???, ???))); What do I need to replace the '???' ? More informations : My Unity configuration is made in two phases. First phase (application is starting), I register all my object but

Injecting class into Authentication attribute using Unity 2 and MVC 3

旧城冷巷雨未停 提交于 2019-12-07 03:10:07
问题 I've read a number of articles that point out how to do this, namely: Stack overflow solution Brad Wilsons excellent tutorial These do appear to work well, however when I follow some of the guidelines from here Securing your ASP.NET MVC 3 Application I seem to come a cropper. The issue for me is when I add my AuthorizationAttribute as a GlobalFilter rather than just decorating a controller/action. Although the dependancy resolvers seem to be called and set my Public exposed [Dependancy]

Injecting an Enumerable containing all registered Implementations of an Interface

烂漫一生 提交于 2019-12-07 02:46:31
问题 Given the following interface: public interface IMyProcessor { void Process(); } I'd like to be able to register multiple implementations and have my DI container inject an enumerable of them into a class like this: 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(); } } } Is this

Conditionally resolve named implementation in Unity

落爺英雄遲暮 提交于 2019-12-07 02:30:24
问题 Unity allows you to name different implementations of the same interface and then resolve them by name: var container = new UnityContainer(); // register container.Register<IFish, OneFish>("One"); container.Register<IFish, TwoFish>("Two"); // resolve var twoFish = container.Resolve("Two"); Now assume I have a class that depends on IFish and implements ITank: class Tank : ITank { public Tank(IFish fish) {...} } How can I resolve ITank and specify which implementation of IFish to get? This

Make sure that the controller has a parameterless public constructor in web api?

只愿长相守 提交于 2019-12-06 16:07:27
I am struggling with this problem of dependency injection using untiy . I have implemented according to this link https://www.asp.net/web-api/overview/advanced/dependency-injection But got this error: { "Message": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'WebAPIController'. Make sure that the controller has a parameterless public constructor.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,

Default the LifetimeManager to the singleton manager (ContainerControlledLifetimeManager)?

馋奶兔 提交于 2019-12-06 15:42:49
I'm using a Unity IoC container to do Dependency Injection. I designed my system around the idea that, at least for a single resolution, all types in the hierarchy would behave as singletons, that is, same type resolutions within that hierarchy would lead to the same instances. However, I (a) would like to scan my assemblies to find types and (b) don't want to explicitly tell unity that every type is to be resolved as a singleton when registering types in the configuration file. So, is there a way to tell unity to treat all registered mappings as singleton? In case anyone is still looking for

asp.net unity dependency injection doesn't work well

情到浓时终转凉″ 提交于 2019-12-06 14:56:14
问题 I am using the mvc 3 framework of asp.net, I have created a UnityControllerFactory that implements the IControllerFactory interface: ... public class UnityControllerFactory : IControllerFactory { private IUnityContainer _container; private IControllerFactory _innerFactory; public UnityControllerFactory(IUnityContainer container) : this(container, new DefaultControllerFactory()) { } protected UnityControllerFactory(IUnityContainer container, IControllerFactory innerFactory) { _container =