Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ApicController上的Autofac注入属性与RegisterApicController结合使用_C#_Asp.net Web Api_Autofac_Autofac Configuration - Fatal编程技术网

C# ApicController上的Autofac注入属性与RegisterApicController结合使用

C# ApicController上的Autofac注入属性与RegisterApicController结合使用,c#,asp.net-web-api,autofac,autofac-configuration,C#,Asp.net Web Api,Autofac,Autofac Configuration,我想将autofac.json文件中的Modules和Data属性注入名为DataController 我读了这一页: 但是我的属性一直是空的,所以我一定是做错了什么。谁能帮我 我的Controller已经有一个构造函数,其中正在注入ILog的实例,因此这些属性是额外的 这是我现在拥有的Autofac配置: var builder=newcontainerbuilder(); builder.registerapicontroller(typeof(Startup.Assembly);//一次注

我想将
autofac.json
文件中的
Modules
Data
属性注入名为
DataController

我读了这一页: 但是我的属性一直是空的,所以我一定是做错了什么。谁能帮我

我的
Controller
已经有一个构造函数,其中正在注入
ILog
的实例,因此这些属性是额外的

这是我现在拥有的
Autofac
配置:

var builder=newcontainerbuilder();
builder.registerapicontroller(typeof(Startup.Assembly);//一次注册所有APIController
//将Microsoft配置扩展与Autofac一起使用,以创建Autofac模块
var autofacConfig=new ConfigurationBuilder();
autofacConfig.AddJsonFile(“autofac.json”);
var autofacModule=新配置模块(autofacConfig.Build());
生成器注册表模块(autofacModule);
var container=builder.Build();
var httpConfigurationConfig=new HttpConfiguration{dependencysolver=new autofacwebapidedencysolver(容器)};
app.useautofac中间件(容器);
app.UseAutofacWebApi(httpConfigurationConfig);
app.UseWebApi(httpConfigurationConfig);
这是我的
autofac.json

{
“组成部分”:[
{
“类型”:“Web.Controllers.DataController,Web”,
“注入属性”:真,
“财产”:{
“模块”:{
“模块1”:”http://localhost:12345/",
“模块2”:”http://localhost:12346/"
},
“数据”:{
“第1项”:“声明”,
“项目2”:“付款”
}
}
}
]
}
我的
控制器
如下所示:

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;
        }
    }
}
名称空间Web.Controllers
{
公共类数据控制器:ApiController
{
专用只读ILog_日志;

公共字典模块{get;set;}/我看不出您在WebApi管道的容器中插入了什么

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
此外,当需要属性注入时,需要使用PropertiesAutowired()方法


您提供的代码似乎很好-我复制了它,它工作得很好


您必须记住,属性是在组件创建之后注入的,因此在您的控制器构造函数中,这些属性是
null
,但是当您点击控制器方法时,它们会被您在json配置文件中提供的正确数据填充。

插入依赖项解析器,我已经更新了代码。
properties自动连线
没有什么区别。不幸的是,
模块
数据
不应该是属性而不是字段吗?有一刻我觉得很傻:-),但不幸的是,即使是属性,问题仍然存在。将
字典
更改为
IDictionary
会有区别吗?不,chan我需要把它变成一个嵌入式资源吗?不,我把它作为一个“内容”,而不把它放在输出目录中。我把它直接放在项目目录中,而不是任何子目录中
builder.RegisterApiControllers(typeof(Startup).Assembly).PropertiesAutowired();