我有以下Azure功能:

[Function("GetList")]
        public async Task<IActionResult> GetList(
           [HttpTrigger(AuthorizationLevel.Function, "GET", Route = "list")] HttpRequest req)
        {
            try
            {
                var result = await GetMyList();
                return result != null
                    ? new OkObjectResult(result)
                    : new NotFoundObjectResult("No results");
            }
}

结果的类型为IList,值类型为MyObject.如果我调试该函数,我可以看到列表中充满了有值的对象.然而,在Chrome中,我得到了一个空值列表,如下所示:

[
    {},
    {},
    {}
]

MyObject如下所示:

public class MyObject
    {
        public string Prop1;
        public string Prop2;
    }

我还try 使用HttpResponseData作为响应,但结果相同.

这曾经在.NET6中起作用,但在升级到.NET8之后,它不返回值.这是否应该在.Net8中以不同的方式实现?

推荐答案

我同意,并感谢你@Peter B的建议.

你需要在你的MyObject班加{get; set;}分.

我已经try 了你的代码,我也得到了相同的输出.

enter image description here

fieldsfields、我为你工作

#My Code:. 一百零二

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>My.Namespace</RootNamespace>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.0.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />

  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext"/>
  </ItemGroup>
</Project>

100:

using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public class HttpTrigger1
    {
        private readonly ILogger _logger;

        public HttpTrigger1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<HttpTrigger1>();
        }

        [Function("HttpTrigger1")]
        public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route ="list")] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

                        try
            {
                var result = await GetMyList();
                return result != null
                    ? new OkObjectResult(result)
                    : new NotFoundObjectResult("No results");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error: {ex.Message}");
                return new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }
        }

        private static async Task<List<MyObject>> GetMyList()
        {
            return new List<MyObject>
        {
            new MyObject { Prop1 = "Vivek", Prop2 = "Shandilya" },
            new MyObject { Prop1 = "Vivek", Prop2 = "Vaibhav" },

        };
        }
    }

    public class MyObject
    {
        public string Prop1 {get; set;}
        public string Prop2 {get; set;}
    }
}

100:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services => {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();

host.Run();

100:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  }
}

OUTPUT: enter image description here

Chrome:

enter image description here

Edge:

enter image description here

Csharp相关问答推荐

System.Net.Http.HttpClient.SendAsync(request)在docker容器内的POST方法30秒后停止

WinForms在Linux上的JetBrains Rider中的应用

如何使用XmlSerializer序列化带有CDATA节的XML文件?

NET8 Maui&;iOS:AppCenter崩溃错误

Blazor Web App WASM的两个独立项目令人困惑

如果设置了另一个属性,则Newtonsoft JSON忽略属性

如果是,我怎么才能让这个加75,如果不是,我怎么才能减go 100?

我什么时候应该在Dapper中使用Connection.OpenAsync?

Content WithTargetPath实际上是有效的MSBuild项吗?

当try 测试具有协变返回类型的抽象属性时,类似功能引发System.ArgumentException

如何在.NET MAUI中最大化GraphicsView的大小?

如何让游戏对象在切换场景时被销毁,但在开始新游戏时重新锁定

在同一个捕获中可以有多种类型的异常吗?

毛伊岛.NET 8图片不再适合按钮

正在try 将自定义字体添加到我的控制台应用程序

Xamarin.Forms中具有类似AspectFill的图像zoom 的水平滚动视图

如何使ExecuteAsync异步运行

EF核心错误';$';附近的语法不正确,但EF核心生成了查询?

如何在C#中判断方法的返回类型是否可以为null(启用了完全可以为null的上下文)

有没有办法自动将双参数转换为 Vector?