You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure函数中C# GraphQL客户端调用API返回NotFound问题排查

Azure Function中调用GraphQL API返回NotFound错误排查

我首次在此提问,查了多篇文章没找到可行方案。我在Azure Function App里写了每分钟触发的定时函数,想通过GraphQL API获取数据,但一直收到报错:"The HTTP request failed with status code NotFound"。想问问哪里操作错了?是查询语句的问题吗?


Postman成功响应结果:

{
    "data": {
        "solarPowerUtilization": [
            {
                "timestamp": "2022-07-22T10:37:16.8036596Z",
                "sensorCount": 2,
                "result": 0.1215339233038348,
                "measurementUnit": "%",
                "latestObservationTime": "2022-07-22T10:36:01Z",
                "earliestObservationTime": "2022-07-22T10:36:01Z",
                "realEstate": {
                    "id": "SomeData",
                    "class": "RealEstate",
                    "aliasId": "4057",
                    "popularName": "SomeData",
                    "address": "SomeData",
                    "solarPowerCapacity": 33900
                }
            }
        ]
    }
}

函数代码:

[FunctionName("AmfFlowIngestion")]
public async Task GetData([TimerTrigger("0 */1 * * * *",
    #if DEBUG
        RunOnStartup = true
    #endif
    )] TimerInfo myTimer, ILogger log)
{

    try
    {
        GraphQLHttpClientOptions graphQLHttpClientOptions = new()
        {
            EndPoint = new Uri(amfApiUrl, UriKind.Absolute),
        };

        GraphQLHttpClient graphQLClient = new(graphQLHttpClientOptions, new GraphQL.Client.Serializer.Newtonsoft.NewtonsoftJsonSerializer());
        graphQLClient.HttpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", amfApiKey);

        GraphQLHttpRequest request = new()
        {
            Query = @"
                query {
                    solarPowerUtilization(realEstateId: ""4057"") {
                        timestamp
                        sensorCount
                        result
                        measurementUnit
                        latestObservationTime
                        earliestObservationTime
                        realEstate {
                            id
                            class
                            aliasId
                            popularName
                            address
                            solarPowerCapacity
                        }
                    }
                }"
        };

        var response = await graphQLClient.SendQueryAsync<Data>(request);

        log.LogInformation(response.ToString());
    }
    catch (Exception ex)
    {
        log.LogError(ex.ToString());
        throw;
    }
}

数据模型:

public partial class Data
{
    [JsonProperty("solarPowerUtilization")]
    public SolarPowerUtilization[] SolarPowerUtilization { get; set; }

    [JsonProperty("solarEnergyProduction")]
    public SolarEnergyProduction[] SolarEnergyProduction { get; set; }
}

public partial class SolarPowerUtilization
{
    [JsonProperty("timestamp")]
    public DateTimeOffset Timestamp { get; set; }

    [JsonProperty("sensorCount")]
    public long SensorCount { get; set; }

    [JsonProperty("result")]
    public double Result { get; set; }

    [JsonProperty("measurementUnit")]
    public string MeasurementUnit { get; set; }

    [JsonProperty("latestObservationTime")]
    public DateTimeOffset LatestObservationTime { get; set; }

    [JsonProperty("earliestObservationTime")]
    public DateTimeOffset EarliestObservationTime { get; set; }

    [JsonProperty("realEstate")]
    public RealEstate RealEstate { get; set; }
}

public partial class SolarEnergyProduction
{
    [JsonProperty("timestamp")]
    public DateTimeOffset Timestamp { get; set; }

    [JsonProperty("result")]
    public long Result { get; set; }

    [JsonProperty("fromDate")]
    public DateTimeOffset FromDate { get; set; }

    [JsonProperty("toDate")]
    public DateTimeOffset ToDate { get; set; }

    [JsonProperty("measurementUnit")]
    public string MeasurementUnit { get; set; }

    [JsonProperty("sensorCount")]
    public long SensorCount { get; set; }

    [JsonProperty("latestObservationTime")]
    public DateTimeOffset LatestObservationTime { get; set; }

    [JsonProperty("quantityKind")]
    public string QuantityKind { get; set; }

    [JsonProperty("placementContext")]
    public string PlacementContext { get; set; }

    [JsonProperty("realEstate")]
    public RealEstate RealEstate { get; set; }
}

public partial class RealEstate
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("class")]
    public string Class { get; set; }

    [JsonProperty("aliasId")]
    public string AliasId { get; set; }

    [JsonProperty("popularName")]
    public string PopularName { get; set; }

    [JsonProperty("address")]
    public string Address { get; set; }

    [JsonProperty("solarPowerCapacity")]
    public long SolarPowerCapacity { get; set; }
}

排查方向
  • 端点URL验证:确认amfApiUrl的值和Postman中使用的完全一致,包括协议(http/https)、域名、路径,没有拼写错误或多余的后缀/前缀。
  • 订阅密钥传递方式:有些API要求订阅密钥放在查询参数而非请求头,检查API文档确认Ocp-Apim-Subscription-Key的传递方式是否正确。
  • 请求细节对比:在客户端添加调试日志,查看实际发送的请求URL、头信息和查询内容,和Postman的请求做逐字段对比。
  • 环境配置检查:Azure Function中amfApiUrl和amfApiKey是否正确配置在应用设置中,避免本地调试和生产环境的配置差异。
  • 查询语句格式:尝试将代码中的查询字符串压缩为单行,排除换行或转义引号可能导致的语法问题。

内容的提问来源于stack exchange,提问作者dadamatth3w

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 16:27:35