unity-container

Unity - ResolveAll by name with condition

痞子三分冷 提交于 2019-12-06 00:42:12
I was wondering if it is possible to resolve all dependancies in Unity by some condition on the name that they were registered. For example: Resloving all interfaces registered where the name registered starts with "ProcessA". And if there is no way to do this then perhaps how can i extend Unity to allow this. You should be able to use Registrations to do this and I would recommend an extension method rather than extending Unity directly: var matches = c.Resolve<IMyService>(name => name.StartsWith("ProcessA")); Using this extension method: public static class MyUnityExtensions { public static

DbContext is Disposed When Using Unity Dependency Injection on WebApi project

隐身守侯 提交于 2019-12-06 00:12:39
I'm fairly new at using dependency injection and I think I must be overlooking something really simple. I have a Web API project where I'm registering generic repositories. The repositories take a dbContext as a parameter in their constructor. The behavior I find strange is that I can make one successfull call to the service but any subsequent calls tell me that the dbcontext has been disposed. I do have a using statement in there but that shouldn't be a problem since DI is supposed to be creating new instances of my dependencies for each web request(although I could be wrong). Here is my

Castle Windsor Typed Factory Facility equivalents

与世无争的帅哥 提交于 2019-12-05 22:59:45
问题 do any other .NET IoC containers provide equivalent functionality to the typed factory facility in Castle Windsor? e.g. if I am using an abstract factory pattern in a WPF application: public class MyViewModel { private IAnotherViewModelFactory factory; public void ShowAnotherViewModel() { viewController.ShowView(factory.GetAnotherViewModel()); } } I don't want to have to create a manual implementation of IAnotherViewModelFactory for every type of ViewModel I wish to show, I want the container

How can I register one singleton to different interfaces in unity, XML config?

不羁岁月 提交于 2019-12-05 21:46:19
问题 How to do it in code is explained here: Unity Register two interfaces as one singleton _container.RegisterType<EventService>(new ContainerControlledLifetimeManager()); _container.RegisterType<IEventService, EventService>(); _container.RegisterType<IEventServiceInformation, EventService>(); bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>()); How to do it in the XML config? 回答1: Personally I like to spell out namespaces and

Can't inject on System.Web.Http.Filters.ActionFilterAttribute using Unity bootstrapper for ASP.NET Web API nuget package

拥有回忆 提交于 2019-12-05 21:28:21
问题 I can't get dependency injection working with a custom ActionFilterAttribute class using the Unity bootstrapper for ASP.NET Web API nuget package. I've registered the type in UnityConfig and I'm using it elsewhere (using constructor injection there though) and it works fine. public static void RegisterTypes(IUnityContainer container) { container.RegisterType<ISettingService, SettingService>(); ... } The code is being called successfully, however the instantiated object (settingService) is

UnityContainer configured in web.config

我怕爱的太早我们不能终老 提交于 2019-12-05 21:04:26
I have the following code var container = new UnityContainer(); //LINE 1 container.RegisterType<ILogUtility,LogUtil>(); //LINE 2 var logger = container.Resolve<Logger>(); //LINE 3 logger.Log(LogType.Warn, "logging from container"); //LINE 4 How do I implement line 2 in web.config such that I will only have to code line 1, 3, and 4 in my code behind? I have searched every where for code example but they are not clear. Thanks Take a look at my tutorial http://netpl.blogspot.com/2011/11/unity-application-block-is-lightweight.html There's an example XML configuration: <?xml version="1.0" encoding=

Could not load type 'System.Reflection.IntrospectionExtensions' from assembly 'mscorlib

安稳与你 提交于 2019-12-05 19:04:25
I'm using Unity IoC (Microsoft.Practices.Unity) and I can compile fine, but I'm getting the following error at run time: Could not load type 'System.Reflection.IntrospectionExtensions' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The Unity version is: 3.0.1026.0 Is there a compatibility problem between .Net 4 and this version of Unity. Also, I get the same error when using Unity v2. If so, how might I go about resolving this issue? EDIT: Also, I've verified that mscorlib is referenced in my project. RESOLVED. Indeed I was using the .NET 4.5

Unity - Resolution of the dependency failed (without registering)

前提是你 提交于 2019-12-05 18:50:35
I'm getting an error on this line of code: using (IMaterialClient rawMaterialServiceProxy = ServerUtility.Container.Resolve<IMaterialClient>()) The error: Resolution of the dependency failed... The current type, Xxx, is an interface and cannot be constructed. Are you missing a type mapping? I'm not registering a concrete IMaterialClient. In the Pluralsight video I just watched, they said that you don't have to register every type because Unity will find an implementation if one wasn't specified. Has that changed? Am I missing something? Why won't that resolve? The assembly with the actual

Dependency Injection and ModelStateWrapper

丶灬走出姿态 提交于 2019-12-05 18:22:21
in tutorial Validating with a service layer constructor for Product Service looks like this: ProductService(IValidationDictionary validationDictionary, IProductRepository repository) and its instance in default controller constructor is created like this: public ProductController() { _service = new ProductService(new ModelStateWrapper(this.ModelState), new roductRepository()); } If I want to use Unity for DI, second constructor should obviously be used. public ProductController(IProductService service) { _service = service; } But then I do not not know to configure Unity to inject first

Child container registration based on route parameters

扶醉桌前 提交于 2019-12-05 17:13:23
We have a multi-tennant ASP.NET MVC application that hosts a booking engine for multiple clients. Each of these clients has multiple packages that can influence Unity Container configuration. We are creating a child container per request and registering different interface implementations based on the client and package parameters passed through the route. Currently we are accomplishing this by doing the following: Controller has a property ServiceLocator that uses a unity container to resolve dependencies. Controller gets IUnityContainer injected and assigned to a property. Controller has a