今天我使用实体框架和 postgres 创建了许多函数,并在本地主机上测试了它们。所以我部署到Azure,当我在Azure门户上测试它时,一切都返回404。我尝试了很多方法,但没有解决它。请有人帮我解决这个问题。
我的功能如下:
public class CityFunction
{
private readonly CityService _cityService;
public CityFunction()
{
var dbContext = new AppDbContext();
_cityService = new CityService(dbContext);
}
private JsonSerializerOptions GetJsonSerializerOptions()
{
return new JsonSerializerOptions
{
WriteIndented = true,
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles,
};
}
[FunctionName("FindCityById")]
public async Task<IActionResult> FindCityById(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "cities/{idCity:int}")]
HttpRequest req,
int idCity,
ILogger log
)
{
var city = await _cityService.FindCityById(idCity);
var jsonSerializerOptions = GetJsonSerializerOptions();
var jsonResponse = JsonSerializer.Serialize(city, jsonSerializerOptions);
return city != null
? new ContentResult
{
Content = jsonResponse,
ContentType = "application/json",
StatusCode = 200,
}
: new NotFoundResult();
}
给定的代码适用于
In-Process model
In-Process
版本。下面给出的代码对我有用:
我刚刚用过
public Function1(CityService cityService)
{
_cityService = cityService;
}
代替
public CityFunction()
{
var dbContext = new AppDbContext();
_cityService = new CityService(dbContext);
}
Function1.cs
:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace dotnet8_inprocess
{
public class Function1
{
private readonly CityService _cityService;
public Function1(CityService cityService)
{
_cityService = cityService;
}
private JsonSerializerOptions GetJsonSerializerOptions()
{
return new JsonSerializerOptions
{
WriteIndented = true,
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles,
};
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "cities/{idCity:int}")] HttpRequest req, int idCity,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var city = await _cityService.FindCityById(idCity);
var jsonSerializerOptions = GetJsonSerializerOptions();
var jsonResponse = JsonSerializer.Serialize(city, jsonSerializerOptions);
return city != null
? new ContentResult
{
Content = jsonResponse,
ContentType = "application/json",
StatusCode = 200,
}
: new NotFoundResult();
}
}
}
dotnet8_inprocess
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<RootNamespace>dotnet8_inprocess</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
My Data
:
postgres=> select * From city;
id | name | population | countrycode | createdat
----+-------------+------------+-------------+----------------------------
1 | Los Angeles | 3980400 | USA | 2024-09-17 04:35:54.869276
2 | Tokyo | 37435191 | JPN | 2024-09-17 04:35:54.869276
3 | London | 8982000 | GBR | 2024-09-17 04:35:54.869276
4 | New Delhi | 1413644826 | IND | 2024-09-17 04:35:54.869276
(4 rows)
OUTPUT
:本地:
天蓝色: