我的应用程序由ASP.NET MVC 3应用程序和Windows服务组成,两者都在同一个盒子上.MVC应用程序使用MassTransit将消息放在MSMQ队列上.该服务使用MassTransit从MSMQ读取这些消息并执行一些工作.当我加载服务和应用程序并让它们同时运行时,一切似乎都正常.但是,如果我只加载Web应用程序,我的消息不会被添加到MSMQ并且基本上消失.奇怪的是,如果我加载我的Web应用程序然后启动并立即停止所述服务,则从Web应用程序发送的后续消息将添加到队列中.
没有很多使用MassTransit和StructureMap和/或MVC的好/现代示例,所以它可能是我的配置问题,但经过几个小时的试验和错误,我不知道如何继续.我也可能在滥用MassTransit - 我真正需要的是来自Web应用程序的火灾和遗忘解决方案......
我的服务如下:
class Program
{
static void Main(string[] args)
{
Bus.Initialize(sbc =>
{
sbc.UseMsmq();
sbc.VerifyMsmqConfiguration();
sbc.UseMulticastSubscriptionClient();
sbc.ReceiveFrom("msmq://localhost/receiver");
});
ObjectFactory.Initialize(x =>
{
x.AddRegistry(new EmailSenderRegistry());
});
var host = HostFactory.New(c =>
{
c.SetServiceName("MyApp.EmailMessageReader");
c.SetDisplayName("MyApp.EmailMessageReader");
c.SetDescription("MyApp.EmailMessageReader");
c.Service<ClientService>(a =>
{
a.ConstructUsing(service => new ClientService());
a.WhenStarted(o => o.Start());
a.WhenStopped(x=>x.Stop());
});
});
host.Run();
}
}
internal class ClientService
{
public void Start()
{
Bus.Instance.SubscribeConsumer<EmailPrepService>();
}
public void Stop()
{
// do nothing
}
}
public class EmailPrepService : Consumes<EmailMessage>.All
{
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试在使用MVVM Light Toolkit构建的WPF应用程序中创建一个简单的全局异常处理程序,但我很难让它工作.
问题是视图模型中出现的异常不会被App的UnhandledException处理程序捕获,即使我为Dispatcher和AppDomain注册一个侦听器,如下所示:
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += DomainUnhandledException;
DispatcherUnhandledException += App_DispatcherUnhandledException;
}
private void DomainUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var exception = unhandledExceptionEventArgs.ExceptionObject as Exception;
ShowExceptionMessage(exception);
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
ShowExceptionMessage(e.Exception);
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
我发现这个博客文章描述了问题点,使用此代码描述的解决方案剪切了视图模型:
// Throw the exception in the UI thread.
App.Current.RootVisual.Dispatcher.BeginInvoke(() => { throw new MyException(); });
Run Code Online (Sandbox Code Playgroud)
但是,我希望所有异常都冒泡到全局异常处理程序,而不仅仅是我自己在VM中抛出的异常处理程序.
所以问题是:在某个地方以某种方式将异常从其他线程重新抛入UI线程吗?
更新:为App的事件处理程序设置添加了更详细的代码.
我在控制器方法中有此代码;
try
{
return PhysicalFile("c:\temp\my-non-existing-file.txt", "text/plain");
}
catch (FileNotFoundException)
{
return NotFound();
}
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下不运行 catch 子句,而是将 a500 Internal Server Error返回给浏览器。使开发人员错误页面处于活动状态,表明FileNotFoundException确实抛出了 a ,但调用堆栈显示它来自中间件。
try
{
return PhysicalFile("c:\temp\my-non-existing-file.txt", "text/plain");
}
catch (FileNotFoundException)
{
return NotFound();
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释如何正确处理这种情况并返回404 Not Found吗?
更新:添加了完整堆栈(带有一些名称清理)