小编Dan*_*nok的帖子

找不到方法:'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider'

在我的 .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)

build-error methodnotfound missingmethod .net-core

5
推荐指数
3
解决办法
8768
查看次数

初始化 DefaultHttpContext.Response.Body 到 MemoryStream 抛出 NullReferencePointer

我正在研究全局异常中间件的实现,我想用单元测试来覆盖中间件。下面你看看我走了多远。

这是单元测试的代码。

    [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

2
推荐指数
1
解决办法
1700
查看次数