当站点中集成ServiceStack.RabbitMQ时快速处理异步请求时,官方建议初始化如下:
public class AppHost : AppHostHttpListenerBase { public AppHost() : base("Rabbit MQ Test Host", typeof(HelloService).Assembly) {} public override void Configure(Container container) { container.Register<IMessageService>(c => new RabbitMqServer()); var mqServer = container.Resolve<IMessageService>(); mqServer.RegisterHandler<Hello>(ExecuteMessage); mqServer.Start(); } }
https://github.com/ServiceStack/ServiceStack/wiki/Rabbit-MQ
而当我们默认将此启动程序放入asp.net站点里的global.asa里:
protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); }
会发现默认的站点里的静态页面无法运行了,经过分析发现:ServiceStack.HostConfig初始化时,其属性出现问题了WebHostPhysicalPath = "~".MapServerPath(),深入看一下MapServerPath的详细实现
public static string MapServerPath(this string relativePath) { return HostContext.IsAspNetHost ? relativePath.MapHostAbsolutePath() : relativePath.MapAbsolutePath(); }
public static bool IsAspNetHost { get { return ServiceStackHost.Instance is AppHostBase; } }
看到这里应该发现了,这个方法判断引起的,因为当前初化的是ServiceStack.AppHostHttpListenerBase,导致HostConfig.WebHostPhysicalPath 的值是不正确的,指向了站点\bin目录了,导致正常的静态资源得不到正确处理。 找到原因了,解决这个问题也就容易多了,直接在上面的初始化方法里加一句:
var cfg = new HostConfig(); cfg.WebHostPhysicalPath = "~".MapAbsolutePath(); SetConfig(cfg);
时间: 2024-12-25 13:53:37