0

我有一个网站 asp.net MVC 解决方案

我的控制器有 2 个构造函数:一个无参数,一个参数很少。

用于通过带有参数的 ctor 的代码,使用 Unity 容器将它们注入 DI。

我已将我的网站合并到另一个网站。

现在我的控制器通过无参数ctor初始化。

我错过了什么?我的代码:

初始化代码被执行。

public static class Initializer
{
    private static bool isInitialize;
    private static readonly object LockObj = new object();
    private static IUnityContainer defaultContainer = new UnityContainer();

    static Initializer()
    {
        Initialize();
    }

    public static void Initialize()
    {
        if (isInitialize)
            return;

        lock (LockObj)
        {
            IUnityContainer container = defaultContainer;

            //registering Unity for web API
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
            //registering Unity for MVC
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            container.RegisterType<ICritieriaLoader, CritieriaLoader>();
            container.RegisterType<IAppsLoader, AppsLoader>();
            container.RegisterType<ISaveTestToBD, SaveTestToDB>();
            container.RegisterType<ITestOperations, TestOperations>();


            container.RegisterType<IServicesManager, ServicesManager>();
            container.RegisterType<IMamConfigurationManager, MamConfigurationsManager>();
            container.RegisterType<IAppsDataManager, AppsDataManager>();
            container.RegisterType<IMamConfiguration_V1Manager, MamConfiguration_V1Manager>();
            container.RegisterType<ICtidProd_V1Manager, CtidProd_V1Manager>();
            container.RegisterType<ICtidsAliasManager, CtidsAliasManager>();


            container.RegisterType<IDalFactory, DalFactory>();
            container.RegisterType<IDal<AppsDataBl>, AppsDataDal>();
            container.RegisterType<IApplicationLogger, Log4NetLogger>();
            container.RegisterType<IDal<MamConfigurationBL>, MamConfigurationBlDal>();
            container.RegisterType<IDal<Conduit.Mam.MaMDBEntityFramework.MamConfiguration>, MamConfigurationDal>();
            container.RegisterType<IDal<AddServiceBL>, NewService>();


            container.RegisterType<IAlias_V1UiToEfConvertor, Alias_V1UiToEfConvertor>(new ContainerControlledLifetimeManager());
            container.RegisterType<IMamConfiguration_V1UiToEfConvertor, MamConfiguration_V1UiToEfConvertor>(new ContainerControlledLifetimeManager());
            container.RegisterType<ICtidProdValidator, CtidPRodValidator>(new ContainerControlledLifetimeManager());
            container.RegisterType<IGeoProdValidator, GeoProdValidator>(new ContainerControlledLifetimeManager());
            container.RegisterType<IConstListsProvider, ConstListsProvider>(new ContainerControlledLifetimeManager());
            container.RegisterType<IEntityToUiConvertor, EntityToUiConvertor>();

            if (!isInitialize)
            {
                isInitialize = true;
            }
        }
    }
}

.

    protected void Application_Start()
    {
        Log.Application.Warn("Application started", "Application started");

        WebApiConfig.Configure(GlobalConfiguration.Configuration);

        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

        RouteConfig.RegisterRoutes(RouteTable.Routes);

        BundleConfig.RegisterBundles(BundleTable.Bundles);

        Initializer.Initialize();

.

[Authorize]
public class CtidProdController : SAPController
{

    #region Members

    private IApplicationLogger mApplicationLog { get; set; }

    //private IMamConfiguration_V1Manager mMamConfigurationManager { get; set; }

    private ICritieriaLoader mCritieriaLoader { get; set; }

    private IAppsLoader mAppsLoder { get; set; }

    private IMamConfiguration_V1UiToEfConvertor mMamConfiguration_V1UiToEfConvertor { get; set; }

    private CtidProdModel mCtidProdModel { get; set; }

    private IConstListsProvider mConstListsProvider { get; set; }

    private ICtidProd_V1Manager mCtidProd_V1Manager { get; set; }

    private ICtidsAliasManager mCtidsAliasManager { get; set; }


    #endregion


    #region CTOR

    public CtidProdController()
    {

    }


    public CtidProdController(IApplicationLogger logger, ICtidProd_V1Manager ctidProd_V1Manager,
        ICritieriaLoader critieriaLoader, IAppsLoader appsLoder, IMamConfiguration_V1UiToEfConvertor mamConfiguration_V1UiToEfConvertor,
        IConstListsProvider constListsProvider, ICtidsAliasManager ctidsAliasManager)
    {
        mApplicationLog = logger;

        mCtidProd_V1Manager = ctidProd_V1Manager;

        mCritieriaLoader = critieriaLoader;

        mAppsLoder = appsLoder;

        mMamConfiguration_V1UiToEfConvertor = mamConfiguration_V1UiToEfConvertor;

        mConstListsProvider = constListsProvider;

        mCtidsAliasManager = ctidsAliasManager;

        FillModel(mConstListsProvider);
    } 

    #endregion
    }

新网站代码: 注入代码:

public class NinjectDependencyScope : IDependencyScope
{
    private IResolutionRoot resolver;

    internal NinjectDependencyScope(IResolutionRoot resolver)
    {
        Contract.Assert(resolver != null);

        this.resolver = resolver;
    }

    public void Dispose()
    {
        IDisposable disposable = resolver as IDisposable;
        if (disposable != null)
            disposable.Dispose();

        resolver = null;
    }

    public object GetService(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has already been disposed");

        return resolver.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has already been disposed");

        return resolver.GetAll(serviceType);
    }
}

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
    private IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}
4

1 回答 1

0

就像达伦戴维斯指出的那样,您可能缺少一些属性。实际上,Unity 默认采用具有最多参数的构造函数,但要确保您有多个 ctor,您应该使用InjectionConstructor属性装饰所需的一个!希望这可以帮助。亲切的问候

于 2013-05-19T14:45:14.623 回答