我想将文件中的属性Modules
和Data
属性注入autofac.json
到我ApiController
的名字中DataController
我读了这个页面:
http ://docs.autofac.org/en/latest/configuration/xml.html但我的属性一直存在NULL
,所以我一定做错了什么。谁能帮我?
我Controller
已经有一个ILog
正在注入实例的构造函数,所以这些属性是额外的。
这是Autofac
我现在的配置:
var builder = new ContainerBuilder();
builder.RegisterApiControllers(typeof(Startup).Assembly); // to register all ApiControllers at once
// Use the Microsoft Configuration Extensions together with Autofac to create an Autofac Module
var autofacConfig = new ConfigurationBuilder();
autofacConfig.AddJsonFile("autofac.json");
var autofacModule = new ConfigurationModule(autofacConfig.Build());
builder.RegisterModule(autofacModule);
var container = builder.Build();
var httpConfigurationConfig = new HttpConfiguration { DependencyResolver = new AutofacWebApiDependencyResolver(container) };
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(httpConfigurationConfig );
app.UseWebApi(httpConfigurationConfig );
这是我的autofac.json
:
{
"components": [
{
"type": "Web.Controllers.DataController, Web",
"injectProperties": true,
"properties": {
"Modules": {
"Module1": "http://localhost:12345/",
"Module2": "http://localhost:12346/"
},
"Data": {
"Item1": "Declarations",
"Item2": "Payments"
}
}
}
]
}
我的Controller
样子是这样的:
namespace Web.Controllers
{
public class DataController : ApiController
{
private readonly ILog _log;
public Dictionary<string, string> Modules { get; set; } // << I want these to be injected!
public Dictionary<string, string> Data { get; set; } // <<
public DataController(ILog log) // gets injected
{
_log = log;
}
}
}