CHAPTER 2
This chapter will walk you through setting up your development and test environment.
In order to be able to follow the concepts and code described here, you will need a working installation of Visual Studio; any version from 2012 upwards will do, including the shiny new Community Edition. I myself have used Visual Studio 2013, but you are free to use a different version; basically, you’ll need a Visual Studio version that can handle .NET 4.x and NuGet packages.
NuGet is Visual Studio’s native packaging system. Most software companies, including Microsoft, as well as independent developers and communities (such as the NHibernate community) provide their libraries and frameworks through NuGet. It’s an easy way to fetch software packages and their dependencies.
For the purpose of the examples in this book, you will need the following packages:
OWIN Self Host
![]()
OWIN Web API
![]()
Entity Framework Code First
![]()
NHibernate
![]()
Unity
![]()
Unity Bootstrapper for ASP.NET MVC
![]()
Enterprise Library Logging Application Block
![]()
ASP.NET Identity (Entity Framework)

ASP.NET Identity (NHibernate)
![]()
OWIN Identity
![]()
AspNet.Identity.EntityFramework.Multitenant

WatiN
![]()
If you want to follow the examples described in this book, you will need to set up different host names for you development machine. This is so that we can make use of the host header tenant identification pattern, described in an earlier chapter. If you have administrative access to the DNS server that serves your local connection (work or home), you can add different aliases to a static IP address and then use that IP address instead of using a DHCP-assigned one.
Another option is to use a free service such as xip.io, which translates host names in domain xip.io into IP addresses; for example, 127.0.0.1.xip.io will automatically translate to 127.0.0.1, 192.168.1.1.xip.io becomes 192.168.1.1, and so on. This is so we can use the strategy for identifying tenants based on the host header mentioned earlier, but it only works with IP addresses, not host names.
Another option is to configure static IP-name mappings through the %WINDIR%\System32\Drivers\Etc\hosts file. This file contains lines in the format:
Code Sample 1
<IP address> <host name> <alias 1> … <alias n> |
For example, you can add the following entry:
Code Sample 2
127.0.0.1 localhost abc.com xyz.net |
This will tell the IP stack on Windows that the hosts named abc.com and xyz.net will both translate to the loopback address 127.0.0.1, without the need for a DNS lookup.
Setting up a multitenant site in Internet Information Services (IIS) is easy: just open the IIS Manager applet and select Sites > Add website. Then add a host name that you wish to handle:

Now add a host name for all the additional tenants, select the site, and click Bindings.


Also important is the application pool: you can have different application pools serving each of the different tenants (the site bindings). This provides better isolation, in the sense that if something goes wrong for one of the tenants—perhaps an unhandled exception that renders the site unusable—it will not affect the others. Also, you can have the application pools running under different identities, which is nice if you wish to access different databases, or have different access levels. Just create as many application pools as you like in IIS Manager:

In case you will be using IIS Express, besides DNS or hosts file, you will also need to configure the website’s bindings to allow for other host names. Unfortunately, you will have to do it by hand, since IIS Express does not offer any graphical tool for that purpose; the site’s configuration file is located at: %HOMEPATH%\Documents\IISExpress\Config\ApplicationHost.config. Open it and locate the site entry that you wish to change; it should look like the following (minus name, id, physicalPath, and bindingInformation):
Code Sample 3
|
<site name="Multitenant" id="1"> <application path="/" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:\InetPub\Multitenant" /> </application> <bindings> <binding protocol="http" bindingInformation="*:80:localhost" /> </bindings> </site> </sites> |
You will probably see some sites already configured. In that case, you have to find the one you’re interested in, maybe through the physicalPath attribute, and add a binding element with a bindingInformation attribute pointing to the proper host name and port.
Code Sample 4
<bindings> <binding protocol="http" bindingInformation="*:80:localhost" /> <binding protocol="http" bindingInformation="*:80:abc.com" /> <binding protocol="http" bindingInformation="*:80:xyz.net" /> </bindings> |
This instructs IIS Express to also accept requests for hosts abc.com and xyz.net, together with localhost.