C# 在NET CORE 3中为DI注册服务时出现问题

C# 在NET CORE 3中为DI注册服务时出现问题,c#,dependency-injection,service,asp.net-core-3.0,C#,Dependency Injection,Service,Asp.net Core 3.0,我正在将依赖注入应用到我的dotnet3.1项目中。我在我的应用层中创建了一个类ServiceRegister: 名称空间Microsoft.Extensions.DependencyInjection { 公共静态类服务寄存器 { 公共静态IServiceCollection AddApplicationServices(此IServiceCollection@this) { //购物车服务 @这个.AddTransient(); @这个.AddTransient(); @这个.AddTran

我正在将依赖注入应用到我的dotnet3.1项目中。我在我的应用层中创建了一个类ServiceRegister:

名称空间Microsoft.Extensions.DependencyInjection
{
公共静态类服务寄存器
{
公共静态IServiceCollection AddApplicationServices(此IServiceCollection@this)
{
//购物车服务
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
//订单服务
@这个.AddTransient();
@这个.AddTransient();
//订单管理服务
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
//产品和服务
@这个.AddTransient();
@这个.AddTransient();
//产品管理服务
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
//股票管理服务
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
@这个.AddTransient();
//用户管理服务
@这个.AddTransient();
返回@this;
}
}
}
在我的startup类中,我添加了:

services.AddApplicationServices();
运行应用程序时,程序会出现以下错误:

ArgumentException: Cannot instantiate implementation type 'Microsoft.AspNetCore.Http.ISession' for service type 'Microsoft.AspNetCore.Http.ISession'
在addCustomerInformation类中,我有一个构造函数:

公共类AddCustomerInformation
{
专用只读会话;
公共地址CustomerInformation(ISession会话)
{
_会话=会话;
}
}
我不是DI方面的专家,我假设我也需要在我的serviceRegister类中以某种方式注册ISession,不确定我应该怎么做,有什么线索吗??我试图在登记簿中添加以下内容:

this.addScope()


但也不走运,它抛出一个错误,表示找不到会话,这是在添加了对Microsoft.AspNetCore.Http的引用之后发生的。我肯定我错过了什么,就是想不出来

您应该像这样注册依赖项:

services.AddScoped<IInterface, Implemantation>();
public class Service
{
    private readonly IInterface _interface;
    public Service(IInterface interface)
    {
        _interface = interface;
    }
}

你可以这样做<代码>服务。addScope()感谢您的回复@viveknuna,当我这样做时,它会发出这样的消息:会话找不到,您是否缺少指令或程序集引用?即使在我添加了对使用Microsoft.AspNetCore.Http的引用之后;这太奇怪了,因为你没有创建一个类
会话
来实现接口
会话
,因为它是一个Microsoft.AspNetCore.Http包,我认为这个会话应该已经实现了。检查你是否需要使用
AddSession
方法注入会话。谢谢你的回复@Givko!但那正是我在做的,不是吗?我的意思是,我尝试添加服务;但实现或“会话”以红色亮起,表示找不到我。它显示了当我把它放在启动类的configureServices和ServiceRegister中时的错误。不确定发生了什么,在您提供的代码中看不到会话和ISession。必须注册代码中显示的所有依赖项。即@this.AddTransient();你可以看到这篇文章:你完全正确,很抱歉,我在看一些视频系列,其中一个家伙错过了会话实现,我在想,实现是从nuget软件包的盒子里出来的,错了!!谢谢你,今天学到了一些新东西;)