unity-container

Random errors occur with per-request DbContext

人走茶凉 提交于 2019-12-05 08:31:33
I'm experiencing random errors (several per day) in my mvc+ef+unity application under higher load (10+ request per sec): The connection was not closed / The connection's current state is connecting deadlocks on Count queries (no explicit transaction) An item with the same key has already been added. in System.Data.Entity.DbContext.SetTEntity while resolving DbContext The remote host closed the connection. The error code is 0x80070057 There is already an open DataReader associated with this Command which must be closed first. - I turned on MARS to get rid off this (altough I believe it should

Creating new instances while still using Dependency Injection

ぃ、小莉子 提交于 2019-12-05 08:30:58
A quick description of the environment: I have a class that represents a chatroom and has a dependency on a logger. It's not the same as a system-wide logger with cross-cutting concerns, but a logger that's tied to that specific chatroom. It logs all activity in that chatroom to it's unique log file. When the chatroom is created I want to open the log file, and when it's destroyed I want to close the log file. The Problem Here's the relevant code I'm using. public interface IChatroomLogger { void Log(ServerPacket packet); void Open(); void Close(); } public class ChatroomLogger :

Unity with factory method who need parameters

99封情书 提交于 2019-12-05 08:18:23
I want to register a type in an Unity container with a factory method who needs paramters. These parameters are to be resolved by unity but only at runtime. Factory method code : public static IApp Create(IOne, ITwo) {...} Register code : container.RegisterType(typeof(IApp), new InjectionFactory(f => App.Create(???, ???))); What do I need to replace the '???' ? More informations : My Unity configuration is made in two phases. First phase (application is starting), I register all my object but one : container.RegisterType<IOne, One>(); container.RegisterType<ITwo, Two>(); container.RegisterType

Injecting an Enumerable containing all registered Implementations of an Interface

风流意气都作罢 提交于 2019-12-05 08:03:16
Given the following interface: public interface IMyProcessor { void Process(); } I'd like to be able to register multiple implementations and have my DI container inject an enumerable of them into a class like this: public class MyProcessorLibrary { private readonly IMyProcessor[] _processors; public MyProcessingThing(IMyProcessor[] processors) { this._processors = processors; } public void ProcessAll() { foreach (var processor in this._processors) { processor.Process(); } } } Is this possible? My current implementation of MyProcessorLibrary looks up all the IMyProcessor implementations

Can Castle.Windsor do automatic resolution of concrete types

拈花ヽ惹草 提交于 2019-12-05 06:13:51
We are evaluating IoC containers for C# projects, and both Unity and Castle.Windsor are standing out. One thing that I like about Unity (NInject and StructureMap also do this) is that types where it is obvious how to construct them do not have to be registered with the IoC Container. Is there way to do this in Castle.Windsor? Am I being fair to Castle.Windsor to say that it does not do this? Is there a design reason to deliberately not do this, or is it an oversight, or just not seen as important or useful? I am aware of container.Register(AllTypes... in Windsor but that's not quite the same

Injecting class into Authentication attribute using Unity 2 and MVC 3

微笑、不失礼 提交于 2019-12-05 05:57:48
I've read a number of articles that point out how to do this, namely: Stack overflow solution Brad Wilsons excellent tutorial These do appear to work well, however when I follow some of the guidelines from here Securing your ASP.NET MVC 3 Application I seem to come a cropper. The issue for me is when I add my AuthorizationAttribute as a GlobalFilter rather than just decorating a controller/action. Although the dependancy resolvers seem to be called and set my Public exposed [Dependancy] property when it actually comes to the part of the code where I am overriding the OnAuthorization() method

Conditionally resolve named implementation in Unity

扶醉桌前 提交于 2019-12-05 05:35:11
Unity allows you to name different implementations of the same interface and then resolve them by name: var container = new UnityContainer(); // register container.Register<IFish, OneFish>("One"); container.Register<IFish, TwoFish>("Two"); // resolve var twoFish = container.Resolve("Two"); Now assume I have a class that depends on IFish and implements ITank: class Tank : ITank { public Tank(IFish fish) {...} } How can I resolve ITank and specify which implementation of IFish to get? This doesn't work: container.Register<ITank, Tank>(); var tank = container.Resolve<ITank>("One"); This works:

Dependency on the framework assembly “System.Runtime, Version=4.0.10.0,” which could not be resolved in the currently targeted framework

别说谁变了你拦得住时间么 提交于 2019-12-05 04:54:20
TFS 2013 - Build: ASP.Net 4.5.1 website I get this error: warning MSB3268: The primary reference "C:\Builds\2\MyProj\Web1_Main\bin\MyProj1.dll" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.5.1". To resolve this problem, either remove the reference "C:\Builds\2\MyProj\Web1_Main\bin\MyProj1.dll" or retarget your application to a framework version which contains "System.Runtime,

Does Resolve<T>() return objects per-session?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 03:44:05
问题 In Microsoft Unity IoC, if I call Resolve<SomeType>() , can I guarantee that the object returned is the one that was created during the current session? For example, three users sign on, and let's say that the object of SomeType that gets created in the container has different values for each user. Will a call to Resolve return the object that was created for the current user? Or would it do something stupid like return the last one that was created? I'm having troubles testing this myself

UnityContainer and internal constructor

喜欢而已 提交于 2019-12-05 01:29:10
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 calling container.Resolve<MyClass>() ). Is there a way to make Unity see that internal constructor? Like