unity-container

Implement Unity MVC 5 dependency injection in ActionFilter

一个人想着一个人 提交于 2019-12-08 01:39:17
问题 I'm trying to inject this user service via Unity (mvc5) in an actionfilter but it is null. How can I implement this? public class TestFilter : ActionFilterAttribute { // this is always null [Dependency] public IUserService UserService { get; set; } // other members } 回答1: You must register UnityFilterAttributeFilterProvider as a FilterProvider first. Modify the App_Start > UnityMvcActivator 's Start method like this: public static void Start() { var container = UnityConfig

Using Unity with Web Api 2 gives error does not have a default constructor

若如初见. 提交于 2019-12-08 00:42:26
问题 I have ASP.NET MVC5 web application and i also have Web API in the same application. I am uisng Unity (version 4) for DI. I am configuring the Unity container on APP start as below public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { UnityConfiguration.Config(); } } public class UnityConfiguration() { public void Config() { UnityContainer container = new UnityContainer(); container.RegisterType<IMyService, Myservice>(); container.RegisterType

Unity - Resolution of the dependency failed (without registering)

£可爱£侵袭症+ 提交于 2019-12-07 13:10:39
问题 I'm getting an error on this line of code: using (IMaterialClient rawMaterialServiceProxy = ServerUtility.Container.Resolve<IMaterialClient>()) The error: Resolution of the dependency failed... The current type, Xxx, is an interface and cannot be constructed. Are you missing a type mapping? I'm not registering a concrete IMaterialClient. In the Pluralsight video I just watched, they said that you don't have to register every type because Unity will find an implementation if one wasn't

ASP.NET 4.0 GridView OnRowEditing events not firing when using Unity 2.0 http module

扶醉桌前 提交于 2019-12-07 12:43:34
问题 I have an ASP.NET web forms site using Master Pages. It is using Unity as my IoC container. I have created an HTTP Module to build up the container using a couple of tutorials i found online. I need the dependency injection to work for User Controls and the only way i was able to get this to work was to hook into the Pages PreInit event as can be seen from the code below. public class UnityHttpModule : IHttpModule { public void Init(HttpApplication context) { context.PreRequestHandlerExecute

Api Controller has a parameterless public constructor error

拥有回忆 提交于 2019-12-07 11:33:32
问题 I am using Unity to do my DI and I have encountered the following error:- "An error occurred when trying to create a controller of type 'UploadController'. Make sure that the controller has a parameterless public constructor." I have the following UnityResolver:- public class UnityResolver : IDependencyResolver, IDependencyScope, System.Web.Http.Dependencies.IDependencyResolver { protected IUnityContainer container; public UnityResolver(IUnityContainer container) { if (container == null) {

Registering types with lambda expression

感情迁移 提交于 2019-12-07 09:42:56
问题 I was wondering how do I achieve such a feature in the UnityContainer: container.RegisterType<IDummy>(Func<IDummy>) // deferred resolution 回答1: If you're going to register factory instead of instance, try this: container.RegisterType<IDummy>(new InjectionFactory(context => new Dummy())); Just replace "context => new Dummy()" with your lambda. 来源: https://stackoverflow.com/questions/24256940/registering-types-with-lambda-expression

How to configure Unity to inject an array for IEnumerable

旧街凉风 提交于 2019-12-07 08:32:42
问题 I have a class which takes an IEnumerable constructor parameter which I want to resolve with Unity and inject an array of objects. These simple classes illustrate the problem. public interface IThing { int Value { get; } } public class SimpleThing : IThing { public SimpleThing() { this.Value = 1; } public int Value { get; private set; } } public class CompositeThing : IThing { public CompositeThing(IEnumerable<IThing> otherThings) { this.Value = otherThings.Count(); } public int Value { get;

An error occurred creating the configuration section handler

走远了吗. 提交于 2019-12-07 08:12:37
问题 I have a dot.NET 4.0 web application with a custom section defined: <configuration> <configSections> <section name="registrations" type="System.Configuration.IgnoreSectionHandler, System.Configuration, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/> .... at the end of the web.config file I have the respective section: .... <registrations> ..... </registrations> </configuration> Every

Authorization Filter Dependency Injection with ASP.New MVC 4 Web Api

Deadly 提交于 2019-12-07 07:31:50
问题 I'm trying to implement dependency injection on a MVC 4 Web Api Authorization Filter. I created a FilterProvider that inherits from ActionDescriptorFilterProvider: public class UnityWebApiFilterAttributeFilterProvider : ActionDescriptorFilterProvider, System.Web.Http.Filters.IFilterProvider { private readonly IUnityContainer _container; public UnityWebApiFilterAttributeFilterProvider(IUnityContainer container) { _container = container; } public IEnumerable<FilterInfo> GetFilters

Random errors occur with per-request DbContext

点点圈 提交于 2019-12-07 06:44:49
问题 I'm experiencing random errors (several per day) in my mvc+ef+unity application under higher load (10+ request per sec): The connection was not closed / The connection's current state is connecting deadlocks on Count queries (no explicit transaction) An item with the same key has already been added. in System.Data.Entity.DbContext.SetTEntity while resolving DbContext The remote host closed the connection. The error code is 0x80070057 There is already an open DataReader associated with this