unity-container

PerRequestLifetimeManager and Task.Factory.StartNew - Dependency Injection with Unity

不打扰是莪最后的温柔 提交于 2020-07-18 07:11:30
问题 How to manage new tasks with PerRequestLifeTimeManager? Should I create another container inside a new task?(I wouldn't like to change PerRequestLifeTimeManager to PerResolveLifetimeManager/HierarchicalLifetimeManager) [HttpPost] public ActionResult UploadFile(FileUploadViewModel viewModel) { var cts = new CancellationTokenSource(); CancellationToken cancellationToken = cts.Token; Task.Factory.StartNew(() => { // _fileService = DependencyResolver.Current.GetService<IFileService>();

Unity - Inject different classes for the same interface

妖精的绣舞 提交于 2020-07-04 07:48:39
问题 I have one interface: IFoo Two classes implementing that interface: FooOne and FooTwo And two classes ClassOne and ClassTwo receiving an IFoo parameter in the constructor. How I configure unity so ClassOne receives a FooOne instance and ClassTwo receives a FooTwo using only one container? I can't do it at runtime so it must be in the config file. 回答1: Have a look at the Unity documentation. For a more readable config file you should define type aliases for IFoo , FooOne , FooTwo , ClassOne

Resolving wrapper classes in C# with the Unity IoC container

放肆的年华 提交于 2020-06-22 10:55:09
问题 I want to use Unity resolve IService to two different implementations, to make use of a wrapper class, the equivalent of: IService service = new DispatcherService(new RealService(), Application.Current.Dispatcher); Where both DispatcherService and RealService implement the IService interface. I have a library containing some services with asynchronous operations. A simplified form of this service looks like this: public interface IService { IAsyncResult StartSomeOperation(); event

Resolve generic Microsoft.Extensions.Logging.ILogger<T> with Unity - get InvalidCastException

橙三吉。 提交于 2020-06-15 08:28:57
问题 I am trying to register a generic ILogger (from Microsoft.Extensions.Logging, not from Serilog) in Unity (version 4). I've got the following class: public class MyClass { private readonly Microsoft.Extensions.Logging.ILogger<MyClass> _logger; public MyClass(Microsoft.Extensions.Logging.ILogger<MyClass> logger) { _logger = logger; } } And the following Unity registrations and test: // Arrange IUnityContainer container = new UnityContainer(); // provider from Serilog.Extensions.Logging nuget

How to debug Unity resolution?

[亡魂溺海] 提交于 2020-03-18 10:36:32
问题 In a WPF project(with prism) we are using Unity as DI framework. Recently, after we merged two big branches, we were not able to start our application, we were having StackOverflowException . Due to the nature of the exception, we were not able to get his call stack or current values, we were only seing that the issue was comming from Unity due to the namespace. We spent more than 5 hours to find were the issue was occuring: Simplified: We were having 3-4 services(A, B, C and D), each one

Make sure that the controller has a parameterless public constructor error

北战南征 提交于 2020-01-26 07:57:10
问题 I have followed this tutorial which has worked great, until I modified my DbContext to have an additional constructor. I am now having issues with the resolution and not sure what to do to fix this. Is there an easy way to force it to grab the parameterless constructor or I am approaching this incorrectly? DbContext with two constructors: public class DashboardDbContext : DbContext { public DashboardDbContext() : base("DefaultConnection") { } public DashboardDbContext(DbConnection

Make sure that the controller has a parameterless public constructor error

ⅰ亾dé卋堺 提交于 2020-01-26 07:56:13
问题 I have followed this tutorial which has worked great, until I modified my DbContext to have an additional constructor. I am now having issues with the resolution and not sure what to do to fix this. Is there an easy way to force it to grab the parameterless constructor or I am approaching this incorrectly? DbContext with two constructors: public class DashboardDbContext : DbContext { public DashboardDbContext() : base("DefaultConnection") { } public DashboardDbContext(DbConnection

Make sure that the controller has a parameterless public constructor error

旧城冷巷雨未停 提交于 2020-01-26 07:56:05
问题 I have followed this tutorial which has worked great, until I modified my DbContext to have an additional constructor. I am now having issues with the resolution and not sure what to do to fix this. Is there an easy way to force it to grab the parameterless constructor or I am approaching this incorrectly? DbContext with two constructors: public class DashboardDbContext : DbContext { public DashboardDbContext() : base("DefaultConnection") { } public DashboardDbContext(DbConnection

Dependency injection not working in web api call

笑着哭i 提交于 2020-01-25 21:42:12
问题 Hi I am trying to build angular 2 web application using WebAPI, Entityframework that is loosely coupled using dependency injection. I am using unity for dependency injection. I have created multiple projects in one solution to address the separation concerns. I have configured the dependency in unity.config however when i execute the webapi application and type the following url http://localhost:8702/api/allcustomers , I get message saying the customer controller doesn't have parameter-less

Unable to register DbConnection with Unity and Entity Framework

旧巷老猫 提交于 2020-01-25 01:07:45
问题 I am not at all sure what the underlying problem is that is causing this exception. I am using ASP.NET MVC, with Unity.Mvc, and Entity Framework 6. I have the following code to register my repositories: public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. // container.LoadConfiguration(); // TODO: Register your types here // container