我有基地controlller我嘗試屬性注射,但沒有工作...Autofac財產注射
public class BaseController : Controller
{
public ILoggingService loggingService { get; set; }
public BaseController()
{
}
}
這是我Autofac配置...
var builder = new ContainerBuilder();
// builder.Register(c => new BaseController { loggingService = c.Resolve<ILoggingService>() });
builder.RegisterControllers(Assembly.GetExecutingAssembly())
.PropertiesAutowired();
builder.Register(c => new BaseController()).OnActivated(e =>
{
e.Instance.loggingService = e.Context.Resolve<ILoggingService>();
});
builder.RegisterAssemblyTypes(Assembly.Load("Aizen.Services"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerRequest();
,則不應使用屬性注入擺在首位。建築師注入是建議的常見做法。 Autofac將支持開箱即用。 – Steven 2014-11-04 10:13:50