【问题标题】:NancyFX reflect changes immediately for static contentsNancyFX 立即反映静态内容的更改
【发布时间】:2015-05-19 23:01:33
【问题描述】:

在 ASP.NET 中,每当我从 VS2012 以调试模式运行服务器时,我对静态内容(js、css 等)所做的任何更改都会在保存后立即反映出来。

在 NancyFX 中,每次更改静态内容时都需要重新启动服务器。我假设这是因为每次运行服务器时 VS 都需要将静态内容复制到输出目录。

保存后是否会立即反映对静态内容所做的更改?

这是我的静态内容配置

public class MainBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureConventions(NancyConventions nancyConventions)
    {
        nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Scripts"));
        base.ConfigureConventions(nancyConventions);
    }
}

这也可能是相关的。我在一个控制台应用程序下运行它,nancyfx 主循环如下所示:

class Program
{
    const ushort port = 64402;
    const string escapeString = "/Terminate";

    static void Main(string[] args)
    {
        NancyHost host;

        #region Making new instance of NancyHost
        var uri = new Uri("http://localhost:" + port + "/");
        var config = new HostConfiguration(); config.UrlReservations.CreateAutomatically = true;

        host = new NancyHost(config, uri);
        #endregion
        #region NancyFX hosting loop
        try
        {
            host.Start();

            Console.Write("Start hosting the Fate/Another ranking system frontend\n" +
                "\t\"" + uri + "\"\n" +
                "To stop the hosting, input \"" + escapeString + "\".\n\n");
            do Console.Write("> "); while (Console.ReadLine() != escapeString) ;
        }
        catch (Exception e)
        {
            Console.WriteLine("Unhandled exception has been occured!\n"
                + e.Message);
            Console.ReadKey(true);
        }
        finally
        {
            host.Stop();
        }

        Console.WriteLine("Goodbye");
        #endregion
    }
}

这将在带有 nginx 的 ubuntu 下运行,以防你想知道我为什么不使用 Nancy.ASPNET.hosting

【问题讨论】:

    标签: c# asp.net .net visual-studio-2012 nancy


    【解决方案1】:

    Nancy 的默认根路径是应用程序的bin 文件夹。如果您希望在刷新后反映资产更新而不需要重建,您可以使用自定义 Nancy.IRootPathProvider,您可以执行以下操作:

    public class NancyCustomRootPathProvider : IRootPathProvider
    {
        public string GetRootPath()
        {
    #if DEBUG
            return Directory.GetParent(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)).FullName;
    #else
            return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    #endif
        }
    }
    

    这还允许生产构建直接从其bin 目录提供服务,这可能是部署应用程序时的情况。

    【讨论】:

      【解决方案2】:

      是不是因为您的站点是在 bin\ 目录之外执行的,而您的静态内容是从该文件夹中提供的,并且在您编译时被复制到那里 - 所以当您在运行时更新静态内容时,它是您的源文件夹已更新且不在您的 bin\ 文件夹中?

      【讨论】:

      • 是的,这似乎是问题所在。在 IIS 的情况下,从 Source 更新我的内容会立即反映更改,所以我假设 IIS 自动将更改的源内容复制到 bin\。我想知道 Nancy.Self.Hosting 是否有类似的功能。
      【解决方案3】:

      不确定您的确切设置是什么,但更新视图或静态内容并使其立即反映更改没有问题。我刚刚使用 Nancy.Hosting.Aspnet 主机在本地(使用 0.20.0)进行了尝试,效果很好

      【讨论】:

      • 我也在使用 0.20.0,但我使用的是 Nancy.Hosting.Self。这有关系吗?
      【解决方案4】:

      您确定在服务器运行时保存您的更改文件吗?

      IISExpress(对我来说,不是对其他人)在运行时会锁定所有视图文件。这意味着我需要重新启动 IISExpress 才能保存任何更改。

      也许你也有类似的事情发生?

      【讨论】:

      • 在 IISExpress 下运行良好。它不起作用的地方是自托管。以防万一,我检查了文件是否被锁定,但没有。
      【解决方案5】:

      您可以将https://github.com/NancyFx/Nancy/wiki/View-location-conventions 中所述的视图位置设置为解决方案源路径的位置。从 app.config 变量中获取位置,以便您可以根据运行的环境(调试/生产)进行切换。然后 nancy 将获取您保存在 IDE 中的视图。 不要忘记设置StaticConfiguration.Caching.EnableRuntimeViewUpdates = true;,以便在页面刷新时获取所有更改。

      【讨论】:

      • 谢谢! EnableRuntimeViewUpdates 正是我想要的。让我免于迁移到 Owin/ASP 主机!
      猜你喜欢
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2019-04-24
      • 2021-12-12
      相关资源
      最近更新 更多