在.NET Core中注册通用类型时遇到的问题。

huangapple go评论48阅读模式
英文:

Problem registering a generic type with DI .net core

问题

I can help you with the translation. Here's the translated portion:

我正在尝试注册用于GraphQL的一些类型,但是对于使用泛型的特定类会出现异常。
该类定义如下:

public class ResponseGraphType<TGraphType, T> : ObjectGraphType<Response<T>> where TGraphType : GraphType
{
    public ResponseGraphType()
    {
...

并尝试这样注册类:

services.AddScoped(typeof(ResponseGraphType<,>), sp =>
{
    Type generic = typeof(ResponseGraphType<,>);
    Type constructed = generic.MakeGenericType();
    return (GraphType)Activator.CreateInstance(constructed);
});

但是当我启动应用程序时,我得到了这个异常:
>System.ArgumentException: 'Cannot instantiate implementation type 'XXX.XXX.XXX.XXX.ResponseGraphType`2[TGraphType,T]' for service type 'GraphQL.Types.IProvideMetadata'.'

IProvideMetadata是一个由以下类实现的接口:

public class MetadataProvider : IProvideMetadata
... 
public interface IProvideMetadata

有关如何注册ResponseGraphType的任何想法吗?

英文:

I'm trying to register some types used for graphql but I get an exception for a specific class that uses generics.
The class is defined like this:

public class ResponseGraphType<TGraphType, T> : ObjectGraphType<Response<T>> where TGraphType : GraphType
{
    public ResponseGraphType()
    {
...

And tried to register the class like this:

services.AddScoped(typeof(ResponseGraphType<,>), sp =>
{
    Type generic = typeof(ResponseGraphType<,>);
    Type constructed = generic.MakeGenericType();
    return (GraphType)Activator.CreateInstance(constructed);
});

But when I start the application I get this exception:
>System.ArgumentException: 'Cannot instantiate implementation type 'XXX.XXX.XXX.XXX.ResponseGraphType`2[TGraphType,T]' for service type 'GraphQL.Types.IProvideMetadata'.'

The IProvideMetadata is an interface implemented by

public class MetadataProvider : IProvideMetadata
... 
public interface IProvideMetadata

Any idea on how to register the ResponseGraphType?

答案1

得分: 1

不可能使用lambda/delegate语法注册开放泛型类型。只有封闭和非泛型服务类型可以使用委托注册。对于开放泛型类型,您需要依赖于自动装配注册语法,也就是您应该执行以下操作:

services.AddScoped(typeof(ResponseGraphType<,>), typeof(ResponseGraphType<,>));

通过这个注册,您指示MS.DI在用户请求该封闭版本的ResponseGraphType<TGraphType, T>时组合并返回适当的封闭版本。

英文:

It's not possible to register open-generic types using the lambda/delegate syntax. Only closed and non-generic service types can be registered using delegate. For open-generic types you need to depend on the Auto-Wiring registration syntax, meaning you should do the following:

services.AddScoped(typeof(ResponseGraphType<,>), typeof(ResponseGraphType<,>));

With this registration you instruct MS.DI to compose and return the appropriate closed version of ResponseGraphType<TGraphType, T> in case the user requests that closed version of ResponseGraphType<TGraphType, T>.

huangapple
  • 本文由 发表于 2023年4月19日 19:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定