Tag Archives: Nancy.Bootstrappers.Unity

Minimal sample: Self hosted Nancy using Owin, Unity bootstrapper including xUnit test

Note: Depending on your environment, you might need to make a name space reservation

netsh http add urlacl url=http://+:8080/ user=DOMAIN\username

See also Hosting Nancy with Owin for more details.

Programm.cs

using Microsoft.Owin.Hosting;
 
namespace NancyOwinBox
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "http://+:8080";
 
            using (WebApp.Start(url))
            {
                Console.WriteLine("Running on {0}", url);
                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }
        }
    }
}

HomeModule.cs

using Nancy.Owin;
 
namespace NancyOwinBox
{
    public class HomeModule : NancyModule
    {
        public HomeModule(IMessageHelper messageHelper)
        {
            Get["/"] = x =>
            {
                var env = this.Context.GetOwinEnvironment();
                return string.Format(messageHelper.HelloWorld());
            };
        }
    }
}

Startup.cs

using Microsoft.Practices.Unity;
using Nancy.Owin;
using Owin;
 
namespace NancyOwinBox
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            IUnityContainer container = new UnityContainer();
 
            container.RegisterType<IMessageHelper, MessageHelper>();
 
            app.UseNancy(new NancyOptions
            {
                EnableClientCertificates = true,
                Bootstrapper = new NancyOwinBoxBootstrapper(container)
            });
        }
    }
}

NancyOwinBoxBootrapper.cs

using Microsoft.Practices.Unity;
using Nancy.Bootstrappers.Unity;
 
namespace NancyOwinBox
{
    public class NancyOwinBoxBootstrapper : UnityNancyBootstrapper
    {
        private IUnityContainer _container;
 
        public NancyOwinBoxBootstrapper(IUnityContainer container)
        {
            _container = container;
        }
 
        protected override IUnityContainer GetApplicationContainer()
        {
            return _container;
        }
    }
}

xUnit 2.x test file: NancyOwinBoxTests.cs

using Microsoft.Practices.Unity;
using Nancy;
using Nancy.Testing;
using Xunit;
 
namespace NancyOwinBox.Testing
{
    public class NancyOwinBoxTests
    {
        [Fact]
        public void ShouldGetHomeScreen() {
 
            IUnityContainer container = new UnityContainer();
 
            container.RegisterType<IMessageHelper, MessageHelper>();
 
            NancyOwinBoxBootstrapper bootstrapper = new NancyOwinBoxBootstrapper(container);
 
            var browser = new Browser(bootstrapper);
 
            var result = browser.Get("/", with =>
            {
                with.HttpRequest();
            });
 
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
 
            Assert.True(true);
        }
    }
}

package.config – NancyOwinBox

<packages>
  <package id="CommonServiceLocator" version="1.0" targetFramework="net45" >
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" >
  <package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net45" >
  <package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net45" >
  <package id="Nancy" version="1.1" targetFramework="net45" /&gt;
  <package id="Nancy.Bootstrappers.Unity" version="1.1" targetFramework="net45" >
  <package id="Nancy.Owin" version="1.1" targetFramework="net45" >
  <package id="Owin" version="1.0" targetFramework="net45" >
  <package id="Unity" version="2.1.505.0" targetFramework="net45" >
</packages>

package.config. NancyOwinBox.Testing

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="CsQuery" version="1.3.3" targetFramework="net45" />
  <package id="Nancy" version="1.1" targetFramework="net45" />
  <package id="Nancy.Bootstrappers.Unity" version="1.1" targetFramework="net45" />
  <package id="Nancy.Testing" version="1.1" targetFramework="net45" />
  <package id="Unity" version="3.5.1404.0" targetFramework="net45" />
  <package id="xunit" version="2.0.0" targetFramework="net45" />
  <package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
  <package id="xunit.assert" version="2.0.0" targetFramework="net45" />
  <package id="xunit.core" version="2.0.0" targetFramework="net45" />
  <package id="xunit.extensibility.core" version="2.0.0" targetFramework="net45" />
  <package id="xunit.runner.visualstudio" version="2.0.0" targetFramework="net45" />
</packages>

Download sample solution including all sources:
NancyOwinBox.zip – Visual Studio 2013