2

我正在使用 OWIN 中间件(因此使用 startup.cs 而不是 global.asax)在我的 ASP.NET MVC 5 Web 应用程序中连接 Autofac 依赖注入,并尝试使用属性注入在控制器中设置公共变量。

我正在玩属性注入,让 Autofac 自动在 LoginController 中设置 Test 属性。

public interface ITest
{
    string TestMethod();
}

public class Test : ITest
{
    public string TestMethod()
    {
        return "Hello world!";
    }
}

public class LoginController : Controller
{
    public ITest Test { get; set; }

    public LoginController()
    {
        var aaa = Test.TestMethod();

        // Do other stuff...
    }
}

这是我的 startup.cs 的样子。我一直在玩,所以其中一些代码可能不需要(或导致我的问题?)。

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        builder.RegisterType<Test>().As<ITest>().SingleInstance();
        builder.Register(c => new Test()).As<ITest>().InstancePerDependency();

        builder.RegisterType<ITest>().PropertiesAutowired();
        builder.RegisterType<LoginController>().PropertiesAutowired();

        builder.RegisterModelBinderProvider();
        builder.RegisterFilterProvider();

        var container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        app.UseAutofacMiddleware(container);

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        // Some other stuff...
    }
}

因此,“测试”公共属性始终为空,因此会在运行时中断。

有什么想法可能是我的问题吗?提前感谢您的帮助!:)

4

1 回答 1

6

因此,“测试”公共属性始终为空,因此会在运行时中断。

它并不总是为空。它在构造函数中为空,因为 Autofac(实际上是所有代码)在构造函数完成之前无法设置属性。

public class LoginController : Controller
{
    public ITest Test { get; set; }

    public LoginController()
    {
        // Test is null, will always be null here
        var aaa = Test.TestMethod();
    }
}

autofac 的超级虚拟化版本执行以下操作:

var controller = new LoginController();
controller.Test = new Test();

如果您需要在设置属性后执行代码,您可以执行以下操作(但实际上您应该只使用构造函数注入):

public class LoginController : Controller
{
    private ITest _test;
    public ITest Test 
    { 
      get { return _test; }
      set 
      {
        var initialize = (_test == null);
        _test = value;
        if (initialize)
        {
          Initialize();
        }
      }
    }

    public LoginController()
    {
    }

    private void Initialize()
    {
      var aaa = Test.TestMethod();
    }
}

同样,更合乎逻辑的方法是这样做:

public class LoginController : Controller
{
    private readonly ITest _test;

    public LoginController(ITest test)
    {
        _test = test;
        var aaa = _test.TestMethod();

        // Do other stuff...
    }
}
于 2017-02-17T00:01:20.160 回答