在 ASP.NET Core 中实现 GraphQL(用于 .Net 的 GraphQL),为什么我的 Query:ObjectGraphType 类没有被依赖注入注册?

tea*_*uit 6 c# asp.net-core-mvc .net-core graphql asp.net-core

我已经设置了一个基本的 Web Api,演示了使用 ASP.Net Core 使用 GraphQL。我遵循了一个教程,感觉到底是什么,但遇到了一个我不明白的错误。

我正在为 .NET v2.4.0 使用 GraphQL

这是错误:

System.InvalidOperationException: No service for type 'Land.GraphQL.Queries.LandQuery' has been registered.
  at at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
  at at GraphQL.FuncDependencyResolver.Resolve(Type type)
  at at GraphQL.FuncDependencyResolver.Resolve[T]()
  at Land.GraphQL.LandSchema..ctor(IDependencyResolver resolver) in .../LandSchema.cs:11
Run Code Online (Sandbox Code Playgroud)

我将不胜感激任何灯光:)

这是代码:

我创建了一个 LandType:ObjectGraphType 来定义类型:

    public class LandType : ObjectGraphType<Entities.Land>
    {
        public LandType(ILandDataAccess landDataAccess)
        {
            Name = "Land";

            Field(land => land.Id, type: typeof(IdGraphType)).Description("Land Id in LandApi context");
            Field(land => land.Apn)
                .Description(
                    "Assessor's Parcel Number (APN) is a unique number that is assigned to each tract of land in a county by the Tax Assessor.");
            Field(land => land.Address).Description("");
            Field(land => land.ZipCode).Description("");
            Field(land => land.City).Description("");
            Field(land => land.County).Description("");
            Field(land => land.State).Description("");
            Field(land => land.Country).Description("");
            Field(land => land.GisNumber).Description("");
            Field(land => land.AssessedValue).Description("");
            Field(land => land.LegalDescription).Description("");
            Field(land => land.Acreage, type: typeof(FloatGraphType)).Description("Acreage of Land");

        }
    }
Run Code Online (Sandbox Code Playgroud)

我创建了一个 LandQuery:ObjectGraphType 来定义查询:

public class LandQuery : ObjectGraphType
    {
        public LandQuery(ILandDataAccess dataAccess)
        {
            Field<ListGraphType<LandType>>(
                "Land", 
                resolve: context => dataAccess.GetLandsAsync());
        }
    }
Run Code Online (Sandbox Code Playgroud)

我创建了一个 LandSchema:Schema 来定义架构:

public class LandSchema : Schema
    {
        public LandSchema(IDependencyResolver resolver) : base(resolver)
        {
            Query = resolver.Resolve<LandQuery>();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我将服务和中间件添加到启动文件中:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(
                s.GetRequiredService));

            services.AddScoped<LandSchema>();

            services.AddGraphQL(o => { o.ExposeExceptions = true; })
                .AddGraphTypes(ServiceLifetime.Scoped);
        }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
                app.UseGraphQL<LandSchema>();
        }
Run Code Online (Sandbox Code Playgroud)

编辑:

感谢评论者@NateBarbettini 和@TonyNgo 激励我找到答案。结果.AddGraphTypes只搜索调用程序集。我的 GraphType 存储在引用的程序集中。传递引用的程序集修复了问题:.AddGraphTypes(typeof(LandSchema).Assembly, ServiceLifetime.Scoped);

Ton*_*Ngo 1

我认为您在配置服务中缺少这行代码。

public void ConfigureServices (IServiceCollection services) {
    services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver (
        s.GetRequiredService));

    services.AddScoped<LandSchema>();
    services.AddGraphQL(x => {
            x.ExposeExceptions = true; //set true only in development mode. make it switchable.
        })
        .AddGraphTypes (ServiceLifetime.Scoped);
}
Run Code Online (Sandbox Code Playgroud)

如果不是这种情况,您可以在这里阅读我的博客