A couple months ago I wrote a post on how to host WCF services in IIS that expose themselves as endpoints on the Windows Azure AppFabric Service Bus. The principal challenge in this scenario is that IIS/WAS relies on message-based activation and will only launch the host after the first request comes in. However, until the host is launched the service will not connect to the Service Bus, and consequently will never receive a message. A classic catch-22.
The solution I proposed was to leverage the Application Warm-Up Extension for IIS 7.5, which will proactively load and initialize processes before the first request arrives. While this is acceptable, I’ve found a better solution using the Windows Server AppFabric Autostart (thanks to conversations with Ron Jacobs).
Last week I spent some time setting up Windows Server AppFabric Cache in anticipation of additional tasks this week. The first task is configuring an ASP.NET web application to use Windows Server AppFabric Caching for the Session State Provider. This allows the web application to spread session objects across the entire cache cluster, resulting in greater scalability.
Below is a walkthrough on how to configure this scenario. In addition to this post, I recommend you take a look at this article on MSDN.
Open up the Cache PowerShell console (Start –> Windows Server AppFabric –> Caching Administration Windows PowerShell). This will automatically import the DistributedCacheAdministration module and use the CacheCluster.
Start the Cache Cluster (if not already started). Run the following command in the PowerShell console:
Start-CacheCluster
Create a new cache that you will leverage for your session state provider. Run the following command in the PowerShell console:
New-Cache MySessionStateCache
Create a new ASP.NET Web Application in Visual Studio 2010 targeting .NET 4.0. This will create a sample project, complete with master page which we’ll leverage later on.
Add references to the Microsoft.ApplicationServer.Caching.Client and Microsoft.ApplicationServer.Caching.Core. To do this, use the following steps (thanks to Ron Jacobs for the insight):
Right-click on your project and select Add Reference.
Select the Browse tab.
Enter the following folder name, and press enter:
%windir%SysnativeAppFabric
Locate and select both Microsoft.ApplicationServer.Caching.Client and Microsoft.ApplicationServer.Caching.Core assemblies.
Add the configSections element to the web.config file as the very first element element in the configuration element:
<!--configSections must be the FIRST element -->
<configSections>
<!-- required to read the <dataCacheClient> element -->
Add the dataCacheClient element to the web.config file, after the configSections element. Be sure to replace YOURHOSTNAME with the name of your cache host. In the PowerShell console you can get the HostName (and CachePort) by starting or restarting your cache).
<dataCacheClient>
<!-- cache host(s) -->
<hosts>
<host
name="YOURHOSTNAME"
cachePort="22233"/>
</hosts>
</dataCacheClient>
Add the sessionState element to the web.config file in the system.web element. Be sure that the cacheName is the same as the cache you created in step 4.
Now, we need a quick and easy way to test this. There are many ways to do this, below is mine. I loaded data into session, then created a button that writes the session data into a JavaScript alert. Quick and easy:
b.OnClientClick = "alert('PageLoadDateTime defined at " +
Session["PageLoadDateTime"] + "')";
return b;
}
Now, hit control-F5 to start your project. After it loads, click the button labeled “Click Me” – you should see the following alert:
That’s it! You have now configured your ASP.NET web application to leverage Windows Server AppFabric Cache to store all Session State.
While I was putting this together, I encountered two errors. I figured I’d share them here, along with resolution, in case any of you encounter the same problems along the way.
Configuration Error
Description: An error occurred during the processing of a configuration
file required to service this request. Please review the specific error
details below and modify your configuration file appropriately.
referred to does not exist. Contact administrator or use the Cache
administration tool to create a Cache.
If you received the above error message, it’s likely that the cacheName specified in the sessionState element is wrong. Update the cacheName to reflect the cache you created in step #4.
Configuration Error
Description: An error occurred during the processing of a configuration
file required to service this request. Please review the specific error
details below and modify your configuration file appropriately.
Parser Error Message: ErrorCode<ERRCA0017>:SubStatus<ES0006>:There is
a temporary failure. Please retry later. (One or more specified Cache
servers are unavailable, which could be caused by busy network or
servers. Ensure that security permission has been granted for this
client account on the cluster and that the AppFabric Caching Service
is allowed through the firewall on all cache hosts. Retry later.)
If you received the above error message, it’s likely that the host name specified in the dataCacheClient is wrong. Update the dataCacheClient host name to reflect the name of your host. Note: it’s likely that it’s just your machine name.
I struggled today to find a good “Getting started with Windows Server AppFabric Cache” tutorial – either my search fu failed me or it simply doesn’t exist. Nevertheless, I was able to piece together the information I needed to get started.
I recommend you break this up into three steps:
Installing Windows Server AppFabric
Configuring Windows Server AppFabric Cache
Testing Windows Server AppFabric Cache with Sample Apps
I think this article will serve as a good tutorial on getting started, and we can refer back to it as the basis for more advanced scenarios.
Under Display additional scenarios select Enterprise.
Now you’ll see an Enterprise tab. Select it, and choose Windows Server AppFabric. Click Install. This will start a multi-step process for installing Windows Server AppFabric (which in my case required two reboots to complete).
Configuring Windows Server AppFabric Cache
Open the Windows Server AppFabric Configuration Wizard (Start –> Windows Server AppFabric –> Configure AppFabric).
Click Next until you reach the Caching Service step. Check Set Caching Service configuration, select SQL Server AppFabric Caching Service Configuration Store Provider for the configuration provider, and click Configure.
Check Create AppFabric Caching Service configuration database, confirm the Server name, and specify a Database name. Click OK.
When asked if you want to continue, click Yes.
You will receive confirmation that your database was created and registered.
On the Cache Node step, confirm the selected port nodes.
You will be asked to continue and apply the configuration settings; select Yes.
On the last step you’ll click Finish.
Open up an elevated Windows PowerShell window.
Add the Distributed Cache administration module
Import-Module DistributedCacheAdministration
Set the context of your Windows PowerShell session to the desired cache cluster with Use-CacheCluster. You can run this without parameters to use the connection parameters provided during configuration.
Use-CacheCluster
Grant your user account access to the cache cluster as a client. Specify your user and domain name.
Grant-CacheAllowedClientAccount domainusername
Verify your user account has been granted access.
Get-CacheAllowedClientAccounts
Start the cluster.
Start-CacheCluster
Testing Windows Server AppFabric Cache with Sample Apps