11

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

5 Answers 5

7

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.

4
+50

Is it because your site is executing out of your bin\ directory and your static content is being served out of that folder and is copied there when you compile - so when you update static content while it's running, it's your source folder that gets updated and not in your bin\ folder?

1
  • Yeah, that seems to be the problem. In case of IIS, updating my contents from Source reflects the changes immediately, so I'm assuming IIS automatically copies the changed source content to bin\. I'm wondering if Nancy.Self.Hosting has a similar functionality.
    – TtT23
    Sep 20, 2013 at 10:02
3

Not sure what your exact setup is, but there is no issue with updating either views or static content and having it immediately reflect changes. I just tried it locally (using 0.20.0) with Nancy.Hosting.Aspnet host and it worked fine

1
  • I'm using 0.20.0 as well but I'm using Nancy.Hosting.Self. Does that matter at all?
    – TtT23
    Sep 15, 2013 at 19:22
2

Are you sure your changes files are being saved when the server is running?

IISExpress (for me, not for others) keeps a lock on all the view files whilst it is running. This means I need to restart IISExpress in order to save any changes.

Maybe something similar is happening with you?

1
  • It works well under IISExpress. Where it doesn't work is self hosting. Just in case, I checked if the files were locked but they weren't.
    – TtT23
    Sep 25, 2013 at 17:20
2

You can set the view location as described in https://github.com/NancyFx/Nancy/wiki/View-location-conventions to the location of your solution source path. Get the location from a app.config variable so that you can switch depending on the environment (debug/production) you are running in. Then nancy will pickup the views you save in your IDE. Don't forget to set StaticConfiguration.Caching.EnableRuntimeViewUpdates = true; so that all changes ares picked up at a page refresh.

1
  • Thanks! EnableRuntimeViewUpdates is exactly what I was looking for. Saved me from moving to Owin/ASP host!
    – Zelid
    Dec 1, 2014 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.