我最近得到了一台 Mac M1,并且正在设置我的机器以进行 Azure Function(dotnet-isolated)开发。
我按照下面的文档进行操作,该文档在我的 Windows 10 计算机上运行得非常好。 https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-csharp?tabs=azure-cli%2Cisolated-process
以下是 Mac 中的设置,
% func --版本 4.0.4895
% dotnet --list-sdks 6.0.403 [/usr/local/share/dotnet/sdk]
% azure-functions-core-tools@4
% az --版本 azure-cli 2.42.0
核心2.42.0 遥测1.0.8
依赖关系: MSAL 1.20.0 天蓝色管理资源 21.1.0b1
完成上述安装后,使用以下命令创建一个新函数
% func new --template "Http Trigger" --name MyHttpTrigger
然后当我尝试使用启动该功能时 %功能开始
我收到以下错误 ......
MyHttpTrigger: [GET,POST] http://localhost:7071/api/MyHttpTrigger
For detailed output, run func with --verbose flag.
[2022-11-30T09:21:10.868Z] Unhandled exception. Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: An HTTP/2 connection could not be established because the server did not complete the HTTP/2 handshake. IOException: Unable to write data to the transport connection: Broken pipe. SocketException: Broken pipe", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request.
[2022-11-30T09:21:10.868Z] ---> System.IO.IOException: An HTTP/2 connection could not be established because the server did not complete the HTTP/2 handshake.
[2022-11-30T09:21:10.868Z] ---> System.IO.IOException: Unable to write data to the transport connection: Broken pipe.
[2022-11-30T09:21:10.868Z] ---> System.Net.Sockets.SocketException (32): Broken pipe
[2022-11-30T09:21:10.868Z] at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)
csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.8.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.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>
程序.cs
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();
host.Run();
功能
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace LocalFunctionProj
{
public class MyHttpTrigger
{
private readonly ILogger _logger;
public MyHttpTrigger(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MyHttpTrigger>();
}
[Function("MyHttpTrigger")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
}
}
本地.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
}
}
launchSettings.json
{
"profiles": {
"LocalFunctionProj": {
"commandName": "Project",
"commandLineArgs": "--port 7146",
"launchBrowser": false
}
}
}
请对我在这里缺少的内容有任何意见吗?
我知道这来得太晚了,但是,我最近也遇到了这个问题,结果是我的 Program.cs 中的调用顺序问题。简而言之,我在 ConfigureAppConfiguration 之前调用了 ConfigureFunctionsWorkerDefaults。在这件事上,我用头撞墙的时间比我愿意承认的要长得多。一旦我改变了调用顺序,一切都很好。例如:
var host = new HostBuilder()
.ConfigureAppConfiguration(builder => builder.AddEnvironmentVariables())
.ConfigureFunctionsWorkerDefaults()
.Build();
希望这对其他人有帮助。
我在使用 .NET 4.8 dotnet-isolated 的 Azure Function 时遇到了同样的问题。在查看了一些博客后,我验证了 Windows Defender 防火墙上的防火墙规则,并发现了一些 func.exe 的规则( 为 Azure Function 生成的可执行文件)阻止连接。 启用此规则后,我能够毫无问题地执行该功能
@Diny,这个问题你得到解决了吗?如果有请分享一下。我现在正需要