首页 > 其他 > 详细

ServiceStack.RabbitMQ在站点中使用时导致静态页面无法正常解析

时间:2016-10-21 19:32:05      阅读:224      评论:0      收藏:0      [点我收藏+]

当站点中集成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);

 


 



 

ServiceStack.RabbitMQ在站点中使用时导致静态页面无法正常解析

原文:http://www.cnblogs.com/yulinlover/p/5985656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!