我正在使用NimbusAPI使用Azure Service Bus。我使用Nimbus从请求/响应操作中获取响应时遇到问题。以下是单元测试的输出。我可以成功获取请求,在断点中逐步执行,并观察它返回响应。客户端,无论是单元测试,MVC应用程序还是其他任何东西,总是超时。下面输出中的倒数第二行显示实际收到了响应,但最后一行表示它不是,并且已经超时。
12/17/2014 9:42:07 PM -05:00: Constructing bus... 12/17/2014 9:42:08
PM -05:00: Creating message pumps and subscriptions.
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.RequestResponse.ResponseMessagePumpFactory
(Nimbus.Infrastructure.RequestResponse.ResponseMessagePumpFactory)
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.MessageSendersAndReceivers.NimbusQueueMessageReceiver
(inputqueue.myapp.mypc) Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.MessagePump (Nimbus.Infrastructure.MessagePump)
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.NimbusMessagingFactory
(Nimbus.Infrastructure.NimbusMessagingFactory) Nimbus.GarbageMan:
Tracking Nimbus.Infrastructure.Commands.CommandMessagePumpsFactory
(Nimbus.Infrastructure.Commands.CommandMessagePumpsFactory) 12/17/2014
9:42:08 PM -05:00: Creating command message pumps Nimbus.GarbageMan:
Tracking
Nimbus.Infrastructure.RequestResponse.RequestMessagePumpsFactory
(Nimbus.Infrastructure.RequestResponse.RequestMessagePumpsFactory)
12/17/2014 9:42:08 PM -05:00: Creating request message pumps
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.RequestResponse.MulticastRequestMessagePumpsFactory
(Nimbus.Infrastructure.RequestResponse.MulticastRequestMessagePumpsFactory)
12/17/2014 9:42:08 PM -05:00: Creating multicast request message pumps
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.Events.MulticastEventMessagePumpsFactory
(Nimbus.Infrastructure.Events.MulticastEventMessagePumpsFactory)
12/17/2014 9:42:08 PM -05:00: Creating multicast event message pumps
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.Events.CompetingEventMessagePumpsFactory
(Nimbus.Infrastructure.Events.CompetingEventMessagePumpsFactory)
12/17/2014 9:42:08 PM -05:00: Creating competing event message pumps
12/17/2014 9:42:08 PM -05:00: Message pumps and subscriptions are all
created. 12/17/2014 9:42:08 PM -05:00: Bus built. Job done!
Nimbus.GarbageMan: Tracking
Nimbus.Infrastructure.MessageSendersAndReceivers.NimbusQueueMessageSender
(Nimbus.Infrastructure.MessageSendersAndReceivers.NimbusQueueMessageSender)
12/17/2014 9:42:08 PM -05:00: Sending request
Myapp.Messages.DoItRequest to
q.myapp.messages.doitrequest
[MessageId:bc1c79cc1965489c84703f5898bd6fd8,
CorrelationId:bc1c79cc1965489c84703f5898bd6fd8] 12/17/2014 9:42:08 PM
-05:00: Sent request Myapp.Messages.DoItRequest to q.myapp.messages.doitrequest
[MessageId:bc1c79cc1965489c84703f5898bd6fd8,
CorrelationId:bc1c79cc1965489c84703f5898bd6fd8] 12/17/2014 9:42:08 PM
-05:00: Waiting for response to Myapp.Messages.DoItRequest from
q.myapp.messages.doitrequest
[MessageId:bc1c79cc1965489c84703f5898bd6fd8,
CorrelationId:bc1c79cc1965489c84703f5898bd6fd8] 12/17/2014 9:42:08 PM
-05:00: Flushing outbound message queue q.myapp.messages.doitrequest (1 messages) 12/17/2014
9:42:08 PM -05:00: Received response to
Myapp.Messages.DoItRequest from
q.myapp.messages.doitrequest
[MessageId:bc1c79cc1965489c84703f5898bd6fd8,
CorrelationId:bc1c79cc1965489c84703f5898bd6fd8] in the form of
System.Threading.Tasks.Task`1[[Myapp.Messages.DoItResponse,
Myapp.Messages, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]] 12/17/2014 9:42:08 PM -05:00: Fetching existing
queues... System.TimeoutException : No response was received from the
bus within the configured timeout. Expected a
'Myapp.Messages.DoItResponse'.
它说我收到了回复......
9:42:08 PM -05:00: **Received response to
Myapp.Messages.DoItRequest from
q.myapp.messages.doitrequest**
[MessageId:bc1c79cc1965489c84703f5898bd6fd8,
CorrelationId:bc1c79cc1965489c84703f5898bd6fd8] **in the form of
System.Threading.Tasks.Task`1[[Myapp.Messages.DoItResponse**,
Myapp.Messages, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]] 12/17/2014
除了我没有
9:42:08 PM -05:00: Fetching existing
queues... System.TimeoutException : **No response was received from the
bus within the configured timeout. Expected a
'Myapp.Messages.DoItResponse'.**
我有什么遗失的东西需要检查吗?
答案 0 :(得分:3)
喔。我的。糟。
似乎我没有致电bus.Start();
来实际启动公交车。我创建/配置了总线,这似乎足以发送消息,但显然需要.Start();
来实际订阅并注意回复的响应。这解释了为什么我可以在后端中断并看到创建的响应,甚至看到队列中的消息返回,但实际请求从客户端超时。
答案 1 :(得分:1)
这很可能是因为调用线程的同步上下文。
要查看同步上下文是什么,请在
上设置监视System.Threading.Thread.CurrentThread.ExecutionContext.SynchronizationContext
如果它不为空,那么您可能会遇到死锁(请参阅下面文章的链接)。
如果您的测试用例如下所示:
[Test]
public async Task WhenSendingARequestWeShouldReceiveAResponse()
{
var result = await _bus.Request(new SomeRequest());
result.ShouldBeOfType<SomeResponse>();
}
然后尝试将其替换为:
[Test]
public async Task WhenSendingARequestWeShouldReceiveAResponse()
{
var result = await _bus.Request(new SomeRequest()).ConfigureAwait(false);
result.ShouldBeOfType<SomeResponse>();
}
或者这个:
[Test]
public Task WhenSendingARequestWeShouldReceiveAResponse()
{
return Task.Run(async () =>
{
var result = await _bus.Request(new SomeRequest());
result.ShouldBeOfType<SomeResponse>();
}
}
这将帮助您确定是否遇到了死锁或其他问题。
有关同步上下文的更多阅读,请查看http://msdn.microsoft.com/en-us/magazine/gg598924.aspx。顺便说一下,你在任何地方调用.Wait()对一个返回任务或任务的方法都要求麻烦。
答案 2 :(得分:0)
在我的情况下,我使用Environment.MachineName
为每个文档创建唯一的实例名称,但忘记了在本地端到端测试期间对其进行更改。原来,在同一台计算机上运行的两个可执行文件的计算机名称是相同的,谁知道呢?
var bus = new BusBuilder().Configure()
.WithNames("MyApplication", Environment.MachineName) // <-- Problem if running multiple instances on the same machine