I'm using the Nancy self hosting feature to do some unit testing. From my unit testing project (separate from the nancy API project), I use the following code to create an instance of my API:
var myApp = new NancyHost(new Uri(MockRestApiUrl), new Bootstrapper(), new HostConfiguration() { UrlReservations = new UrlReservations { CreateAutomatically = true } });
In the Bootstrapper I have some code which looks for a set of config files to preload into memory. Note that these are not static files to be served, but files which drive the configuration of the API.
The problem I have is that the Bootstrapper generates an exception because it can't find the configuration files it's looking for. I set the config file path via RootPathProvider.GetRootPath()
and get:
C:...\AppData\Local\Temp\b3f2483d-8c40-45d8-8ba3-dd8db2b9bfd3\b3f2483d-8c40-45d8-8ba3-dd8db2b9bfd3\assembly\dl3\bf80d16d\b3b00d8e_bd91d201\Mapping\PropertyMapping.json
When I look at the folder, the Mapping folder and config file are missing. I have tried marking these as 'Content' and 'Copy Always' in Visual Studio and have cleaned my solution several times, but no luck. How do I get these files copied to the self-hosted site?
I still have no idea why the injected path provider in my module was giving me an '...AppData...' path. However, I did get round it as follows:
public class SelfHostRootPathProvider : IRootPathProvider
{
public string GetRootPath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
And on a class derived from my app's Bootstrapper:
public class IntegrationBootstrapper : Bootstrapper
{
protected override IRootPathProvider RootPathProvider
{
get { return new SelfHostRootPathProvider(); }
}
}
And if you need the instantiation:
var myApp = new NancyHost(new Uri(ServiceUrl), new IntegrationBootstrapper(), new HostConfiguration() { UrlReservations = new UrlReservations { CreateAutomatically = true } });
That resolves the path to the debug folder of my unit test project. I then had to make sure that any configuration files were linked to my unit test project (Add -> Existing Item -> Add as link) and set as Content / Always Copy in Visual Studio in both projects.