You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.Net中使用Fast Endpoints时集成测试环境报错排查

集成测试中FastEndpoints启动失败排查求助

业务项目里FastEndpoints组件运行完全正常,但在集成测试环境启动时直接失败,报错触发在Program.cs的builder.Services.AddFastEndpoints代码行,报错信息见附图。

我怀疑是程序集版本不一致导致的问题,但没法确定,希望有人帮忙排查原因。

以下是我的OrderApiApplicationFactory类代码:

public class OrderApiApplicationFactory<TEntryPoint> : WebApplicationFactory<Program>, IAsyncLifetime where TEntryPoint : Program
{
    private const string Database = "master";
    private const string Username = "sa";
    private const string Password = "yourStrong(!)Password";
    private string _connectionString;
    private const ushort MsSqlPort = 1433;
    private const ushort RabbitMqPort = 5672; 
    private readonly MsSqlContainer _mssqlContainer;
    private readonly RabbitMqContainer _rabbitMqContainer;

    public HttpClient HttpClient { get; private set; } = default!;

    private const string RabbitMqUsername = "guest";
    private const string RabbitMqPassword = "guest";

    public OrderApiApplicationFactory()
    {
        _mssqlContainer = new MsSqlBuilder()
            .WithImage("mcr.microsoft.com/mssql/server:2022-latest")
            // Remove static port binding and let Docker assign random ports
            .WithPortBinding(0, MsSqlPort)
            .WithEnvironment("ACCEPT_EULA", "Y") 
            .WithEnvironment("SA_PASSWORD", Password)
            .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(MsSqlPort))
            .Build();

        _rabbitMqContainer = new RabbitMqBuilder()
            .WithImage("rabbitmq:3-management-alpine") 
            .WithPortBinding(5672, RabbitMqPort) // Bind RabbitMQ's default port to the specified host port
            .WithEnvironment("RABBITMQ_DEFAULT_USER", RabbitMqUsername)
            .WithEnvironment("RABBITMQ_DEFAULT_PASS", RabbitMqPassword)
            .WithWaitStrategy(Wait.ForUnixContainer()
                .UntilPortIsAvailable(RabbitMqPort) // Wait until the specified host port is available
                .UntilPortIsAvailable(15672)) // Wait until the management port is available
            .Build();
    }

    public async Task InitializeAsync()
    {
        await _mssqlContainer.StartAsync();
        await _rabbitMqContainer.StartAsync();
        _rabbitMqContainer.GetMappedPublicPort(RabbitMqPort);
        GetDatabaseConnectionString();
        var x = _rabbitMqContainer.Hostname;

        HttpClient = CreateClient();
    }

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureAppConfiguration(configurationBuilder =>
        {
            var configuration = new ConfigurationBuilder().AddJsonFile("testcontainersappsettings.json")
                .AddInMemoryCollection(new Dictionary<string, string>
                {
                    ["ConnectionStrings:DefaultConnection"] =  _connectionString,
                    ["RabbitMQConfiguration:Config:HostName"] = _rabbitMqContainer.Hostname
                })
                .AddEnvironmentVariables()
                .Build();

            configurationBuilder.AddConfiguration(configuration);
        });

        builder.ConfigureServices(services =>
        {
            RemoveDbContextOptions<BookyWooksOrderDbContext>(services);
            //RemoveDbContextOptions<IntegrationEventLogDbContext>(services);

            AddDbContext<BookyWooksOrderDbContext>(services);
            //AddDbContext<IntegrationEventLogDbContext>(services);
        });
    }

    private void RemoveDbContextOptions<T>(IServiceCollection services) where T : DbContext
    {
        var descriptor = services.SingleOrDefault(
            d => d.ServiceType == typeof(DbContextOptions<T>));

        if (descriptor != null)
            services.Remove(descriptor);
    }

    private void AddDbContext<T>(IServiceCollection services) where T : DbContext
    {
        services.AddDbContext<T>(options =>
        {
            options.UseSqlServer(_connectionString,
                builder => builder.MigrationsAssembly(typeof(T).Assembly.FullName));
        });
    }
    
    private string GetDatabaseConnectionString()
    {
        _connectionString = _mssqlContainer.GetConnectionString(); // $"Server={_mssqlContainer.Hostname},{_mssqlContainer.GetMappedPublicPort(MsSqlPort)};Database={Database};User Id={Username};Password={Password};TrustServerCertificate=True";
        return _connectionString;
    }

    public new async Task DisposeAsync()
    {
        await _mssqlContainer.DisposeAsync();
        await _rabbitMqContainer.DisposeAsync();
    }
}

内容的提问来源于stack exchange,提问作者Ports

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 22:58:29