2

我在 mvc 中使用 SimpleInjector 和这段代码

public static class SimpleInjectorInitializer
{
    public static void Initialize()
    {
        var container = new Container();
        InitializeContainer(container);
        container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
        container.Verify();
        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    }

    private static void InitializeContainer(Container container)
    {
        container.Register<IPersonRepository, PersonEntityRepository>();
    }
}

但现在我想在我的 mvc 应用程序中测试LightInject Ioc 但是如何?

我写了这段代码

[assembly: WebActivator.PostApplicationStartMethod(typeof(LightInjectInitializer), "InitializeLI")]

public static class LightInjectInitializer
{
    public static void InitializeLI()
    {
        var container = new ServiceContainer();            
        InitializeContainer(container);
        container.RegisterControllers(Assembly.GetExecutingAssembly());
        container.EnableMvc();
    }

    private static void InitializeContainer(ServiceContainer container)
    {
        container.Register(typeof(PersonEntityRepository), typeof(IPersonRepository), new PerScopeLifetime());

    }
}

在此处输入图像描述

但告诉我这个错误

No parameterless constructor defined for this object. 

任何人都可以帮助我编写在 MVC 中工作的等效代码吗?

示例代码: 下载

4

2 回答 2

4

I am the author of LightInject and would like to help you out.

This static initialize method? Where is it called from. The reason that you get this error is probably because the initialize code did not execute.

If you would like further assistance, it would be valuable to see what you have in global.asax.cs.

This page also contains information about how to set this up.

http://www.lightinject.net/#mvc

于 2014-08-19T17:32:30.580 回答
0

请将控制器注册行更改为:

container.RegisterControllers(typeof(MVCApplicationNamespace.Controllers.HomeController).Assembly);
于 2014-12-18T15:55:07.690 回答