参数错误导致的依赖注入的错误提示:A suitable constructor for type 'MyApp.OptionMethodSetting' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

  • A+
所属分类:.NET技术
摘要

前几天做一个项目,发生一个奇怪的错误System.InvalidOperationException:“A suitable constructor for type ‘MyApp.OptionMethodSetting’ could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.” 

前几天做一个项目,发生一个奇怪的错误

System.InvalidOperationException:“A suitable constructor for type 'MyApp.OptionMethodSetting' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.” 

参数错误导致的依赖注入的错误提示:A suitable constructor for type 'MyApp.OptionMethodSetting' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

 

通常情况下这是由于依赖注入的实现无法访问到造成的,可是实际看了代码,该访问的都是public,而且这段代码是从另一个项目带过来的,另一个项目跑的好好的,这还真是奇怪了。

中间折腾了半天,发现是由于这个中间件有一个参数传递了null造成的。

参数错误导致的依赖注入的错误提示:A suitable constructor for type 'MyApp.OptionMethodSetting' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

 

就是这个罪魁祸首。

怎么会传null呢?

项目是拷过来的,所以很多这边用不上的配置,就删除了,结果导致这个也在appsetting.json里被误删了,结果导致读取时不存在,传递了null

参数错误导致的依赖注入的错误提示:A suitable constructor for type 'MyApp.OptionMethodSetting' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

 

 这个是祸根。

记录下,备忘。下次发生这样的问题的时候,查下是不是参数有问题。