unity-container

Should I use OwinContext's Environment to hold application specific data per request

蓝咒 提交于 2019-11-30 17:20:11
I need a way to store a logging object per request. With HttpContext I would add this to the items Dictionary. I don't want to bring HttpContext into this if I can help it. The below code is what I propose for a Unity LifeTimeManager that will store objects in OwinContext's Environment property, which I have access to with my Owin middleware. public class OwinContextLifetimeManager : LifetimeManager { private string key = (new Guid()).ToString(); private IDictionary<string, object> environment; public OwinContextLifetimeManager(IDictionary<string, object> environment) { this.environment =

Unity IOC Buildup vs Resolve?

核能气质少年 提交于 2019-11-30 17:13:14
I was wondering when do I use buildup and when do I use resolve, when using the Unity IOC. And when do I call teardown? Thanks Resolve is used when you want the Unity container to construct the instance (a new one just when you need it or an pre-existing singleton), inject its dependencies and hand you the reference to the object. BuildUp is used when you already have an instance of the object and want the container to just resolve and inject its dependencies. Teardown is the opposite of BuildUp. You can pass your object to the Teardown method of the container to shut down / clean up /

Unity InjectionConstructor for multiparam constructor overriding only single one

陌路散爱 提交于 2019-11-30 16:47:38
I have a class with constructor like this: public class Bar { public Bar(IFoo foo, IFoo2 foo2, IFoo3 foo3, IFooN fooN, String text) { } } I want to register Bar in Unity and provide a value for text: unity.RegisterType<Bar, Bar>(new InjectionConstructor("123")); However I can't do this because there is no single parameter constructor for Bar. Is there a way to provide a value for text without specifying all other parameters as ResolvedParameter<IFooN> etc.. I really don't like it, lot's of code, and every time I change a constructor of Bar I need to add another ResolvedParameter Sebastian

Should I use OwinContext's Environment to hold application specific data per request

隐身守侯 提交于 2019-11-30 16:33:31
问题 I need a way to store a logging object per request. With HttpContext I would add this to the items Dictionary. I don't want to bring HttpContext into this if I can help it. The below code is what I propose for a Unity LifeTimeManager that will store objects in OwinContext's Environment property, which I have access to with my Owin middleware. public class OwinContextLifetimeManager : LifetimeManager { private string key = (new Guid()).ToString(); private IDictionary<string, object>

Unity InjectionConstructor for multiparam constructor overriding only single one

点点圈 提交于 2019-11-30 16:21:59
问题 I have a class with constructor like this: public class Bar { public Bar(IFoo foo, IFoo2 foo2, IFoo3 foo3, IFooN fooN, String text) { } } I want to register Bar in Unity and provide a value for text: unity.RegisterType<Bar, Bar>(new InjectionConstructor("123")); However I can't do this because there is no single parameter constructor for Bar. Is there a way to provide a value for text without specifying all other parameters as ResolvedParameter<IFooN> etc.. I really don't like it, lot's of

Unity DI Inject DbContext with PerRequestLifetimeManager

荒凉一梦 提交于 2019-11-30 15:47:48
I have the following code that initialize instances with Unity: IUnityContainer container = new UnityContainer(); container.RegisterType<DbContext, VotingSystemContext>(new PerRequestLifetimeManager(), new InjectionConstructor()); container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>)); container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager()); container.RegisterTypes( AllClasses.FromAssemblies( Assembly.GetAssembly(typeof(IUserService)), Assembly.GetAssembly(typeof(UserService))), WithMappings.FromMatchingInterface, WithName.Default,

Unity not using the default constructor of the class

戏子无情 提交于 2019-11-30 14:43:18
问题 I have this class : public class Repo { public Repo() : this(ConfigurationManager.AppSettings["identity"], ConfigurationManager.AppSettings["password"]) { } public Repo(string identity,string password) { //Initialize properties. } } I added a line to web.config so that this type will be automatically constructed by Unity container. but during the execution of my application, I receive this error message : "System.InvalidOperationException : the parameter identity could not be resolved when

An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll

非 Y 不嫁゛ 提交于 2019-11-30 14:14:23
In my Asp.net MVC project I have a bootsrapper that initialize a unity-container. I don't know why, but I get An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll I have doubled checked and registration is done only in my initializer. All dependencies are injected in the ctors only. What could have caused this? protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); Initializer

Using Unity With Minimal Configuration

孤街醉人 提交于 2019-11-30 14:12:16
At work, we use Unity a lot. It's great at it's job, but the more you use it, the more your configuration file grows, the more runtime issues increase and the more you have to recreate your unity configuration for each test project. So we end up with a huge unity configuration section that has to be duplicated accross several projects and when it comes time to deploy, you end up having to track down dlls that you forgot to add references to, but you only discover these at runtime. Not fun. I'm guessing someone has come across this issue and has a solution. Ideally, I would like to figure out

Is it possible to get current Unity container inside controller

与世无争的帅哥 提交于 2019-11-30 13:56:38
I registered unity container like this: var container = new UnityContainer(); container.RegisterType<ICacheManager, CacheManager>(new ContainerControlledLifetimeManager()) Is it possible to get access to this "container" from controller One way of doing this which I often do for convenience is to declare your container as a global variable in your Global.ascx.cs file like: public class MvcApplication : System.Web.HttpApplication { public static UnityContainer Container; protected void Application_Start() { // assuming your initialize here } } However this is fairly hack-ish. The correct thing