left-icon

ASP.NET Multitenant Applications Succinctly®
by Ricardo Peres

Previous
Chapter

of
A
A
A

CHAPTER 2

Setup

Setup


Introduction

This chapter will walk you through setting up your development and test environment.

Visual Studio

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

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

Address mapping

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.

IIS

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:

Setting up a multitenant site in IIS

  1. Setting up a multitenant site in IIS

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

Adding additional host names to a site

  1. Adding additional host names to a site

All the site's host names

  1. All the site's host names

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:

Creating an application pool for a tenant

  1. Creating an application pool for a tenant

IIS Express

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

<sites>

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

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.