unity-container

MVC/Unity - How to inject dependencies into custom FilterAttributes?

大兔子大兔子 提交于 2019-12-08 18:03:02
问题 I Have a custom HandleErrorAttribute that extends FilterAttribute . How can I have Unity inject dependencies into this attribute at the same time that it injects the controller's dependencies itself? 回答1: Ok, figured it out. Mostly I used Ben's solution above from the blog post he pointed to. The problem is that Unity behaves a little differently. You can't inject dependencies on the filters directly, because they are of type IActionFilter and IExceptionFilter respectively. This led me to

How can you force Unity to Create a new instance?

爷,独闯天下 提交于 2019-12-08 17:43:28
问题 Using Unity Application block how can you force the Unity configuration to create a new instance of an object when we call the UnityContainer.Resolve<T>() method in WCF context? 回答1: Lifetime Manager in Unity is all what you need. By default, Unity use TransientLifetimeManager : TransientLifetimeManager. For this lifetime manager Unity creates and returns a new instance of the requested type for each call to the Resolve or ResolveAll method. This lifetime manager is used by default for all

Resolve multiple implementation of a single interface in unity

点点圈 提交于 2019-12-08 12:30:18
问题 I have the following scenario : public interface IFoo { } public interface IFoo3 { } public class Foo4 : IFoo3 { } public class Foo1 : IFoo { } public class Foo2 : IFoo { Foo2 (IFoo object1, IFoo3 object2) } on the client side : IUnityContainer container = new UnityContainer(); container.RegisterType<IFoo, Foo1>("Foo1"); container.RegisterType<IFoo3, Foo4>(); container.RegisterType<IFoo, Foo2>("Foo2"); IFoo3 obj = container.Resolve<IFoo3>(); //Resolve 1 IFoo obj2 = container.Resolve<IFoo>();

Register generic types with constructor in SimpleInjector how?

情到浓时终转凉″ 提交于 2019-12-08 09:23:49
问题 I use Microsoft Unity as IoC container and wrote some code like this: public static void RegisterTypes(IUnityContainer container) { MyContext ctx = new MyContext (); // is EntityFramework DbContext container.RegisterType(typeof(IEntityRepository<>), typeof(EntityRepository<>), new InjectionConstructor(ctx)); container.RegisterType(typeof(IEntityService<>), typeof(EntityService<>)); } It works for me without problem, but I want to know how to write its equivalent in SimpleInjector . 回答1: There

Automapper Random Errors

浪尽此生 提交于 2019-12-08 08:04:17
问题 2 months ago I have asked about this problem! and the problem still exists. I am not going to copy/paste the same problem here because I found out that the error is not for a specific Entity-DTO mapping but for any Entity-DTO which is first in a controller. I mean if the program flow hits to a Country-CountryDto, the error says: Missing type map configuration or unsupported mapping. Mapping types: Country -> CountryDTO MyApp.Domain.BoundedContext.Country -> MyApp.Application.BoundedContext

How can I register a type with dynamic string parameters in it's constructor at Composition root?

て烟熏妆下的殇ゞ 提交于 2019-12-08 07:10:43
问题 I have these constructors: public Configurator(string workingDirectory, string siteExclusions) : this(directory, exclusions, new ServerManager(), new DirectoryCheck()) { } public Configurator(string directory, string exclusions, IServerManager manager, IDirectoryCheck directoryCheck) { this.manager = manager; this.directoryCheck = directoryCheck; if (exclusions != null) { // do stuff } this.directory = directory; } Where directory and exclusions are created by the creating code, within a

Make sure that the controller has a parameterless public constructor in web api?

狂风中的少年 提交于 2019-12-08 06:15:24
问题 I am struggling with this problem of dependency injection using untiy . I have implemented according to this link https://www.asp.net/web-api/overview/advanced/dependency-injection But got this error: { "Message": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'WebAPIController'. Make sure that the controller has a parameterless public constructor.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web

How do I get Unity to inject a reference to HttpSessionState to a service

我只是一个虾纸丫 提交于 2019-12-08 05:57:34
问题 Disclaimer: I have a fair bit of experience with DI containers but am quite new to Unity. I have an MVC project that is all wired up with Unity DI using constructor injection and works great. But I now have a service that I want to inject into my controllers (and maybe places other than controllers at some point) and this service needs access to ASP.NET session state. The service's purpose is to manage a list in session and I don't want the list mechanics in my controller. I realize I could

Prism EventAggregator Exception - must be constructed on the UI thread

偶尔善良 提交于 2019-12-08 05:06:33
问题 I've just revisited some very old code to update it to the latest version of Prism (Version 5) and during Module initialisation I was getting the following exception message: Exception is: InvalidOperationException - To use the UIThread option for subscribing, the EventAggregator must be constructed on the UI thread. Wherever I was executing something like: eventAggregator.GetEvent<AppStatusMessageEvent>() .Subscribe(OnAppStatusChanged, ThreadOption.UIThread, true); Changing all of these

Types not resolving with Unity [MVC 5]

限于喜欢 提交于 2019-12-08 02:47:34
问题 I am using ASP.NET MVC 5 with Unity 3 and Unity bootstrapper for ASP.NET MVC. In the UnityConfig.cs file, I registered the following type in the RegisterTypes method: public static void RegisterTypes(IUnityContainer container) { container.RegisterType<IProjectRepository, ProjectRepository>(); } In an MVC controller, I am resolving like this: UnityContainer container = new UnityContainer(); public ActionResult List() { var projectRepository = container.Resolve<IProjectRepository>(); return