.NET 5 项目中 System.Web.Http 的 HttpConfiguration [英] HttpConfiguration from System.Web.Http in .NET 5 project
问题描述
更新:
完整代码示例:
公共类 DelegatingHandlerStub : DelegatingHandler {private readonly Func>_handlerFunc;公共 DelegatingHandlerStub() {_handlerFunc = (请求,取消令牌) =>Task.FromResult(request.CreateResponse(HttpStatusCode.OK));}public DelegatingHandlerStub(Func> handlerFunc) {_handlerFunc = handlerFunc;}受保护的覆盖任务SendAsync(HttpRequestMessage 请求,CancellationToken 取消令牌){返回_handlerFunc(请求,取消令牌);}}公共异步任务 Should_Return_Ok() {//安排var expected = "Hello World";var mockFactory = new Mock();var 配置 = 新的 HttpConfiguration();var clientHandlerStub = new DelegatingHandlerStub((请求,取消令牌)=> {request.SetConfiguration(配置);var response = request.CreateResponse(HttpStatusCode.OK, 预期);返回 Task.FromResult(response);});var client = new HttpClient(clientHandlerStub);mockFactory.Setup(_ => _.CreateClient(It.IsAny())).Returns(client);IHttpClientFactory factory = mockFactory.Object;var controller = new ValuesController(工厂);//行为var result = await controller.Get();//断言result.Should().NotBeNull();var okResult = 结果为 OkObjectResult;var actual = (string) okResult.Value;实际.应该().是(预期);}
原文:
我正在按照本指南模拟 IHttpClientFactory
.
Visual Studios 修复是安装包'Microsoft.AspNet.WebApi.Core'
.
这有效并且代码运行良好,但我收到以下警告:
<块引用>警告 NU1701 包Microsoft.AspNet.WebApi.Core 5.2.7"是使用 '.NETFramework,Version=v4.6.1 恢复,.NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7,.NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2,.NETFramework,Version=v4.8' 而不是项目目标框架'net5.0'.此软件包可能与您的项目不完全兼容.
我已尝试安装下面推荐的 Microsoft.AspNetCore.Mvc.WebApiCompatShim
,但它不起作用.
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.WebApiCompatShim
https://stackoverflow.com/a/57279121/3850405
是否有另一个 NuGet 可以用来解决这个问题?
查看 Microsoft.AspNet.WebApi.Core
的依赖项,它仅依赖于 Microsoft.AspNet.WebApi.Client
而使用 .NETStandard 2.0代码>.理想情况下,我不想创建一个针对
.NET Standard 2.0
的新项目并将代码放在那里.我想使用 .NET 5
项目.
https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Core/
https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/
https://docs.microsoft.com/en-us/dotnet/standard/net-standard
我没有在 .NET 5
中获得 HttpConfiguration
所以我通过消除对 HttpConfiguration
的需求解决了它code>HttpConfiguration 像这样:
private LoginController GetLoginController(){var expected = "Hello world";var mockFactory = new Mock();var mockMessageHandler = new Mock();mockMessageHandler.Protected().Setup(SendAsync", ItExpr.IsAny(), ItExpr.IsAny()).ReturnsAsync(新的HttpResponseMessage{StatusCode = HttpStatusCode.OK,内容 = 新字符串内容(预期)});var httpClient = new HttpClient(mockMessageHandler.Object);mockFactory.Setup(_ => _.CreateClient(It.IsAny())).Returns(httpClient);var logger = Mock.Of>();var controller = new LoginController(logger, mockFactory.Object);返回控制器;}
Update:
Complete code example:
public class DelegatingHandlerStub : DelegatingHandler {
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handlerFunc;
public DelegatingHandlerStub() {
_handlerFunc = (request, cancellationToken) => Task.FromResult(request.CreateResponse(HttpStatusCode.OK));
}
public DelegatingHandlerStub(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handlerFunc) {
_handlerFunc = handlerFunc;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
return _handlerFunc(request, cancellationToken);
}
}
public async Task Should_Return_Ok() {
//Arrange
var expected = "Hello World";
var mockFactory = new Mock<IHttpClientFactory>();
var configuration = new HttpConfiguration();
var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
request.SetConfiguration(configuration);
var response = request.CreateResponse(HttpStatusCode.OK, expected);
return Task.FromResult(response);
});
var client = new HttpClient(clientHandlerStub);
mockFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(client);
IHttpClientFactory factory = mockFactory.Object;
var controller = new ValuesController(factory);
//Act
var result = await controller.Get();
//Assert
result.Should().NotBeNull();
var okResult = result as OkObjectResult;
var actual = (string) okResult.Value;
actual.Should().Be(expected);
}
Original:
I'm following this guide to mock IHttpClientFactory
.
https://stackoverflow.com/a/54227679/3850405
For it to work I need the following line:
var configuration = new HttpConfiguration();
Visual Studios fix is Install package 'Microsoft.AspNet.WebApi.Core'
.
This works and the code runs fine but I get the following warning:
Warning NU1701 Package 'Microsoft.AspNet.WebApi.Core 5.2.7' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project.
I have tried to install Microsoft.AspNetCore.Mvc.WebApiCompatShim
that was recommended below but it does not work.
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.WebApiCompatShim
https://stackoverflow.com/a/57279121/3850405
Is there another NuGet that can be used to solve this?
Looking at dependencies for Microsoft.AspNet.WebApi.Core
it is only dependent on Microsoft.AspNet.WebApi.Client
that in turn uses .NETStandard 2.0
. Ideally I would not like to create a new project targeting .NET Standard 2.0
and put the code in there. I would like to use the .NET 5
project.
https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Core/
https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/
https://docs.microsoft.com/en-us/dotnet/standard/net-standard
I did not get HttpConfiguration
in .NET 5
so I solved it by removing the need for HttpConfiguration
like this:
private LoginController GetLoginController()
{
var expected = "Hello world";
var mockFactory = new Mock<IHttpClientFactory>();
var mockMessageHandler = new Mock<HttpMessageHandler>();
mockMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(expected)
});
var httpClient = new HttpClient(mockMessageHandler.Object);
mockFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
var logger = Mock.Of<ILogger<LoginController>>();
var controller = new LoginController(logger, mockFactory.Object);
return controller;
}
这篇关于.NET 5 项目中 System.Web.Http 的 HttpConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!