gpt4 book ai didi

c# - 无法从程序集 'GraphQL.Http.IDocumentWriter' 加载类型 'GraphQL, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null'

转载 作者:行者123 更新时间:2023-12-04 04:00:43 28 4
gpt4 key购买 nike

到达 services.AddGraphQL 时发生错误.
我尝试降级 GraphQL to 2.4 的版本但随后 FuncServiceProvider停止工作。
这是我所拥有的:MarketScheme.cs

public class MarketScheme : Schema
{
public MarketScheme(IServiceProvider provider) : base(provider)
{
Query = provider.GetRequiredService<AppQuery>();
}
}

Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<marketContext>();

services.AddScoped<IMarketRepository, MarketRepository>();

services.AddScoped<IProductRepository, ProductRepository>();

services.AddScoped<IServiceProvider>(provider => new FuncServiceProvider(provider.GetRequiredService));

services.AddScoped<MarketScheme>();

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

services.AddControllers();
}

最佳答案

这有两个部分。

  • 界面GraphQL.Http.IDocumentWriter移至 GraphQL.IDocumentWriter .
  • JSON 序列化被提取到两个不同的库中,GraphQL.NewtonsoftJsonGraphQL.SystemTextJson .

  • 如果您使用的是 server项目,您将需要 update to 4.x.
    See the 2.x to 3.0 upgrade guide如果您编写了自己的中间件。
    Newtonsoft.Json 示例
    using Newtonsoft.Json;

    private static async Task ExecuteAsync(HttpContext context, ISchema schema)
    {
    GraphQLRequest request;
    using (var reader = new StreamReader(context.Request.Body))
    using (var jsonReader = new JsonTextReader(reader))
    {
    var ser = new JsonSerializer();
    request = ser.Deserialize<GraphQLRequest>(jsonReader);
    }

    var executer = new DocumentExecuter();
    var result = await executer.ExecuteAsync(options =>
    {
    options.Schema = schema;
    options.Query = request.Query;
    options.OperationName = request.OperationName;
    options.Inputs = request.Variables.ToInputs();
    });

    context.Response.ContentType = "application/json";
    context.Response.StatusCode = result.Errors?.Any() == true ? (int)HttpStatusCode.BadRequest : (int)HttpStatusCode.OK;

    var writer = new GraphQL.NewtonsoftJson.DocumentWriter();
    await writer.WriteAsync(context.Response.Body, result);
    }

    public class GraphQLRequest
    {
    public string OperationName { get; set; }
    public string Query { get; set; }
    public Newtonsoft.Json.Linq.JObject Variables { get; set; }
    }
    System.Text 示例
    using System.Text.Json;

    private static async Task ExecuteAsync(HttpContext context, ISchema schema)
    {
    var request = await JsonSerializer.DeserializeAsync<GraphQLRequest>
    (
    context.Request.Body,
    new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
    );

    var executer = new DocumentExecuter();
    var result = await executer.ExecuteAsync(options =>
    {
    options.Schema = schema;
    options.Query = request.Query;
    options.OperationName = request.OperationName;
    options.Inputs = request.Variables.ToInputs();
    });

    context.Response.ContentType = "application/json";
    context.Response.StatusCode = 200; // OK

    var writer = new GraphQL.SystemTextJson.DocumentWriter();
    await writer.WriteAsync(context.Response.Body, result);
    }

    public class GraphQLRequest
    {
    public string OperationName { get; set; }

    public string Query { get; set; }

    [JsonConverter(typeof(GraphQL.SystemTextJson.ObjectDictionaryConverter))]
    public Dictionary<string, object> Variables { get; set; }
    }

    关于c# - 无法从程序集 'GraphQL.Http.IDocumentWriter' 加载类型 'GraphQL, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63079557/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com