.NET Core调试时IOrderHelp服务解析失败异常求助
问题描述
调试.NET Core的C#项目时触发未处理异常,错误信息:
An unhandled exception occurred while processing the request
InvalidOperationException: Unable to resolve service for type 'PlatformWebApi.Controllers.Order.IOrderHelp' while attempting to activate
PlatformWebApi.Controllers.Order.OrderApi
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
相关代码:
OrderApi.cs
namespace PlatformWebApi.Controllers.Order { [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class OrderApi: Controller { public Log log = new Log(); private readonly WebOptions _webOption; private IWebApiHelp _WebApiHelp; private ISapFunction _sapFunction; private readonly IOrderHelp _orderHelp; public OrderApi(IOptions<WebOptions> myConfiguration, IWebApiHelp WebApiHelp, ISapFunction SapFunction, IOrderHelp OrderHelp) { _webOption = myConfiguration.Value; _WebApiHelp = WebApiHelp; _orderHelp = OrderHelp; _sapFunction = SapFunction; } [HttpPost(ApiRoutes.Order.AddUpdateOrder)] [Consumes("application/xml", "application/json")] public ResponseData AddUpdateOrder([FromBody] OrderXml.OrderDetails xmlString) { ResponseData oRes = _orderHelp.Add_updateOrder(xmlString); return oRes;//Json(oRes); } } }
IOrderHelp.cs
namespace PlatformWebApi.Controllers.Order { public interface IOrderHelp { ResponseData Add_updateOrder(OrderXml.OrderDetails xmlString); } }
OrderHelp.cs
public class OrderHelp :IOrderHelp { string error = ""; public Log log = new Log(); private readonly WebOptions _webOption; private IWebApiHelp _WebApiHelp; private ISapFunction _sapFunction; private ISAPConnectionHelp _SAPConnectionHelp; public OrderHelp(IOptions<WebOptions> myConfiguration, IWebApiHelp WebApiHelp, ISapFunction SapFunction, ISAPConnectionHelp SAPConnectionHelp) { _webOption = myConfiguration.Value; _WebApiHelp = WebApiHelp; _sapFunction = SapFunction; _SAPConnectionHelp = SAPConnectionHelp; } public ResponseData Add_updateOrder(OrderXml.OrderDetails xmlString) { string sErrorMsg = ""; int DocEntry = 0, res = 0; List<ResponseKeys> oListResponseKeys = new List<ResponseKeys>(); ResponseData oResponseData = new ResponseData(); SAPbobsCOM.Documents oOrder = (Documents)_SAPConnectionHelp.GetCompany().GetBusinessObject(BoObjectTypes.oOrders); // 省略后续代码 } }
解决方案
这个异常是因为.NET Core依赖注入容器中未注册IOrderHelp接口与它的实现类OrderHelp,导致框架无法创建OrderApi控制器实例。
需要在项目的服务配置文件中添加注册代码:
.NET 6+(Program.cs)
在builder.Build()之前添加:
// 注册IOrderHelp与实现类 builder.Services.AddScoped<IOrderHelp, OrderHelp>(); // 同时确保其他依赖服务已注册,替换为你的实际实现类 builder.Services.AddScoped<IWebApiHelp, WebApiHelp>(); builder.Services.AddScoped<ISapFunction, SapFunction>(); builder.Services.AddScoped<ISAPConnectionHelp, SAPConnectionHelp>();
.NET Core 3.x及更早版本(Startup.cs)
在ConfigureServices方法中添加:
public void ConfigureServices(IServiceCollection services) { // 其他服务注册代码... services.AddScoped<IOrderHelp, OrderHelp>(); services.AddScoped<IWebApiHelp, WebApiHelp>(); services.AddScoped<ISapFunction, SapFunction>(); services.AddScoped<ISAPConnectionHelp, SAPConnectionHelp>(); }
生命周期选择说明:
AddScoped:每个请求创建一个实例,适配Web API控制器场景AddTransient:每次获取服务时创建新实例AddSingleton:应用生命周期内仅创建一个实例
Web API场景下优先使用AddScoped即可。
内容的提问来源于stack exchange,提问作者Esther
相关产品推荐
相关产品推荐

