unity-container

Resolving type with PerRequestLifetimeManager without HTTP request

好久不见. 提交于 2019-11-27 14:51:54
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 to use HangFire . I've set up this library in my project and created a simple task in which I want to

Can not install NuGet package

淺唱寂寞╮ 提交于 2019-11-27 14:36:29
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? 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 You are using Higher version .Net framework. And you are adding Lower version of Unity in to it. your Application Framework will be above 4.0 or 4.0 and you are adding Unity frame work which is of .net frame 3.5. 来源: https:

Specify constructor for the Unity IoC container to use

对着背影说爱祢 提交于 2019-11-27 14:19:15
问题 I'm using the Unity IoC container for resolving my objects. However, I've run into an issue. When I have more than one constructor - how does Unity know which one to use? It seems to use the one with parameters when I have one with and one without. Can I explicitly tell it which constructor to use? Specifically I had a case similar to the following Person class with two constructors. In this case I want the IoC container to use the default constructor - without parameters - but it chooses the

Unity Singleton Code

ぃ、小莉子 提交于 2019-11-27 13:56:36
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 deserialize OperationEntity and call PerformAction() - with the email instance taken care of by Unity.

Cannot get rid of “physical connection is not usable” exception

不羁的心 提交于 2019-11-27 13:36:34
问题 I am about to shoot myself. Spent few weeks now trying to solve this issue. We have an ASP.NET MVC 4 web app that uses SQL Server 2012 and Entity Framework as ORM and Unity for IoC. Web app is hosted on Amazon EC2. I started getting "Physical connection is not usable" exception. It happens few times a day. I searched many articles and forums and tried all the possible suggestions: Tried removing pooling from connection string "Polling=False" Tried limiting pool size and connection lifetime

Use of IsAssignableFrom and “is” keyword in C#

时间秒杀一切 提交于 2019-11-27 13:00:24
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. When is it imporantant to choose IsAssignableFrom over is ? What difference would it make in the

Unity 2.0 and handling IDisposable types (especially with PerThreadLifetimeManager)

六月ゝ 毕业季﹏ 提交于 2019-11-27 11:46:50
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 implementation of build in lifetime managers. My conclusion: TransientLifetimeManager - no handling of

How does Unity.Resolve know which constructor to use?

坚强是说给别人听的谎言 提交于 2019-11-27 11:31:26
问题 Given a class with several constructors - how can I tell Resolve which constructor to use? Consider the following example class: public class Foo { public Foo() { } public Foo(IBar bar) { Bar = bar; } public Foo(string name, IBar bar) { Bar = bar; Name = name; } public IBar Bar { get; set; } public string Name { get; set; } } If I want to create an object of type Foo using Resolve how will Resolve know which constructor to use? And how can I tell it to use the right one? Let's say I have a

Is MEF a dependency injection framework?

≯℡__Kan透↙ 提交于 2019-11-27 11:08:11
问题 The recently announced managed extensibility framework (MEF) of .NET 4.0 - is it a dependency injection framework? Will Microsoft Unity from Patterns and Practices be obsolete in 4.0 ? How does MEF compare to a framework like Unity? 回答1: Specifically addressed in the PDC 2008 2nd Keynote by Scott Guthrie, MEF has a lot more to do with things like extending Visual Studio 2008 and other applications, without having to use all the COM and older technologies... A very good demonstration of

Proper way to Mock repository objects for unit tests using Moq and Unity

杀马特。学长 韩版系。学妹 提交于 2019-11-27 09:35:05
问题 At my job we are using Moq for mocking and Unity for an IOC container. I am fairly new to this and do not have many resources at work to help me out with determining the best practices I should use. Right now, I have a group of repository interfaces (Ex: IRepository1, IRepository2... IRepository4) that a particular process needs to use to do its job. In the actual code I can determine all of the IRepository objects by using the IOC container and using the RegisterType() method. I am trying to