unity-container

Default the LifetimeManager to the singleton manager (ContainerControlledLifetimeManager)?

自古美人都是妖i 提交于 2019-12-10 11:29:19
问题 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

Unity: pass parameters to custom lifetime constructor, in xml configuration file

南笙酒味 提交于 2019-12-10 11:17:58
问题 I wrote my CustomLifetimeManager like this: public class CustomLifetimeManager <T> : LifetimeManager { private readonly string _arg; public CustomLifetimeManager(string arg) { _arg = arg; } } Now, it works easy configuring the container programmatically, but how add it in configuration file like the following? <type type="myTime" mapTo="myImpl"> <lifetime type="CustomLifetimeManager"/> </type> 回答1: You need to add a second class: A TypeConverter. This class is responsible for taking a string

Passing multiple implementations of the same interface using DI

无人久伴 提交于 2019-12-10 10:51:09
问题 One of our company's products is composed of many small web-applications and a windows service, a.k.a. components, each potentially residing in a different computer. One of them is a WebForms project, which serves as the configuration hub for all the others. We are designing a feature now to expose general information of a component. Imagine a simple interface like this for instance: public interface IStatistics { Statistics GetStatistics(); } We want to use this same interface on all

Unity intercept change call handler based on method annotation

纵饮孤独 提交于 2019-12-10 09:43:31
问题 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. 回答1: I've achieved this using the HandlerAttribute - in general I use this anyway for

Forcing WcfSvcHost.exe to use my custom service host

旧城冷巷雨未停 提交于 2019-12-10 09:31:27
问题 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

Unity Container Resolve

为君一笑 提交于 2019-12-10 03:19:06
问题 I just starting with Unity Container and my registration looks like this: static void UnityRegister() { _container = new UnityContainer(); _container.RegisterType<IBook, Book>(); _container.RegisterType<IBookRepository, BookRepository>("Book"); _container.RegisterType<IBookService, BookService>(); _container.RegisterType<IBookRepository, DatabaseRepository>("Database"); } Now when I try to resolve doing this: var service = _container.Resolve<IBookService>("Database"); I get error below:

UnityContainer and internal constructor

旧城冷巷雨未停 提交于 2019-12-10 02:19:08
问题 I have a class with internal constructor and want to Resolve it from Unity (2.0). public class MyClass { internal MyClass(IService service) { } } then I'm doing _container.Resolve<MyClass>(); when I do so I have an exception Exception is: InvalidOperationException - The type MyClass cannot be constructed. IService is registered and the only problem is that constructor is internal. I really want this class to be public, but I want it to be creatable only via a factory (in which I'm actually

Unity constructor parameters

五迷三道 提交于 2019-12-09 17:56:44
问题 class Entity:IEntityName { #region IEntityName Members public string FirstName { get; set; } public string LastName { get; set; } #endregion } public class Person:IPerson { public IEntityName EntityName; public Person() { } public Person(IEntityName EntityName) { this.EntityName = EntityName; } public string ReverseName() { return string.Format("Your reverse name is {0} {1}",EntityName.LastName, EntityName.FirstName); } public override string ToString() { return string.Format("Name is {0} {1}

Unity .NET: List of dependencies

北城余情 提交于 2019-12-09 16:12:14
问题 Is it possible to inject a list of dependencies like this in Unity or other kind of IoC libraries? public class Crawler { public Crawler(IEnumerable<IParser> parsers) { // init here... } } In this way I can register multiple IParser in my container and then resolve them. Is it possible? Thanks 回答1: It is possible but you need to apply some workarounds. First you need to register each IParser with a name in the Unity Container. Second you need to register a mapping from IParser[] to

Unity Bootstrapper from NuGet is throwing error on App_Start

拥有回忆 提交于 2019-12-09 15:40:16
问题 the Microsoft Unity Bootstrapper for Unity 3.0 is throwing an error on this line: FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container)); with the following error: An exception of type 'System.ArrayTypeMismatchException' occurred in mscorlib.dll but was not handled in user code Additional information: Attempted to access an element as a type incompatible with the array. The entirety of the code is here and this was all pre-made and written by the bootstrapper