[英].NET6 Isolated Azure Function Unit and Integration Test causes gRPC exception
I have an isolated Azure Function that makes couple of HTTP POST calls.我有一个隔离的 Azure Function,它进行了几个 HTTP POST 调用。 I am trying to write an integration test for them.我正在尝试为他们编写集成测试。 But the test setup fails with a gRPC error.但是测试设置失败并出现 gRPC 错误。
Here is the Program.cs that configures the HttpClient with services.AddHttpClient();这是使用 services.AddHttpClient() 配置 HttpClient 的 Program.cs;
public class Program
{
public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddHttpClient();
}).Build();
host.Run();
}
}
The sample function looks like this:样本 function 如下所示:
public class AzFunctionEndpoint
{
public AzFunctionEndpoint(IHttpClientFactory httpClientFactory, IConfiguration configuration, ILoggerFactory loggerFactory)
{
logger = loggerFactory.CreateLogger<ResolveEndpoint>();
this.httpClientFactory = httpClientFactory;
this.configuration = configuration;
}
[Function("azfunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "azs/azfunction")] HttpRequestData req)
{
// Couple of other HTTP calls using httpClientFactory
// return
var res = req.CreateResponse(HttpStatusCode.OK);
return res;
}
private readonly IConfiguration configuration;
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger logger;
}
The Function runs correctly on local machine and when deployed to Azure. Function 在本地机器上正确运行,并在部署到 Azure 时正确运行。
Now I try and create an integration test with现在我尝试创建一个集成测试
public class AzFunctionEndpointIntegrationTests
{
public AzFunctionEndpointIntegrationTests()
{
factory = new WebApplicationFactory<Program>();
var clientFactory = factory.Services.GetService<IHttpClientFactory>();
// THIS LINE CAUSES gRPC host error
client = clientFactory.CreateClient();
}
[Fact]
public async Task AzFunction_Should_BeOK()
{
// POST to the azure function
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(HttpMethods.Post), "api/azs/azfunction");
var response = await client.SendAsync(request);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
}
private HttpClient client;
private WebApplicationFactory<Program> factory;
}
The test code that tries to create HttpClient to invoke my function causes this exception尝试创建 HttpClient 以调用我的 function 的测试代码导致此异常
client = clientFactory.CreateClient();
System.InvalidOperationException: The gRPC channel URI 'http://:51828' could not be parsed. System.InvalidOperationException:无法解析 gRPC 通道 URI“http://:51828”。
I don't quite understand what is this error.我不太明白这个错误是什么。 Any help on writing integration tests for Azure Functions is appreciated.感谢任何有关为 Azure 函数编写集成测试的帮助。
System.InvalidOperationException: The gRPC channel URI 'http://:51828' could not be parsed. System.InvalidOperationException:无法解析 gRPC 通道 URI“http://:51828”。
I don't quite understand what is this error.我不太明白这个错误是什么。 Any help on writing integration tests for Azure Functions is appreciated.感谢任何有关为 Azure 函数编写集成测试的帮助。
Below are the few workaround may help to fix the above issue.以下是一些可能有助于解决上述问题的解决方法。
GitHub issue
the root cause we have observed that, May be due to the usage of incorrect run/debug
configuration the IDE you are using.基于这个GitHub issue
,我们观察到的根本原因可能是由于您使用的 IDE 使用了不正确的run/debug
配置。 Please make sure that you are using the same as suggested on the given gitHub link
to run the application.请确保您使用的是给定gitHub link
中建议的相同方式来运行应用程序。
To write an Integration test you can refer this Example for your function app .要编写集成测试,您可以为您的 function 应用参考此示例。
Example of sample code:-示例代码示例:-
test.cs
[TestClass]
public class DefaultHttpTriggerTests
{
private HttpClient _http;
[TestInitialize]
public void Initialize()
{
this._http = new HttpClient();
}
[TestCleanup]
public void Cleanup()
{
this._http.Dispose();
}
[TestMethod]
public async Task Given_OpenApiUrl_When_Endpoint_Invoked_Then_It_Should_Return_Title()
{
// Arrange
var requestUri = "http://localhost:7071/api/openapi/v3.json";
// Act
var response = await this._http.GetStringAsync(requestUri).ConfigureAwait(false);
var doc = JsonConvert.DeserializeObject<OpenApiDocument>(response);
// Assert
doc.Should().NotBeNull();
doc.Info.Title.Should().Be("OpenAPI Document on Azure Functions");
doc.Components.Schemas.Should().ContainKey("greeting");
var schema = doc.Components.Schemas["greeting"];
schema.Type.Should().Be("object");
schema.Properties.Should().ContainKey("message");
var property = schema.Properties["message"];
property.Type.Should().Be("string");
}
}
For more information please refer the below links:-有关更多信息,请参阅以下链接:-
SO THREAD:- System.InvaliOperationException: The gRPC channel URI 'http://0' could not be parsed . SO THREAD:- System.InvaliOperationException: The gRPC channel URI 'http://0' could not be parsed 。
MICROSOFT DOCUMENTATION:- Guide for running C# Azure Functions in an isolated process MICROSOFT 文档:- 运行 C# Azure 隔离进程中函数的指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.