unity-container

Using LogManager.GetLogger with Unity

萝らか妹 提交于 2019-12-04 13:08:08
问题 Given this class: class Foo { readonly ILog log; public Foo(ILog log) { this.log = log; } ... } I'd like to configure Unity to inject ILog. That's easy: container.RegisterInstance<ILog>(LogManager.GetLogger(typeof(XYZ))); But I'd like to make Unity call LogManager.GetLogger with the type of the parent type being resolved. This is close: container.RegisterType<ILog>(new InjectionFactory((c, t, s) => LogManager.GetLogger(t))); But t in this case is the type being resolved ( ILog ), not the type

Is there a way to include an assembly referenced using Unity when using publish website?

北慕城南 提交于 2019-12-04 12:26:54
I'm a using the repository pattern and Unity to manage the dependency to my concrete repository objects. In itself this is not an issue however I am running into a problem when using publish website in both VS and the TFS build process. I believe the problem is being caused by the fact that although the project which contains the concrete repository objects is referenced in my application, the classes that it contains are never used directly. This is because I am using Unity to create instances of the concrete objects at runtime using the Unity config held in my web.config. The repository

Asp.net Identity, Generate WebApi token OAuthGrantResourceOwnerCredentialsContext - no access to UserManager using Unity

瘦欲@ 提交于 2019-12-04 12:15:17
I am trying to setup a project structure so that I have a WebApi, WebUI and Domain layer. I have moved all the Asp.Net.Identity objects into the Domain layer and have also setup the ApplicationContext here too (inheriting from IdentityContext). (I have used this tutorial and package as a base which is excellent. http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/ ) In the WebAPI layer I am able to use the Account controller correctly to login and register. However, I cannot generate an access token. The OAuthGrantResourceOwnerCredentialsContext method internally

Unity Dependency Injection with Global Web API Filter Attribute

南笙酒味 提交于 2019-12-04 11:44:17
问题 Referencing this CodePlex unity article I was able to get filter attribute working with a WebAPI controller as follows: [MyFilterAttribute] public class TestController : ApiController {} However, if I want to apply my filter attribute across all actions with a GlobalConfiguration it gets stripped of the injected dependency: public class MyFilterAttribute : ActionFilterAttribute { [Dependency] public MyDependency { get; set; } public override void OnActionExecuting(HttpActionContext

RegisterType with an interface in UnityContainer

限于喜欢 提交于 2019-12-04 11:15:38
I'm using UnityContainer, and I want to register an interface not with a type, but with another interface. Unfortunately, I'm unable to do it cleanly.. I have several common interfaces, which are united in one interface, and I need to register them in the container. The code is like the following: interface IDeviceImporter { void ImportFromDevice(); } interface IFileImporter { void ImportFromFile(); } interface IImporter : IDeviceImporter, IFileImporter { } class Importer1: IImporter { } class Importer2: IImporter { } When entering the library, I know which importer to use, so the code is like

Using Unity DI with a Console Application

别等时光非礼了梦想. 提交于 2019-12-04 11:05:31
Im trying to get Unity to work with my console application, however.. all the properties that I try to dependency inject are still set to null. This is my code: Program.cs namespace .Presentation.Console { class Program { static void Main(string[] args) { var mainThread = new MainThread(); } } } MainThread.cs namespace xxxx.Presentation.Console { public class MainThread { public IUnitOfWork UnitOfWork { get; set; } public IMapper Mapper { get; set; } public MainThread() { Mapper.RegisterMappings(); } } } App.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections>

Unity 3 Configuration By Convention not finding Types in Web Project

好久不见. 提交于 2019-12-04 10:42:59
I am trying to get this convention configuration working but I am having a problem in my ASP.NET MVC5 Project.. I have added the following in my Application_Start method and hooked it up to DependencyResolver public static IUnityContainer CreateContainer() { IUnityContainer container = new UnityContainer(); container.RegisterTypes( AllClasses.FromAssembliesInBasePath(), WithMappings.FromAllInterfaces, WithName.Default, WithLifetime.ContainerControlled); return container; } But it fails to register any types, on closer inspection, when I see whats in AllClasses.FromAssembliesInBasePath() it

MVC Integration tests with Unity IoC

对着背影说爱祢 提交于 2019-12-04 09:39:52
Am trying Unity IoC, after using constructor based DI. Problem is trying to get integration tests working. http://patrick.lioi.net/2013/06/20/streamlined-integration-tests/ "Running your integration tests should exercise as much of the real system as is reasonably possible" Patrick above describes setting up an IoC inside the MVC Unit test project.. but I'm stuck as to how to implement public class HomeController : Controller { readonly IWinterDb db; // Unity knows that if IWinterDb interface is asked for, it will inject in a new WinterDb() public HomeController(IWinterDb db) { this.db = db; }

How to registerType with a PARAMETER constructor?

落花浮王杯 提交于 2019-12-04 08:03:17
问题 How do I registertype with the container where the type doesn't have NO PARAMETER constructor. In fact my constructor accepts a string, and I normally pass in a string that represents a Path. So when I do resolve it automatically creates the new type but passing in a string? 回答1: It's simple. When you register the constructor, you just pass the value you want injected for the parameter. The container matches up your constructor based on the type of value (API) or name of parameter (XML). In

WebApi equivalent for HttpContext.Items with Dependency Injection

末鹿安然 提交于 2019-12-04 07:31:19
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 HttpControllerActivator : IHttpControllerActivator { private readonly IUnityContainer _container;