【问题标题】:System.InvalidOperationException: Unable to resolve service for type while attempting to activateSystem.InvalidOperationException:尝试激活时无法解析服务类型
【发布时间】:2019-10-18 07:17:12
【问题描述】:

我正在尝试使用 Hangfire 执行循环作业,但出现此异常

 System.InvalidOperationException: Unable to resolve service for type 'MyModel' while attempting to activate 

注意:它会在 我的模型 而不是 我的服务 上引发异常,所以我不明白这一点。我还在当前类的构造函数中注入了该模型。

以前有人遇到过这个问题吗?提前致谢。

更新 我确实注册了我的所有模型,但最深的模型我将字符串传递给构造函数,它现在显示:

 System.InvalidOperationException: No constructor for type 'System.String' can be instantiated using services from the service container and default values.

【问题讨论】:

  • 您的服务可能有构造函数或任何预期的参数,因此您的服务不会被容器解析
  • 你用的是 ASP.NET 还是 ASP.NET CORE?
  • 另外请展示一些你的容器和构造函数的代码。

标签: c# asp.net hangfire


【解决方案1】:

我猜 Asp.Net 默认依赖注入没有为你的类型定义任何东西

试试这样的

services.AddScoped<IMyService>(_ => new MyService("value here"));

在你的情况下,你必须初始化你的模型。

【讨论】:

  • 我的服务器将字符串放入参数中,现在它显示以下消息:System.InvalidOperationException: No constructor for type 'System.String' can be instantiated using services from the service container and default values.
猜你喜欢
  • 1970-01-01
  • 2022-09-24
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多