C# InvalidOperationException:无法解析.Net Core类型的服务
我试图在.NETCore中构建一个简单的应用程序,我认为我配置的一切都是正确的。有人知道这是什么类型的DI错误吗,每次我打电话: Startup.cs:C# InvalidOperationException:无法解析.Net Core类型的服务,c#,.net,asp.net-mvc,asp.net-core,C#,.net,Asp.net Mvc,Asp.net Core,我试图在.NETCore中构建一个简单的应用程序,我认为我配置的一切都是正确的。有人知道这是什么类型的DI错误吗,每次我打电话: Startup.cs: // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ser
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//ApplicationServices
services.AddScoped<ICustomerAppService>(sp => sp.GetService<CustomerAppService>());
}
CustomerAppService.cs:
public class CustomerAppService : ICustomerAppService
{
private readonly IRepository<Customer> _customerRepository;
public CustomerAppService(IRepository<Customer> customerRepository)
{
_customerRepository = customerRepository;
}
public CustomerDto GetCustomerById(int id)
{
return Mapper.Map<CustomerDto>(_customerRepository.GetById(id));
}
}
公共类CustomerAppService:ICCustomerAppService
{
私人只读电子存储库(customerRepository);;
公共CustomerAppService(IRepository customerRepository)
{
_customerRepository=customerRepository;
}
公共CustomerTo GetCustomerById(内部id)
{
返回Mapper.Map(_customerRepository.GetById(id));
}
}
您是否已注册CustomerAppService
?如果您在ConfigureServices
中没有注册,您的注册可能如下所示:
services.AddScoped<ICustomerAppService, CustomerAppService>();
services.addScope();
您是否已注册CustomerAppService
?如果您在ConfigureServices
中没有注册,您的注册可能如下所示:
services.AddScoped<ICustomerAppService, CustomerAppService>();
services.addScope();
您在启动时有一行奇怪的代码:
services.AddScoped<ICustomerAppService>(sp => sp.GetService<CustomerAppService>());
您在启动时有一行奇怪的代码:
services.AddScoped<ICustomerAppService>(sp => sp.GetService<CustomerAppService>());
我认为您忘记在您的CustomerApps服务中注册IRepository
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<ICustomerAppService, CustomerAppService>();
services.addScope(typeof(IRepository)、typeof(Repository));
services.addScope();
我想你忘了在CustomerApps服务中注册IRepository
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<ICustomerAppService, CustomerAppService>();
services.addScope(typeof(IRepository)、typeof(Repository));
services.addScope();
services.AddScoped()为什么不呢代码>?为什么不干脆services.addScope()代码>?这实际上是正确的,但不是所示错误的原因。我的答案将修复当前的问题,然后抛出另一个问题,您的将修复:)这实际上是正确的,但不是所示错误的原因。我的答案将解决当前的问题,然后抛出另一个问题,你的将解决:)是的,我做到了。现在,我在尝试激活“Application.Core.Services.Customers.CustomerAppService”时遇到以下错误:InvalidOperationException:无法解析类型为“Application.Core.Interfaces.IRepository”的服务。是的,我这样做了。现在,我收到以下错误:InvalidOperationException:无法解析类型“Application.Core.Interfaces.IRepository”“1[iNail.DataAccess.Customers.Customer]”的服务,同时尝试激活“Application.Core.Services.Customers.CustomerAppService”。