left-icon

ASP.NET Multitenant Applications Succinctly®
by Ricardo Peres

Previous
Chapter

of
A
A
A

CHAPTER 6

Web Services

Web Services


Introduction

Web services, of course, are also capable of handling multitenant requests. In the .NET world, there are basically two APIs for implementing web services: Windows Communication Foundation (WCF) and the new Web API. Although they share some overlapping features, their architecture and basic concepts are quite different. While a thorough discussion of these APIs is outside the scope of this book, let’s see some of the key points that are relevant to multitenancy.

WCF

WCF, by default, does not make use of the ASP.NET pipeline, which means that HttpContext.Current is not available. This is so that WCF has a more streamlined, focused model where the classic pipeline is not needed (but it is possible to enable it).

If we do not want to use the ASP.NET pipeline, we need to change our implementation of the tenant identification strategy. Inside a WCF method call, it is always possible to access the current request context through the OperationContext.Current or WebOperationContext.Current (for REST web services) properties. So, we need to write one implementation of the Tenant Identification strategy that knows how to fetch this information:

Code Sample 67

public class WcfHostHeaderTenantIdentification : ITenantIdentifierStrategy

{

     public String GetCurrentTenant(RequestContext context)

     {

          var request = WebOperationContext.Current.IncomingRequest;

          var host = request.Headers["Host"];

 

          return host.Split(':').First().ToLower();

     }

}

On the other side, if ASP.NET pipeline is an option, we just need to enable it through XML configuration, through the aspNetCompatibilityEnabled attribute:

Code Sample 68

<system.serviceModel>

     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

</system.serviceModel>

Or, we can enable it through an attribute in code:

Code Sample 69

[AspNetCompatibilityRequirements(

     RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

public class DateService : IDateService

{

     //rest goes here

}

Note: For a good discussion of the ASP.NET compatibility mode, check out this article.

Web API

ASP.NET Web API is the new API for writing REST web services with the .NET framework. It closely follows the ASP.NET MVC model, and is thus based on controllers and actions.

Currently, it can be hosted in either IIS (the traditional way) or in an OWIN-supported host. In the first case, all of the classic ASP.NET APIs (System.Web.DLL) are available, including HttpContext.Current, so you can use the same strategy implementations that were described for MVC and Web Forms. For OWIN, read Chapter 8 for some tips.

Unit Testing

Whenever the ASP.NET classic APIs are available, we can follow the prescription described in the Unit Testing section of Chapter 5. For OWIN, jump to Chapter 8.

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.