unity-container

Unity with ASP.NET Core and MVC6 (Core)

可紊 提交于 2019-11-28 04:44:37
Update 09.08.2018 Unity is being developed here but I haven't had the time to test how it playes with the ASP.NET Core framework. Update 15.03.2018 This solution is for the specific problem of using ASP.NET Core v1 with Unity while using the .NET Framework 4.5.2 NOT the .NET Core Framework. I had to use this setup since I needed some .Net 4.5.2 DLLs but for anyone starting afresh I would not recommend this approach. Also Unity is not being developed any further (to my knowlage) so I would recommend using the Autofac Framework for new projects. See this Post for more info on how to do that.

Unity Register two interfaces as one singleton

允我心安 提交于 2019-11-28 03:48:36
how do I register two different interfaces in Unity with the same instance... Currently I am using _container.RegisterType<EventService, EventService>(new ContainerControlledLifetimeManager()); _container.RegisterInstance<IEventService>(_container.Resolve<EventService>()); _container.RegisterInstance<IEventServiceInformation>(_container.Resolve<EventService>()); which works, but does not look nice.. So, I think you get the idea. EventService implements two interfaces, I want a reference to the same object if I resolve the interfaces. Chris Edit After some feedback in the comments I've decided

Can someone explain Microsoft Unity?

自闭症网瘾萝莉.ら 提交于 2019-11-28 02:32:43
I've been reading the articles on MSDN about Unity (Dependency Injection, Inversion of Control), but I think I need it explained in simple terms (or simple examples). I'm familiar with the MVPC pattern (we use it here), but I just can't really grasp this Unity thing yet, and I think it's the next step in our application design. Chris Holmes Unity is just an IoC "container". Google StructureMap and try it out instead. A bit easier to grok, I think, when the IoC stuff is new to you. Basically, if you understand IoC then you understand that what you're doing is inverting the control for when an

Unity IoC does not inject dependency into Web API Controller

别等时光非礼了梦想. 提交于 2019-11-28 02:29:48
问题 I'm very new to using Unity, but my problem is that whenever I call my web service, I get an exception stating that "Make sure that the controller has a parameterless public constructor" I've followed multiple tutorials and I still get the same issue. In the Register function of my WebApiConfig class, I have var container = new UnityContainer(); container.RegisterType<IValidator, Validator>(new HierarchicalLifetimeManager()); config.DependencyResolver = new UnityResolver(container); Here is

ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi

妖精的绣舞 提交于 2019-11-28 00:14:04
问题 My ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi. In the same project simple controllers works with Unity.Mvc3 properly. But when I run Web API controller derived from ApiController I'm getting a message: {"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Type 'ElectricTests.Controllers.Api.DocumentsController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n

Register null as instance in Unity container

南笙酒味 提交于 2019-11-27 23:15:28
问题 I have a repository class with optional dependency: class MyRepository : BaseRepository, IMyRepository { public MyRepository(IDataContext dataContext, ICacheProvider cacheProvider = null) : base(dataContext, cacheProvider) {} // … } The existence of cacheProvider parameter acts as strategy for the repository. I want setup Unity container like this: Container.RegisterType<IDataContext, MyDataContext>(new PerResolveLifetimeManager(), new InjectionConstructor()) .RegisterInstance<ICacheProvider>

Unity Lifetime Managers & EF Data Context --> Best Practice

时光毁灭记忆、已成空白 提交于 2019-11-27 21:47:52
问题 All, There has been a lot of posts about Unity Lifetime Managers but I have yet to find someone state a good rule of thumb for "in these cases you should always use X". Let me describe my application, I have an ASP.NET MVC 4 Web Application. I have a Visual Studio solution containing 3 projects, my 'Core' project which has all of my EF stuff, a testing project, and the MVC Web Project. I am using Unity for dependency injection and have the following code right now: // Context container

Exception is: InvalidOperationException - The current type, is an interface and cannot be constructed. Are you missing a type mapping?

送分小仙女□ 提交于 2019-11-27 21:04:35
In my bootstrapper: namespace Conduit.Mam.ClientServices.Common.Initizliaer { public static class Initializer { private static bool isInitialize; private static readonly object LockObj = new object(); private static IUnityContainer defaultContainer = new UnityContainer(); static Initializer() { Initialize(); } public static void Initialize() { if (isInitialize) return; lock (LockObj) { IUnityContainer container = defaultContainer; //registering Unity for MVC DependencyResolver.SetResolver(new UnityDependencyResolver(container)); //registering Unity for web API // GlobalConfiguration

MVVM Light + Unity or Prism?

社会主义新天地 提交于 2019-11-27 19:44:22
问题 I am a little out-of-date in WPF right now and would be interested to hear peoples opinions on the latest version of Prism (which I used a couple of versions ago) vs an MVVM Light + Unity approach (which I have never done - decent examples URLs would be good). My project will be a large one comprising multiple modules written by several developers. There is also the funds to bring in a 3rd party control suite in order to set up a nice workspace using one of the fancy Docking/Workspace layout

Constructor Injection in C#/Unity?

假装没事ソ 提交于 2019-11-27 19:08:48
I'm using C# with Microsoft's Unity framework. I'm not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity. My problem can be summed up using the following example code: class Train(Person p) { ... } class Bus(Person p) { ... } class Person(string name) { ... } Person dad = new Person("joe"); Person son = new Person("timmy"); When I call the resolve method on Bus how can I be sure that the Person 'son' with the name 'timmy' is injected and when resolving Train how can I be sure that Person 'dad' with then name 'joe' is resolved? I'm