简体   繁体   English

Net Core - 无法解决依赖关系

[英]Net Core - unable to resolve dependency

I'm using GraphQL 2.4.0 with .NET Core 3.1 solution and Microsoft DI.我正在使用 GraphQL 2.4.0 和 .NET Core 3.1 解决方案和 Microsoft DI。 From within mutation, I need to perform additional processing (shown in MyMutation class below).在突变内部,我需要执行额外的处理(如下面的 MyMutation class 所示)。

My Schema.cs looks like:我的Schema.cs看起来像:

public class MySchema : GraphQL.Types.Schema
{
    private ISchema _schema { get; set; }
    public ISchema GraphQLSchema
    {
        get
        {
            return this._schema;
        }
    }

    public MySchema()
    {
        this._schema = Schema.For(@"
            type MyObj{
                id: ID
                name: String,
                type: String,
                createdAt: String
            }

            type Mutation {
                objSync(type: String): MyObj
            }

            type Query {
                myobj: MyObj
            }
        ", _ =>
        {
            _.Types.Include<MyQuery>();
            _.Types.Include<MyMutation>();
        });
    }
}

MyMutation.cs looks like: MyMutation.cs看起来像:

[GraphQLMetadata("MyMutation")]
public class MyMutation
{
    private readonly ISomeType someType;

    public MyMutation(ISomeType someType)
    {
        this.someType= someType;
    }

    [GraphQLMetadata("objSync")]
    public Document SyncObj(string type)
    {
        byte[] _bytes = null;
        _bytes = someType.Process(type).Result;
        return new Obj { File = Encoding.UTF8.GetString(_bytes , 0, _bytes .Length) };
    }
}

ISomeType looks like: ISomeType看起来像:

public interface ISomeType
{
    Task<byte[]> Process(string type);
}

and SomeManager.cs would be: SomeManager.cs将是:

public class SomeManager: ISomeType
{
    private readonly IConfiguration configuration;
    private readonly Func<string, IOtherTypeManager> otherType;

    public SomeManager(IConfiguration configuration, Func<string, IOtherTypeManager> otherType)
    {
        this.configuration = configuration;
        this.otherType= otherType;
    }

    public async Task<byte[]> Process(string type)
    {
       ...
    }
}

As you may notice, I need to resolve ISomeType with DI.您可能会注意到,我需要使用 DI 解析 ISomeType。 So Startup.cs looks like:所以Startup.cs看起来像:

...
public static void AddServices(this IServiceCollection services)
    {
        services.AddSingleton<IDependencyResolver>(_ => new FuncDependencyResolver(_.GetRequiredService));
        services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
        services.AddSingleton<IDocumentWriter, DocumentWriter>();
        services.AddSingleton<ISomeType, SomeManager>();
        ...
    }
...

When executed the program, it throws the following exception:当执行程序时,它会抛出以下异常:

Error trying to resolve objSync.
Failed to call Activator.CreateInstance. Type: MyNameSpace.Graphql.MyMutation

No parameterless constructor defined for type 'MyNameSpace.Graphql.MyMutation'.

at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
at GraphQL.DefaultDependencyResolver.Resolve(Type type)

Do I need to resolve the ISomeType differently?我需要以不同的方式解析ISomeType吗? Please guide.请指导。

Update更新

Getting rid of ISomeType from MyMutation.cs solves this problem... but I need it to call someType.Process(type).Result .MyMutation.cs ISomeType了这个问题......但我需要它来调用someType.Process(type).Result

MyMutation class also need to be registered in DI container. MyMutation class 也需要在 DI 容器中注册。 Can you try this out?你能试试这个吗?

services.AddSingleton<MyMutation>();
services.AddScoped<MyMutation>();
services.AddScoped<ISchema, MySchema>();

Basically I will follow this example: https://github.com/graphql-dotnet/examples/tree/master/src/StarWars基本上我会遵循这个例子: https://github.com/graphql-dotnet/examples/tree/master/src/StarWars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解决 net.core 3.0 中的依赖 HttpClient - Unable to resolve dependency HttpClient in net.core 3.0 .NET Core WebAPI 依赖注入解析 null - .NET Core WebAPI dependency injection resolve null .NET Core 依赖注入,解析泛型接口 - .NET Core dependency injection, resolve generic interface .NET 核心依赖注入解决问题 - .NET Core Dependency Injection resolve question .NET 核心依赖注入错误:“无法解析“AspNetCoreWebAPI.Data.IBookRepository”类型的服务” - .NET Core dependency injection error : "Unable to resolve service for type 'AspNetCoreWebAPI.Data.IBookRepository'" .NET Core 依赖注入; 尝试激活服务时无法解析“System.String”类型的服务 - .NET Core Dependency Injection; Unable to resolve service for type 'System.String' while attempting to activate Service 依赖注入无法解析在 Azure 中运行的 .NET Core Function App 中的类型的服务 - Dependency Injection unable to resolve service for type in .NET Core Function App running in Azure ASP.NET 核心依赖注入错误 - 尝试激活“服务”时无法解析“存储库”类型的服务 - ASP.NET Core Dependency Injection error - Unable to resolve service for type “Repository” while attempting to activate “Service” 无法解析依赖项“ JSON.NET” - Unable to resolve dependency 'JSON.NET' 无法在 .net core 3.1 中注入依赖项 - unable to inject dependency in .net core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM