我如何将Unity依赖注入任何我的类

时间:2015-11-25 21:52:51

标签: c# asp.net-mvc unity-container

在ASP.NET MVC5中,我只有 Saver

 public class Saver
 {
    private readonly ResponseModel _model;

    [Dependency]
    public SomeService SomeService { get; set; }


    public Saver(ResponseModel model)
    {
        _model= estateModel;
    }

    public void Save(){
        // here SomeService is null
    }        
 }

在UnityConfig.cs中我有这个

container.RegisterType<SomeService >(new TransientLifetimeManager());

然后在UnityMvcActivator.cs中我有了这个

        IUnityContainer container = UnityConfig.GetConfiguredContainer();

        FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
        FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        // TODO: Uncomment if you want to use PerRequestLifetimeManager
        DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

我该怎么做才能解决此错误。为什么要在控制器中注入工作,而不是在其他类中,这不是控制器?

0 个答案:

没有答案