Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# InvalidOperationException:无法解析.Net Core类型的服务_C#_.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# InvalidOperationException:无法解析.Net Core类型的服务

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

我试图在.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)
    {
        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”。