unity-container

How to use DI when spawning new Windows Forms downstream?

﹥>﹥吖頭↗ 提交于 2019-12-05 16:59:22
I have the Unity DI container working initially with my Windows Forms application. In Program.cs I have the following: static void Main() { var container = BuildUnityContainer(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(container.Resolve<MainForm>()); } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<ITest, MyTestClass>(); container.RegisterType<ISomeOtherTest, MyOtherClass>(); return container; } In my MainForm constructor I have the following which does work :

Authorization Filter Dependency Injection with ASP.New MVC 4 Web Api

前提是你 提交于 2019-12-05 16:18:14
I'm trying to implement dependency injection on a MVC 4 Web Api Authorization Filter. I created a FilterProvider that inherits from ActionDescriptorFilterProvider: public class UnityWebApiFilterAttributeFilterProvider : ActionDescriptorFilterProvider, System.Web.Http.Filters.IFilterProvider { private readonly IUnityContainer _container; public UnityWebApiFilterAttributeFilterProvider(IUnityContainer container) { _container = container; } public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor) { var filters = base.GetFilters

An error occurred creating the configuration section handler

こ雲淡風輕ζ 提交于 2019-12-05 15:47:44
I have a dot.NET 4.0 web application with a custom section defined: <configuration> <configSections> <section name="registrations" type="System.Configuration.IgnoreSectionHandler, System.Configuration, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/> .... at the end of the web.config file I have the respective section: .... <registrations> ..... </registrations> </configuration> Every time I call System.Configuration.ConfigurationManager.GetSection("registrations"); I get the following

Unity intercept change call handler based on method annotation

痴心易碎 提交于 2019-12-05 15:22:44
I have a method in a class as follow that I want to intercept: [CustomTag1(Order = 0)] [CustomTag2(Order = 1)] public virtual DoSomething() How can I inject the order value into ICallHandler.Order property, when using CustomAttributeMatchingRule ? I don't want the order to be hard coded to the handler itself or at registration. I want it to be a variable of the Order property of the method annotation. I've achieved this using the HandlerAttribute - in general I use this anyway for attribute-style interception in Unity simply because you don't have to bother creating policies manually - instead

Registering types with lambda expression

笑着哭i 提交于 2019-12-05 14:48:59
I was wondering how do I achieve such a feature in the UnityContainer: container.RegisterType<IDummy>(Func<IDummy>) // deferred resolution If you're going to register factory instead of instance, try this: container.RegisterType<IDummy>(new InjectionFactory(context => new Dummy())); Just replace "context => new Dummy()" with your lambda. 来源: https://stackoverflow.com/questions/24256940/registering-types-with-lambda-expression

Forcing WcfSvcHost.exe to use my custom service host

爷,独闯天下 提交于 2019-12-05 13:47:46
Is it possible to force WcfSvcHost (which is executed automatically when I do an F5 or when I am debugging another project in the solution) to use a custom ustom service? I have my custom service host working great in my asp.net Host container by using a service factory which in turn calls the Custom Service Base. But when WcfSvcHost executes it's not using my custom ustom service. Is this possible? If not, what are my alternatives? I presume I must uncheck "Start WCF service host when debugging a project in another solution" which is in the WCF Options in app properties but then I must create

How to configure Unity to inject an array for IEnumerable

懵懂的女人 提交于 2019-12-05 12:39:31
I have a class which takes an IEnumerable constructor parameter which I want to resolve with Unity and inject an array of objects. These simple classes illustrate the problem. public interface IThing { int Value { get; } } public class SimpleThing : IThing { public SimpleThing() { this.Value = 1; } public int Value { get; private set; } } public class CompositeThing : IThing { public CompositeThing(IEnumerable<IThing> otherThings) { this.Value = otherThings.Count(); } public int Value { get; private set; } } Say I want to inject four SimpleThing in to CompositeThing . I've tried several

Register Generic Type in Unity Based On Concrete Type

泪湿孤枕 提交于 2019-12-05 11:54:33
I'm trying to emulate a behavior that I can configure in Ninject, only using Unity instead. I am attempting to use the Cached Repository Pattern, given the following classes and interface: public interface IRepository<T> { T Get(); } public class SqlRepository<T> : IRepository<T> where T : new() { public T Get() { Console.WriteLine("Getting object of type '{0}'!", typeof(T).Name); return new T(); } } public class CachedRepository<T> : IRepository<T> where T : class { private readonly IRepository<T> repository; public CachedRepository(IRepository<T> repository) { this.repository = repository; }

Unity framework - reusing instance

不羁岁月 提交于 2019-12-05 09:45:09
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 = realTimeRepository; } Now, the constructor for the implementation of IRealtimeRepository also takes an

what are pitfalls of making UnityContainer not thread safe?

情到浓时终转凉″ 提交于 2019-12-05 08:36:29
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 other side I don't see in what case race condition would occur. I want to use Unity with Composition Root