非泛型方法“IServiceProvider.GetService(Type)"不能与类型参数一起使用 [英] The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments

查看:40
本文介绍了非泛型方法“IServiceProvider.GetService(Type)"不能与类型参数一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET Core 依赖项注入,但是当我尝试在另一个类中获取服务时,出现IServiceProvider.GetService(Type)"无法与类型参数一起使用的错误.

I am using .NET Core dependency injection, but when I am trying to get the service in another class, I am getting the 'IServiceProvider.GetService(Type)' cannot be used with type arguments' error.

这个错误是什么意思?我知道泛型类型参数是这样的:GenericInterface<>,并且 GetService 方法不将 GenericInterface<> 作为参数.

What does this error means? I understand that a generic type argument is something like this: GenericInterface<>, and the GetService method does not take the GenericInterface<> as an argument.

为什么我会收到这个错误,我该如何解决?

Why am I getting this error and how do I solve it?

界面

public interface IService
{
   void Process();
}

实现接口的类

public class Service : BaseService<IConfig, SomType>
{
    public Service(
        ILogger logger : base(logger)
    {
    }

    ...
}

BaseService 类是一个抽象类,它实现了 IService 接口.

The BaseService class is an abstract class and it implements the IService interface.

public abstract class BaseService<TConfig, TE> : AnotherBaseService, IService where TConfig : IConfig where TE : struct, IConvertible
{
      protected BaseService(ILogger logger): base(logger)
      {
      } 

      ...
}

另一个基础服务

public abstract class BaseService
{
   protected readonly ILogger Logger;

   protected BaseService(ILogger logger)
   {
      Logger = logger;
   }

   ...
}

我是如何注册它们的.

serviceCollection.AddScoped<IService, Service>();

我如何获得所需的服务.

How I am getting the service I need.

var incoming = serviceProvider.GetService<IService>();

注意:我刚刚开始使用依赖注入和 .NET Core,对 DI 一无所知.你的回答会很有帮助.

NB: I am just getting started with dependency injection, .NET Core and do not know everything about DI just yet. Your answers would be of great help.

推荐答案

通用 GetService 方法是一种扩展方法.这意味着你需要有一个 :

The generic GetService< T> method is an extension method. This means you need to have a :

using Microsoft.Extensions.DependencyInjection;

允许编译器找到它.

此方法仅适用于可选服务.如果无法构造对象,它将返回 null,或者因为类型没有注册或者因为它的一些依赖项丢失.

This method is only meant for optional services. It will return null if the object can't be constructed, either because the type wasn't registered or because some of its dependencies are missing.

GetRequiredService 应该在应用程序无法工作时使用,除非有服务可用.如果无法创建实例,则会抛出 InvalidOperationException.

GetRequiredService should be used when an application can't work unless a service is available. If an instance can't be created, it will throw an InvalidOperationException.

当抛出该异常时,完整的异常文本将对查找实际问题有巨大帮助.构造函数中抛出的异常可以出现在 Exception.InnerException 属性.以抛出异常而告终的调用序列将出现在 StackTrace 属性.调用 Exception.ToString() 将返回一个字符串,其中包含当前异常和任何内部异常的所有信息.

When that exception is thrown, the full exception text will be a huge help in finding the actual problem. Exceptions thrown in constructors can appear in the Exception.InnerException property. The sequence of calls that ended up in an exception being thrown will appear in the StackTrace property. Calling Exception.ToString() will return a string that contains all of that information for the current exception and any inner exceptions.

这篇关于非泛型方法“IServiceProvider.GetService(Type)"不能与类型参数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆