在我的 .net Core 应用程序的 Main 方法中,我收到此错误,我不知道应该在哪里寻找解决方案。
这是我的主要方法:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
Run Code Online (Sandbox Code Playgroud)
这是我在按 F5 启动项目时收到的错误消息:
System.MissingMethodException: 'Method not found: 'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection)'.'
Run Code Online (Sandbox Code Playgroud) 我正在研究全局异常中间件的实现,我想用单元测试来覆盖中间件。下面你看看我走了多远。
这是单元测试的代码。
[Fact]
public async Task MethodName_StateUnderTest_ExpectedBehavior()
{
//Arrange
IExceptionHandlerFeature exceptionHandlerFeature = new ExceptionHandlerFeature {Error = new NotFoundException()};
IFeatureCollection features = new FeatureCollection();
features.Set(exceptionHandlerFeature);
var context = new DefaultHttpContext(features);
context.Response.Body = new MemoryStream();
//Act
await ExceptionMiddleware.HandleException(context);
//Assert
context.Response.StatusCode.Should().Be((int) HttpStatusCode.NotFound);
}
Run Code Online (Sandbox Code Playgroud)
这是 ExceptionMiddleware.Handle 方法的代码
public static async Task HandleException(HttpContext context)
{
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature == null)
{
return;
}
if (contextFeature.Error is AppValidationException validationException)
{
context.Response.StatusCode = (int) HttpStatusCode.BadRequest;
var failures = JsonConvert.SerializeObject(validationException.Failures);
await context.Response.WriteAsync(
new ErrorDetails
{ …Run Code Online (Sandbox Code Playgroud) unit-testing middleware httpcontext exceptionhandler .net-core