unity-container

Register custom controller in runtime

北城以北 提交于 2019-12-25 03:05:56
问题 I've some api controller that i want to register at runtime in my mvc application. The controller itself resides into Class Library project and looks like: public class CustomController : ApiController { public string Get() { return "Hello"; } } I've compile that library (gave it a name Injection.dll ), and in my mvc app try to register those controller via Unity.Mvc library container public static class UnityConfig { public static void RegisterComponents() { var container = new

Unity dependency injection in an ASP MVC 5.0 RoleProvider defined in configuration

筅森魡賤 提交于 2019-12-25 02:09:38
问题 I'm trying to implement a role provider using a separate EF repository class, as an injectable dependency, to access my roles store. My problem is that the role provider is defined in configuration (web.config) and therefore is not instantiated via the Unity DI container. I haven't been able to find a way to either shift the configuration to code or get hold of the role provider after it's built to call container.BuildUP() on it. Any suggestions would be appreciated. 回答1: I think that my

Can someone explain the magic going on in Prism's resolve<> method?

て烟熏妆下的殇ゞ 提交于 2019-12-24 23:15:09
问题 I've got a CustomersModule.cs with the following Initialize() method: public void Initialize() { container.RegisterType<ICustomersRepository, CustomersRepository>(new ContainerControlledLifetimeManager()); CustomersPresenter customersPresenter = this.container.Resolve<CustomersPresenter>(); } The class I resolve from the container looks like this: class CustomersPresenter { private CustomersView view; private ICustomersRepository customersRespository; public CustomersPresenter(CustomersView

IRepository, IService, Unity in an ASP.NET MVC Application, reference question

可紊 提交于 2019-12-24 18:53:22
问题 I'm new to Unity, but this question is more generic to IoC, and I’m pretty new to implementing IoC as a whole. I have VS2010 solution with this project structure (simplified slightly): Business Objects – Folder DomainModel (Class Lib prj.) – Entity Framework 2 POCO entities Data Layer – Folder DataAccess (Class Lib prj.) – EF2 EDMX Repository (Class Lib prj.) – IRepository interface & repository concrete implementations Presentation Layer – folder WebUI – MVC Project Service Layer Service

WCF Custom ServiceBehavior/InstanceProvider with parameterless constructor

99封情书 提交于 2019-12-24 15:45:52
问题 We're trying to use Dependency Injection for a WCF Service. The Service has a dependency on a Unity Container. The container is used to find the appropriate class that implements an IJob Interface (based on a JobKey parameter in the method call) and calls a method on it. The Service is being hosted in MVC2. I've omitted as much irrelevant stuff as possible from the snippets below. Full code available if required... What I've done so far: Based on this MSDN Article, I've created a custom

C# EF6 make multiple async calls to one context using Unity - Asp.Net Web Api

拜拜、爱过 提交于 2019-12-24 12:23:43
问题 I get the following error when visiting my SPA site which makes some calls to the API when loaded: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe. If I don't use the same context and try to update values I get the following error, see this question: Entity Framework 6 -

WebActivatorEx.dll (Unity bootstrap) is firing on a different project

て烟熏妆下的殇ゞ 提交于 2019-12-24 12:04:30
问题 I have 3/4 projects all using their own Unity, with a bootstrap.cs file, these are from the Unity Mvc5 nuget package. When I start project 1, namespace (MvcAdmin) it is firing the bootstrap for project 2 (namespace MvcApi), which is causing an exception! I've checked and confirmed it's not caused by iis routes or anything. Also checked, all WebActivatorEx's are using different namespaces etc. This class below is what is firing, but it shouldn't be: [assembly: WebActivatorEx

How resolve a dependency with unity passing arguments in the constructor? [duplicate]

纵然是瞬间 提交于 2019-12-24 10:59:25
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can I pass constructor parameters to Unity’s Resolve() method? Constructor Injection in C#/Unity? It is possible to resolve a dependency with unity passing arguments in the constructor of the service to resolve ? If it is how I can do it ? 回答1: Create an InjectionConstructor and pass that to RegisterType() : var unityContainer = new UnityContainer(); ISnuh snuh = new Snuh(); InjectionConstructor

How should I register this type that has parameterized constructor, in Unity?

若如初见. 提交于 2019-12-24 10:49:50
问题 I am trying to register a type that has a constructor method like: public Foo (int myNumber, IBar bar) {...} I produce instance of IBar via Unity container. How should I regsiter and Resolve the Foo? Regarding to the example below, how can I replace the line new Foo... with a unity register and resolve way? public interface IBar { public void SayNumber(); } public class Bar : IBar { public void SayNumber() { Coonsole.Write("Number : "); } } public interface IFoo { void int GetMyNumberTimesTwo

Is CreateChildContainer() Thread-Safe?

扶醉桌前 提交于 2019-12-24 09:38:31
问题 Back in 2009, which may have been pre-Unity 2.0, I see a thread of discussion which suggests CreateChildContainer() is not thread safe: Also, if you create child container with CreateChildContainer() method, they will add themself to its parent container’s livetime container during creation and remove when disposing. So, disposing child containers from separate threads may lead to errors, even each child container accessed only by one thread at time. Is that indeed (still) true for Unity 2.0?