unity-container

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

你。 提交于 2019-11-26 20:31:43
问题 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

Constructor Injection in C#/Unity?

醉酒当歌 提交于 2019-11-26 19:46:17
问题 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

Setter / property injection in Unity without attributes

自古美人都是妖i 提交于 2019-11-26 19:13:03
问题 I am working on a project where the Unity framework is used as the IoC container. My question relates to injecting an optional dependency (in this case a logger) into several classes using property- or setter injection. I do not want to clutter the constructors of all my classes with these optional dependencies, but I cannot find a good way to handle this in Unity. The way you would do this is, according to the MSDN documentation, by adding an attribute to the property: private ILogger logger

Use of IsAssignableFrom and “is” keyword in C#

一曲冷凌霜 提交于 2019-11-26 18:14:31
问题 While trying to learn Unity, I keep seeing the following code for overriding GetControllerInstance in MVC: if(!typeof(IController).IsAssignableFrom(controllerType)) { ... } this seems to me a pretty convoluted way of basically writing if(controllerType is IController) { ... } I appreciate there are subtle differences between is and IsAssignableFrom , ie IsAssignableFrom doesn't include cast conversions, but I'm struggling to understand the implication of this difference in practical scenarios

Why is PerThreadLifetimeManager used in this example?

我的梦境 提交于 2019-11-26 17:51:32
I am following the example linked to below for setting up unity to work with my service layer. My project is setup very similar to the one in this article and I understand everything except why exactly is PerThreadLifetimeManager used when registering the service dependency. Keep in mind I am also using a generic repository and unitofwork that is being used in my service layer as well. Most unity examples use the default(transient) lifetime manager, and since my setup is similar to the one below I'm wondering why I should use the PerThreadLifeimeManager? I am using an ASP.NET web forms project

Resolving type with PerRequestLifetimeManager without HTTP request

我只是一个虾纸丫 提交于 2019-11-26 16:56:30
问题 I have a MVC application that uses IoC with Unity and I have a DbContext implementation defined using the PerRequestLifetimeManager . This object is injected to controllers through Unit of Work implementation. container.RegisterType<DBContext, MyContext>(new PerRequestLifetimeManager()); Everything is working fine so far and the app has a decent number of models and controllers. Now what I was trying to do recently is add some automated tasks for this application and for this purpose I wanted

Can not install NuGet package

℡╲_俬逩灬. 提交于 2019-11-26 16:51:45
问题 I am trying to add the Unity package to my solution, but I keep receiving the listed message: Attempting to resolve dependency 'Unity (≥ 3.5.1404.0)'. 'Unity' already has a dependency defined for 'CommonServiceLocator'. Any Idea how to fix this? 回答1: This problem arises with older version of nuget. Update nuget PM, 2.8.50313.46 is current version, Look for the latest for VS 2012 回答2: You are using Higher version .Net framework. And you are adding Lower version of Unity in to it. your

Unity Singleton Code

北慕城南 提交于 2019-11-26 16:24:01
问题 I'm new to Unity and am trying to write some Unity logic which initialises and register/resolves a singleton instance of the Email object so that it can be used across several other objects, one example below being OperationEntity. So when it's registered it populates the Email singleton with some values from a config file, then whenever an instance of OperationEntity is created (in my case it's being deserialized) it uses that same Email singleton. So all my client logic needs to do is

How do you reconcile IDisposable and IoC?

本秂侑毒 提交于 2019-11-26 16:09:44
I'm finally wrapping my head around IoC and DI in C#, and am struggling with some of the edges. I'm using the Unity container, but I think this question applies more broadly. Using an IoC container to dispense instances that implement IDisposable freaks me out! How are you supposed to know if you should Dispose()? The instance might have been created just for you (and therefor you should Dispose() it), or it could be an instance whose lifetime is managed elsewhere (and therefor you'd better not). Nothing in the code tells you, and in fact this could change based on configuration!!! This seems

Unity 2.0 and handling IDisposable types (especially with PerThreadLifetimeManager)

落花浮王杯 提交于 2019-11-26 15:45:51
问题 I know that similar question was asked several times (for example: here, here,here and here) but it was for previous versions of Unity where the answer was dependent on used LifetimeManager class. Documentation says: Unity uses specific types that inherit from the LifetimeManager base class (collectively referred to as lifetime managers) to control how it stores references to object instances and how the container disposes of these instances. Ok, sounds good so I decided to check