unity-container

Implement Unity MVC 5 dependency injection in ActionFilter

∥☆過路亽.° 提交于 2019-12-06 13:51:24
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 } 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.GetConfiguredContainer(); FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>()

How can I implement Ninject .InSingletonScope() when using Unity IoC

浪子不回头ぞ 提交于 2019-12-06 11:20:05
问题 I was using ninject IoC in my application and in particular the following: kernel.Bind<RepositoryFactories>().To<RepositoryFactories>() .InSingletonScope(); I would like to implement this using the Unity IoC but can someone tell me how I can make it the same and also what does it mean "InSingletonScope()" ? The following works but I am worried that it's not being done correctly because of the Singleton that maybe needs to be specified. container.RegisterType<RepositoryFactories,

Unity: pass parameters to custom lifetime constructor, in xml configuration file

烈酒焚心 提交于 2019-12-06 11:14:35
I wrote my CustomLifetimeManager like this: public class CustomLifetimeManager <T> : LifetimeManager { private readonly string _arg; public CustomLifetimeManager(string arg) { _arg = arg; } } Now, it works easy configuring the container programmatically, but how add it in configuration file like the following? <type type="myTime" mapTo="myImpl"> <lifetime type="CustomLifetimeManager"/> </type> You need to add a second class: A TypeConverter. This class is responsible for taking a string and turning it into whatever type you want. Once you implement it, you can then do something like this in

Need complete DI sample for WCF

无人久伴 提交于 2019-12-06 11:02:32
Does anyone have a complete and working DI sample for WCF? Every sample I find just gets me more confused. Does anyone know of a complete and working standalone simple sample that works with the built in stuff? Maybe once I get a handle of the built in stuff, I can move on to different DI frameworks such as StructureMap or Unity with WCF. My MVC project is currently using Unity for all its DI. Mark Seemann The code download for my book Dependency Injection in .NET contains a full, working example. However, most of what you'll need to know is explained in this answer: Injecting data to a WCF

Unity 2.0 IOC Configuration about Generic class

走远了吗. 提交于 2019-12-06 07:46:34
I want some Repository class extend one common generic class to perform some common operation, problem is: how to config a UserExRepository type in config file. public class UserExRepository : Repository<User>, IUserEx { public UserExRepository(Context context):base(context){ } } public abstract class Repository<TObject> : IRepository<TObject> where TObject : class { protected Context Context = null; public Repository(Context context) { Context = context; } // do some common operation about entity, like create, delete... } You can configure binding generic to generic, generic to non-generic,

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

这一生的挚爱 提交于 2019-12-06 07:34:37
问题 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

RegisterType with an interface in UnityContainer

狂风中的少年 提交于 2019-12-06 06:42:22
问题 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 { }

Using Unity in WPF

半世苍凉 提交于 2019-12-06 06:37:56
问题 I have Unity 2.0 working well within the App.xaml.cs to register and resolve within that class. The question I have is regarding a best practice. I have a number of User Controls and other classes that also need to resolve some of the same and new Interface <-> implementations. The problem is theres no way to access the Unity container I created in the App.xaml.cs. I cannot use constructor or property injection to pass on the container reference. Just too many (its a large project) The user

PerThreadLifetimeManager in Unity

痞子三分冷 提交于 2019-12-06 06:27:18
In the Unity PerThreadLifetimeManager documentation, I read that: " This lifetime manager does not dispose the instances it holds ". Ref.: http://msdn.microsoft.com/en-us/library/ff647854.aspx So, if I am using a ThreadPool, does it mean that objects resolved using Unity on a Thread of the ThreadPool will not get disposed at the end of the work done in that thread before being returned to the pool? Any pattern or ideas how I can ensure that the objects do get disposed & I get a clean thread from the thread pool? Thanks! It depends on what type of lifetime manager. The PerThreadLifetimeManager

Using Unity DI with a Console Application

痞子三分冷 提交于 2019-12-06 06:21:20
问题 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