【发布时间】:2019-08-23 03:17:32
【问题描述】:
我有一个要从 Azure Functions v1 移植到 v2 的函数,作为其中的一部分,我在更新创建存根 HttpRequestMessage 的单元测试时遇到了问题。这是适用于 Functions v1 的代码,针对 .NET Framework 4.7
public class FunctionExample
{
[FunctionName(nameof(SomeFunction))]
public static async Task<HttpResponseMessage> SomeFunction
(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "SomeFunction")]
HttpRequestMessage request
)
{
var body = await request.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(body))
{
return request.CreateResponse(HttpStatusCode.BadRequest, "Bad job");
}
else
{
return request.CreateResponse(HttpStatusCode.OK, "Good job");
}
}
}
还有我的测试代码
public class FunctionExampleTests
{
[Test]
public async Task TestSomeFunction()
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("http://localhost/"),
Content = new StringContent("", Encoding.UTF8, "application/json")
};
request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey,
new HttpConfiguration());
var response = await FunctionExample.SomeFunction(request);
var content = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
Assert.That(content, Is.EqualTo("\"Bad job\""));
}
}
移植到v2后,我的函数项目csproj文件是这样的。唯一的区别是我不再以完整框架为目标,并且添加了AzureFunctionsVersion 节点。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
</ItemGroup>
</Project>
这是我重定向到 .NET Core 2.0 后测试项目的 csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FunctionExample\FunctionExample.csproj" />
</ItemGroup>
</Project>
以前,我使用this question 的答案正确地存根 HttpRequestMessage,但这似乎不再起作用。
当我尝试编译它时,我得到以下编译错误
Error CS0246 The type or namespace name 'HttpConfiguration' could not be found (are you missing a using directive or an assembly reference?)
Error CS0103 The name 'HttpPropertyKeys' does not exist in the current context
所以如果我只是删除该行,希望不再需要修复
request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
相反,我收到此错误消息
System.InvalidOperationException : HttpRequestMessage 实例未正确初始化。使用 HttpRequestMessageHttpContextExtensions.GetHttpRequestMessage 为当前请求创建一个 HttpRequestMessage。
尝试按照错误消息的指示对我来说没有结果,我尝试设置 HttpContext like was done in this question
request.Properties.Add(nameof(HttpContext), new DefaultHttpContext());
但这给了我一个不同的错误(与问题相同)
System.ArgumentNullException : 值不能为空。
参数名称:提供者
【问题讨论】:
标签: c# azure .net-core azure-functions asp.net-core-webapi