unity-container

Is there TryResolve in Unity?

早过忘川 提交于 2019-11-28 11:59:22
How can I make Unity not to throw ResolutionFailedException if Resolve fails? Is there something like TryResolve<IMyInterface> ? var container = new UnityContainer(); var foo = container.TryResolve<IFoo>(); Assert.IsNull(foo); Johan Leino This has been an issue on the codeplex site, you can find the code here (look at the bottom of that thread and they have made an extension method...very handy) http://unity.codeplex.com/Thread/View.aspx?ThreadId=24543 and the you can use code like this: if (container.CanResolve<T>() == true) { try { return container.Resolve<T>(); } catch (Exception e) { // do

Bootstrapping Unity - Composition Root location

霸气de小男生 提交于 2019-11-28 11:51:24
I have a simple .NET project and I am wondering what's the best approach for bootstrapping Unity. I started with a WebApp with a bunch of controllers. Each of these controllers has its own Handler class to which the controller delegates the implementation. Something in the lines of: public class UsersHandler : IUsers { IAuthenticationClient authenticationClient; ILogger logger; public UsersHandler(IAuthenticationClient authClient, ILogger logger) { ... } } In the Application_Start method of the Global.asax I am creating the UnityContainer and registering types. There's a second project (Class

How to inject constructor in controllers by unity on mvc 4

十年热恋 提交于 2019-11-28 11:29:08
问题 I'm using unity 1.1 version and i couldn't inject constructor. My code sush as: Registering dependencies in global.asax Application_Startup method: Core.Instance.Container.RegisterType<ICartBusiness, CartBusiness>(); Injecting constructor: private ICartBusiness _business; public FooController(ICartBusiness business) { _business = business; } mvc throwing this exception: No parameterless constructor defined for this object. P.S: i can't use any new version of unity because i'm using too

using a Handler in Web API and having Unity resolve per request

人盡茶涼 提交于 2019-11-28 11:11:36
I am using Unity as my IoC framework and I am creating a type based on the value in the header of each request in a handler: var container = new UnityContainer(); container.RegisterType<IFoo,Foo>(new InjectionConstructor(valuefromHeader)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); The problem is that the handler's SendAsync means that the global container is getting overwritten by different requests and the controllers that use IFoo in their constructor are getting the wrong values. 1) Can I make the SendAsync sync? 2) If not,

How to inject webapi AccountController in WebApi

我怕爱的太早我们不能终老 提交于 2019-11-28 10:51:36
问题 I have default constuctor and constructor with params like here: public class AccountController : ApiController { private const string LocalLoginProvider = "Local"; private ApplicationUserManager _userManager; public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; } [Dependency] public IRepository Repository{ get; private set; } public AccountController() { } public AccountController(ApplicationUserManager userManager, ISecureDataFormat<AuthenticationTicket>

How to do open generic decorator chaining with unity + UnityAutoRegistration

我们两清 提交于 2019-11-28 10:34:52
Went off on an interesting tangent today after reading this article on command handler decoration . I wanted to see if I could implement the pattern using Unity instead of SimpleInjector , and so far it is proving extremely difficult. The first thing I had to do was install UnityAutoRegistration to resolve the open generic ICommandHandler<TCommand> interface. Current solution for that aspect is as follows: Container = new UnityContainer().LoadConfiguration(); Container.ConfigureAutoRegistration() .ExcludeSystemAssemblies() .Include(type => type.ImplementsOpenGeneric(typeof(ICommandHandler<>)),

Using ASP.NET Session for Lifetime Management (Unity)

◇◆丶佛笑我妖孽 提交于 2019-11-28 09:46:26
I am considering using Unity to manage the lifetime of a custom user class instance. I am planning on extending the LifetimeManager with a custom ASP.NET session manager. What I want to be able to do is store and retrieve the currently logged in user object from my custom classes, and have Unity get the instance of User from the session object in ASP.NET, or (when in a Win32 project) retrieve it statically or from the current thread. So far my best solution is to create a static instance of my Unity container on startup, and use the Resolve method to get my User object from each of my classes.

Can I register my types in modules in Unity like I can in Autofac?

拜拜、爱过 提交于 2019-11-28 09:16:31
I am fairly familiar with Autofac and one feature that I really love about Autofac is the registering of modules. Does anyone know how I can do this with Unity? I'm having a hard time finding which terms to use in Google to come up with the unity equivalent if there is one. public class Global : HttpApplication, IContainerProviderAccessor { private static IContainerProvider _containerProvider; protected void Application_Start(object sender, EventArgs e) { var builder = new ContainerBuilder(); builder.RegisterModule(new MyWebModule()); _containerProvider = new ContainerProvider(builder.Build())

ASP.NET MVC inject per request

喜夏-厌秋 提交于 2019-11-28 08:48:35
I need to inject EF context per request. Is there any way to implement it? The solution proposed in the Unity Discussion list is to create a child container per request, have that child container create the EF context as ContainerControlledLifetime, then have the child container disposed at the end of the request. By doing so you don't have to create a custom LifetimeManager. I'm not very familiar with Unity but the principle would be something like this: Application_BeginRequest(...) { var childContainer = _container.CreateChildContainer(); HttpContext.Items["container"] = childContainer;

Unity Register For One Interface Multiple Object and Tell Unity Where to Inject them

天大地大妈咪最大 提交于 2019-11-28 08:33:11
Hi I have been having trouble trying to tell Unity that for an Interface if it has multiple implementations , I want it to inject them in different classes.Here is what I mean: Let's say I have an interface IProductCatalogService and two implementations ProductCatalog : IProductCatalogService and ProductCatalogService : IProductCatalogService . How would I go about telling Unity that for Class A I want in my constructor passed an instance of type ProductCatalog and for Class B I want an instance of ProductCatalogService . I am using Unity in an ASP.NET Web API project and I have set the