我试图访问我的 OrderService 中的 productservice,如下所示,但它会抛出错误
没有为此对象定义的无参数构造函数。描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。
//Many repositories are here just like 3 belows
private readonly IRepository<Store> _storeRepository;
private readonly IRepository<ProductPicture> _productPictureRepository;
private readonly IRepository<Picture> _picture;
private readonly IProductService _productService;
//private readonly IProductService _productService = EngineContext.Current.Resolve<IProductService>();
//tried above line but with no success
//Constructor for OrderService goes below
public OrderService(IRepository<Store> storeRepository,
IProductService productService
)
{
_storeRepository = storeRepository;
_productService= productService;
}
如果我删除 productService 的引用,应用程序运行良好。我在网上和 nopcommerce 论坛上搜索了很多,但没有找到任何解决方案。
有帮助吗?
1 回答
问题是
IProductService
的实现没有提供无参数的构造函数,或者public ProductServiceImpl() { }
,因此依赖注入框架无法解析它。注册
IProductService
时,请确保指定要使用的构造函数或为注册的实现创建一个空构造函数。