unity-container

Using Unity with Web Api 2 gives error does not have a default constructor

好久不见. 提交于 2019-12-06 06:09:34
I have ASP.NET MVC5 web application and i also have Web API in the same application. I am uisng Unity (version 4) for DI. I am configuring the Unity container on APP start as below public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { UnityConfiguration.Config(); } } public class UnityConfiguration() { public void Config() { UnityContainer container = new UnityContainer(); container.RegisterType<IMyService, Myservice>(); container.RegisterType<IGenericRepository, GenericRepository>(); container.RegisterType<DbContext, MyEntities>(); } } public class

Singleton Per Call Context WCF Web Request in Unity

旧时模样 提交于 2019-12-06 05:43:39
问题 I have gone through the question Singleton Per Call Context (Web Request) in Unity. Basically I want to create a singleton object per wcf request using Unity Container. Though I found the answers of other question helpful for the ASP.Net web application, I am not sure those answers still apply to WCF service. The questions is, Using both CallContext and HttpContext in WCF service, can we create PerCallContextOrRequestLifeTimeManager? Would that serve singleton object per wcf call? 回答1: Andrew

Unity dependency injection in custom membership provider

折月煮酒 提交于 2019-12-06 03:43:02
问题 I have ASP.NET MVC3 project where I want to use custom membership provider. Also I want to use Unity for resolving my dependency injection. this is code from Global.asax: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var container = new UnityContainer(); container.RegisterType<IAuthentification, Authentification>(); container.RegisterType<IRepository, Repository>(); DependencyResolver

WebApi equivalent for HttpContext.Items with Dependency Injection

谁都会走 提交于 2019-12-06 03:40:11
问题 I'm building an ASP.NET WebApi 2.1 app that needs an equivalent of HttpContext.Items as a per request cache. I cannot use HttpContext even under IIS hosting because HttpContext seems to be lost when i'm doing async work (using TPL calls, not async/await due to some interface that needs to be matched) in services/repos layers (HttpContext.Current becomes null). I'm using unity 3.5 and cannot achieve to do a proper per request injection. Tried the HttpControllerActivator method : public class

How to register multiple types that implement the same interface

大兔子大兔子 提交于 2019-12-06 03:34:27
I have a single interface and this is being used by 2 classes. I am using unity configuration to identify the instance based on the interface. Now I want to know how should i register these types so that i can call the appropriate implementation based on the single interface itself. This is how I do it: var container = new UnityContainer().RegisterType<IAmImplementedMoreThanOnce, Implementation1>("Implementation1") .RegisterType<IAmImplementedMoreThanOnce, Implementation2>("Implementation2") .RegisterType<IHaveDependencies1, WithDependenciesImplementation1>(new InjectionConstructor(new

How can I dynamically register generic classes with a name with Unity?

℡╲_俬逩灬. 提交于 2019-12-06 02:35:14
问题 I have an assembly with a lot of classes (300+) with a BaseClass and I want register a generic class with a interface. With unity you have to register by { Name } if you want to resolve an array of objects of the interface. I want an array of objects in the MainViewModel automatically. Is there a way to automate this with reflection? Any suggestions? Example (pseudo): public class BaseClass { public void doFoo(); } public ClassNumber001 : BaseClass { } public ClassNumber002 : BaseClass { }

How to prevent Unity overwriting existing mappings with AutoRegistration

送分小仙女□ 提交于 2019-12-06 02:31:27
Unity 3 offers new capabilities for AutoRegistration (Registration by Convention) such as: container.RegisterTypes( AllClasses.FromLoadedAssemblies(), //uses reflection WithMappings.FromMatchingInterface, //Matches Interfaces to implementations by name WithName.Default); This code will register all types that implement their similarly named interfaces, against those interfaces. For example, class MyService : IMyService will be registered automatically as though you had written the following: container.RegisterType<IMyService, MyService >(); My Question: What if I want this most of the time,

Register a type with primitive-arguments constructor?

狂风中的少年 提交于 2019-12-06 02:16:19
问题 I have a class that has in its constructor some primitive type arguments, such as string etc. How should I register the type with unity container? public LoginManager( IRegionManager regionManager, IEventAggregator eventAggregator, string mainRegionName, Uri login, Uri target) { this.regionManager = regionManager; this.eventAggregator = eventAggregator; this.mainRegionName = mainRegionName; this.login = login; this.target = target; } } Update : Remeber that the IRegionManager and the

IOC with Entity Framework

纵然是瞬间 提交于 2019-12-06 01:43:31
问题 I am trying to make use of Unity Framework with Entity Framework. Let me explain the scenario. Let's say I have a Database with 5 tables. I will have 5 interfaces each of them corresponding to one table in Database having each field of table as a member. Now I want my Entity Framework generated classes to implement corresponding table interface. All navigational properties should return objects as interface references. This should allow me to resolve these entities using Unity framework to

Dependency injection vs assembly dependencies

岁酱吖の 提交于 2019-12-06 01:43:07
问题 Say, that I have the following project structure: Application <-> BusinessLogic <-> DataAccessLayer I've prepared all types to use poor-man's-dependency-injection and now I want to introduce the real one using Unity. But I'm struggling on where to put the dependency container and its configuration (i suppose I'll configure it from code). DataAccessLayer needs to register Context (EF) BusinessLogic needs to register data repositories (which use context) Application needs to register services