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

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

问题描述

我正在使用.NET Core依赖项注入,但是当我尝试在另一个类中获取服务时,我得到了'IServiceProvider.GetService(Type)'不能与类型实参一起使用的错误信息。



此错误是什么意思?
我知道通用类型参数是这样的:GenericInterface<>,而GetService方法不将GenericInterface<>作为参数。



为什么会出现此错误以及如何解决?



界面

 公共接口IService 
{
void Process();
}

实现接口的类

 公共类服务:BaseService< IConfig,SomType> 
{
公共服务(
ILogger记录器:基本记录器)
{
}

...
}

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

 公共抽象类BaseService< TConfig,TE>:AnotherBaseService,IService其中TConfig:IConfig其中TE:结构,IConvertible 
{
受保护的BaseService (ILogger记录器):基本(记录器)
{
}

...
}

AnotherBaseService

 公共抽象类BaseService 
{
受保护的只读ILogger记录器;

受保护的BaseService(ILogger记录器)
{
Logger =记录器;
}

.. 。
}

我如何注册它们。

  serviceCollection.AddScoped< IService,Service>(); 

我如何获得所需的服务。

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

NB:我刚刚开始使用依赖项注入,.NET Core,并且不了解DI的全部知识刚刚呢您的回答将有很大帮助。

解决方案

通用 GetService< T> 方法是扩展方法。这意味着您需要使用Microsoft.Extensions.DependencyInjection;

 

允许编译器找到它。



此方法仅适用于可选服务。如果无法构造该对象,则将返回 null ,原因是未注册类型或缺少某些依赖项。



GetRequiredService 。如果无法创建实例,它将抛出InvalidOperationException。



抛出该异常时,完整的异常文本将为 huge 帮助发现实际问题。构造函数中引发的异常可以出现在 Exception.InnerException 属性。以抛出异常而结束的调用顺序将显示在 StackTrace 属性。调用 Exception.ToString()将返回一个字符串,其中包含当前异常和任何内部异常的所有信息。

$

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.

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?

The interface

public interface IService
{
   void Process();
}

The class implementing the interface

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

    ...
}

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)
      {
      } 

      ...
}

The AnotherBaseService

public abstract class BaseService
{
   protected readonly ILogger Logger;

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

   ...
}

How I registered them.

serviceCollection.AddScoped<IService, Service>();

How I am getting the service I need.

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

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.

解决方案

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

using Microsoft.Extensions.DependencyInjection;

to allow the compiler to find it.

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 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.

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天全站免登陆