Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NancyFX reflect changes immediately for static contents

In ASP.NET, whenever I'm running my server in Debug mode from VS2012,any changes I make to static contents (js,css, etc) are reflected immediately upon saving.

In NancyFX, I need to restart my server everytime I make changes to static content. I'm assuming this is because VS needs to copy the static contents to output directory each time I run the server.

Is there anyway to reflect the changes made to static contents immediately upon saving?

Here's my configuration for static contents

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

This is probably relavant too. I'm running this under a console application with nancyfx main loop written like this:

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
    }
}

This will be ran under ubuntu w/ nginx in case you're wondering why I'm not using Nancy.ASPNET.hosting

like image 576
l46kok Avatar asked Sep 15 '13 16:09

l46kok


1 Answers

Nancy's default root path is the bin folder of your application. If you want updates on your assets to be reflected after a refresh without needing to rebuild you can use a custom Nancy.IRootPathProvider you can do something like the following:

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
    }
}

This also allows for production builds to serve directly from their bin directory as may be the case when deploying the application.

like image 108
James Hulse Avatar answered Oct 14 '22 02:10

James Hulse