英文:
MassTransit request/response - The host was not found for the specified address
问题
I understand you only want the code-related parts to be translated. Here's the relevant code-related part:
I'm trying to implement an integration test scenario where Component A inside an IIS host requests some data from Component B (external component) over the RabbitMQ/Masstransit bus using the IRequestClient. The Component B has its own bus instance running. My goal is to "mock" the target consumer on Component B by registering a replacement consumer inside the test and have that respond to the same request.
The production code works fine. But when it comes to this test, the replacement consumer is invoked as it should and I can debug it, however as soon as `await context.RespondAsync(..);` is called, I get the following exception:
MassTransit.Messages Error: 0 : R-FAULT rabbitmq://localhost:0/myVHost/Command.Bus.Messages.Data.IGetDataRequest_0.0.0.0 7a000000-9a3c-0005-b155-08db518a24c8 Bus.Messages.Data.IGetDataRequest MassTransit.MessageHandler<Bus.Messages.Data.IGetDataRequest>(00:00:03.2257322) The host was not found for the specified address: rabbitmq://localhost:0/myVHost/bus-MYLOCALHOST-testhost-xeyyyyr48oyym8zfbdpidntbrh?durable=false&autodelete=true, MassTransit.EndpointNotFoundException: The host was not found for the specified address: rabbitmq://localhost:0/myVHost/bus-MYLOCALHOST-testhost-xeyyyyr48oyym8zfbdpidntbrh?durable=false&autodelete=true
at MassTransit.RabbitMqTransport.Transport.SendTransportProvider.GetSendTransport(Uri address)
at MassTransit.RabbitMqTransport.Transport.SendTransportProvider.MassTransit.ISendTransportProvider.GetSendTransport(Uri address)
at MassTransit.Transports.SendEndpointProvider.CreateSendEndpoint(Uri address)
at MassTransit.Transports.SendEndpointCache`1.GetSendEndpointFromFactory(TKey address, SendEndpointFactory`1 factory)
at GreenPipes.Caching.Internals.PendingValue`2.CreateValue()
at MassTransit.Transports.SendEndpointCache`1.GetSendEndpoint(TKey key, SendEndpointFactory`1 factory)
at MassTransit.Context.BaseConsumeContext.GetSendEndpoint(Uri address)
at MassTransit.Context.BaseConsumeContext.RespondAsync[T](T message, IPipe`1 sendPipe)
at MyTest.Helpers.Consumers.TestDataRequestConsumer.Consume(ConsumeContext`1 context) in C:\Projects\MyTest\Integration.MyTest\Helpers\Consumers\TestDataRequestConsumer.cs:line 56
at RBus.MessageHandler.<Setup>b__3_0[T](ConsumeContext`1 c)
at MassTransit.Pipeline.Filters.HandlerMessageFilter`1.GreenPipes.IFilter<MassTransit.ConsumeContext<TMessage>>.Send(ConsumeContext`1 context, IPipe`1 next)
Here's the test setup:
[OneTimeSetUp]
public async Task SetUp()
{
var webApp = new WebApplicationFactory<Startup.IisHost.Startup>()
.WithWebHostBuilder(builder =>
{
builder.UseTestServer();
builder.ConfigureTestServices(services =>
{
services.Configure<DatabaseOptions>(o => o.ConnectionString = testEnvironment.DbConnectionString);
services.Configure<CustomerClientOptions>(configuration.GetSection("Customer"));
services.AddSingleton(subscriptionHelper);
});
});
httpClient = webApp.CreateClient();
var serviceCollection = new ServiceCollection();
serviceCollection.Configure<CustomerClientOptions>(configuration.GetSection("Customer"));
serviceCollection.AddCustomerClient();
serviceCollection.AddTransient<IIdentityProvider, IdentityProvider>();
serviceCollection.AddBus(configuration.GetSection("BusConfig"), x =>
{
x.WithIdentityProvider((provider, id) => provider.GetRequiredService<IIdentityProvider>().ProvideIdentityByCustomerId(id));
x.WithRequestQueue<IGetDataRequest, TestDataRequestConsumer>();
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var busControl = serviceProvider.GetService<IBusControl>();
await busControl.StartAsync().ConfigureAwait(false);
// Further code is irrelevant and removed for brevity
}
I hope this helps! If you have any specific questions or need further assistance with this code, please feel free to ask.
英文:
I'm trying to implement an integration test scenario where Component A inside an IIS host requests some data from Component B (external component) over the RabbitMQ/Masstransit bus using the IRequestClient. The Component B has its own bus instance running. My goal is to "mock" the target consumer on Component B by registering a replacement consumer inside the test and have that respond to the same request.
The production code works fine. But when it comes to this test, the replacement consumer is invoked as it should and I can debug it, however as soon as await context.RespondAsync(..);
is called, I get the following exception:
MassTransit.Messages Error: 0 : R-FAULT rabbitmq://localhost:0/myVHost/Command.Bus.Messages.Data.IGetDataRequest_0.0.0.0 7a000000-9a3c-0005-b155-08db518a24c8 Bus.Messages.Data.IGetDataRequest MassTransit.MessageHandler<Bus.Messages.Data.IGetDataRequest>(00:00:03.2257322) The host was not found for the specified address: rabbitmq://localhost:0/myVHost/bus-MYLOCALHOST-testhost-xeyyyyr48oyym8zfbdpidntbrh?durable=false&autodelete=true, MassTransit.EndpointNotFoundException: The host was not found for the specified address: rabbitmq://localhost:0/myVHost/bus-MYLOCALHOST-testhost-xeyyyyr48oyym8zfbdpidntbrh?durable=false&autodelete=true
at MassTransit.RabbitMqTransport.Transport.SendTransportProvider.GetSendTransport(Uri address)
at MassTransit.RabbitMqTransport.Transport.SendTransportProvider.MassTransit.ISendTransportProvider.GetSendTransport(Uri address)
at MassTransit.Transports.SendEndpointProvider.CreateSendEndpoint(Uri address)
at MassTransit.Transports.SendEndpointCache`1.GetSendEndpointFromFactory(TKey address, SendEndpointFactory`1 factory)
at GreenPipes.Caching.Internals.PendingValue`2.CreateValue()
at MassTransit.Transports.SendEndpointCache`1.GetSendEndpoint(TKey key, SendEndpointFactory`1 factory)
at MassTransit.Context.BaseConsumeContext.GetSendEndpoint(Uri address)
at MassTransit.Context.BaseConsumeContext.RespondAsync[T](T message, IPipe`1 sendPipe)
at MyTest.Helpers.Consumers.TestDataRequestConsumer.Consume(ConsumeContext`1 context) in C:\Projects\MyTest\Integration.MyTest\Helpers\Consumers\TestDataRequestConsumer.cs:line 56
at RBus.MessageHandler.<Setup>b__3_0[T](ConsumeContext`1 c)
at MassTransit.Pipeline.Filters.HandlerMessageFilter`1.GreenPipes.IFilter<MassTransit.ConsumeContext<TMessage>>.Send(ConsumeContext`1 context, IPipe`1 next)
Here's the test setup:
[OneTimeSetUp]
public async Task SetUp()
{
var webApp = new WebApplicationFactory<Startup.IisHost.Startup>()
.WithWebHostBuilder(builder =>
{
builder.UseTestServer();
builder.ConfigureTestServices(services =>
{
services.Configure<DatabaseOptions>(o => o.ConnectionString = testEnvironment.DbConnectionString);
services.Configure<CustomerClientOptions>(configuration.GetSection("Customer"));
services.AddSingleton(subscriptionHelper);
});
});
httpClient = webApp.CreateClient();
var serviceCollection = new ServiceCollection();
serviceCollection.Configure<CustomerClientOptions>(configuration.GetSection("Customer"));
serviceCollection.AddCustomerClient();
serviceCollection.AddTransient<IIdentityProvider, IdentityProvider>();
serviceCollection.AddBus(configuration.GetSection("BusConfig"), x =>
{
x.WithIdentityProvider((provider, id) => provider.GetRequiredService<IIdentityProvider>().ProvideIdentityByCustomerId(id));
x.WithRequestQueue<IGetDataRequest, TestDataRequestConsumer>();
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var busControl = serviceProvider.GetService<IBusControl>();
await busControl.StartAsync().ConfigureAwait(false);
// Further code is irrelevant and removed for brevity
}
In the Startup.IisHost.Startup
ConfigureServices method I register the client which has the IRequestClient injected in the constructor, then add the bus.
services.AddTransient<IDataClient, DataClient>();
services.AddBus(Configuration.GetSection("Bus"), x =>
{
x.WithIdentityProvider((provider, customerId) => provider.GetRequiredService<IIdentityProvider>().ProvideIdentityByCustomerId(customerId));
});
services.AddHostedService<BusService>();
The "actual" bus is started from within IHostedService:
public BusService(ILogger<BusService> logger, IBusControl busControl)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.busControl = busControl ?? throw new ArgumentNullException(nameof(busControl));
}
public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
await busControl.StartAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
logger.LogCritical(e, "Error while starting IIS Bus service");
throw;
}
}
Here's the request client:
internal class DataClient : IDataClient
{
private readonly IRequestClient requestClient;
public DataClient (IRequestClient requestClient)
{
this.requestClient = requestClient ?? throw new ArgumentNullException(nameof(requestClient));
}
/// <inheritdoc/>
public async Task<IDictionary<SomeData, OtherData>> GetData(IEnumerable<string> names, IEnumerable<int> ids)
{
if (names == null) throw new ArgumentNullException(nameof(names));
if (ids == null) throw new ArgumentNullException(nameof(ids));
var namesList = names.ToList();
var idsList = ids.ToList();
if (!idsList.Any())
{
return new Dictionary<SomeData, OtherData>();
}
var response = await requestClient.Request<IGetDataRequest, IGetDataResponse>(new
{
Names = namesList ,
Ids = idsList
}).ConfigureAwait(false);
var riiDeviceCredentialsResponse = response.Message;
...
}
}
And here's the consumer (identical to the one in Component B):
public class TestDataRequestConsumer: IConsumer<IGetDataRequest>
{
private readonly GetDataOptions options;
public TestDataRequestConsumer(IOptions<GetDataOptions> options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
this.options = options.Value;
}
public async Task Consume(ConsumeContext<IGetDataRequest> context)
{
// Do some stuff..
await context.RespondAsync(response).ConfigureAwait(false);
}
}
Both the test and Component B are using the same bus configuration:
"UseInMemory": false,
"HostScheme": "rabbitmq",
"HostName": "localhost",
"HostPort": 5672,
"Username": "myuser",
"Password": "mypassword",
"VHostName": "myVHost",
"ServerVersion": "0.0.0.0",
"NumberOfRetry": 3,
"SecBetweenRetry": 2,
"PrefetchCount": 4,
When I look into the RabbitMQ management console, I can see that the messages are first added to the correct queue, but then later moved to the error queue.
The proprietary bus libraries I'm using are utilizing MassTransit 5.1.5.1633.
I'm not really sure where the problem might be. Any help and/or pointers for further investigation will be greatly appreciated.
Let me know if there's more info that's needed.
答案1
得分: 0
感谢Chris的帮助,通过将测试项目降级至.NET Core 3.1(.NET 5也可以),这个问题已经解决。另外,最新版本的MassTransit也可以解决问题,但在我的情况下,这个选项相对昂贵,所以在适当的时候,较低版本的.NET就足够了。
英文:
Thanks to Chris, this was resolved by downgrading the test project to .NET Core 3.1 (.NET 5 works too). Alternatively, the latest version of MassTransit would do the job, but in my case this options is rather costly, so in due time a lower .NET version will suffice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论