我是Rebus的新手。 我想问一个问题:
我测试Server mode,但它不起作用。当Web应用程序启动时,它只处理消息(来自pubsubsample.websubscriber1.input队列)。
BTW,它在单向客户端模式下运行良好。(仅发送消息)
以下是服务器模式的代码段:
public class CheckStatus : IHandleMessages<NewTradeRecorded>
{
readonly IBus bus;
public CheckStatus(IBus bus)
{
this.bus = bus;
}
public void Handle(NewTradeRecorded message)
{
}
}
Asp.net MVC
protected void Application_Start()
{
using (var adapter = new BuiltinContainerAdapter())
{
adapter.Register(() => new CheckStatus(adapter.Bus));
Configure.With(adapter)
.Transport(t => t.UseMsmqAndGetInputQueueNameFromAppConfig())
.MessageOwnership(o => o.FromRebusConfigurationSection())
.CreateBus()
.Start();
adapter.Bus.Subscribe<NewTradeRecorded>();
}
}
的web.config
<rebus inputQueue="pubsubsample.websubscriber1.input" errorQueue="pubsubsample.websubscriber1.error" workers="1" maxRetries="5">
<endpoints>
<add messages="Trading.Messages" endpoint="trading.input"/>
</endpoints>
答案 0 :(得分:3)
要回答你的第一个问题,请卤面是否支持出版,并从相同的过程,答案是肯定的订阅 - 没有技术理由,你为什么不能订阅消息,并从同一发布同样的信息流程,包括您的Web应用程序。
是否是另一回事:)
.NET中的Web应用程序本质上是一种瞬态,即当IIS确定是时候回收它们时它们会被回收,然后订阅消息通常不是最好的想法,因为您的应用程序可能不会在运行时运行一个事件已经发布,因此无法处理它。
然后,当它因为IIS向其发送Web请求而唤醒时,您的应用程序可能会等待处理1,000,000个事件,这需要花费很长时间才能完成。
在某些情况下,我听说过有人想在Web应用程序中使用Rebus pub / sub来保持Web应用程序中的缓存更新 - 但是他们遇到了严重问题,这是因为IIS支持重叠两个实例同一个Web应用程序 - 嘿,突然间,一会儿,同一个Web应用程序的两个实例正在运行,从而允许Web应用程序即将关闭以抢夺应该由新实例处理的一些事件。 / p>
出于这些原因,一般情况下我会不建议在网络应用程序中执行pub / sub。
那么,为什么你的酒吧/子项不起作用?第一件事:创建后不要立即丢弃容器适配器! :)
请改为:
static readonly _stuffToDispose = new List<IDisposable>();
protected void Application_Start()
{
var adapter = new BuiltinContainerAdapter();
_stuffToDispose.Add(adapter);
adapter.Register(() => new CheckStatus(adapter.Bus));
Configure.With(adapter)
.Transport(t => t.UseMsmqAndGetInputQueueNameFromAppConfig())
.MessageOwnership(o => o.FromRebusConfigurationSection())
.CreateBus()
.Start();
adapter.Bus.Subscribe<NewTradeRecorded>();
}
protected void Application_End()
{
_stuffToDispose.ForEach(d => d.Dispose());
}
这样,您的公交车将不会在您的网络应用程序启动后立即停止处理消息。