CF Buildpack for ASP.NET apps to use Redis as Session Store – No Code Required

Alfus Jaganathan
Cloud Solutions Architect
May 22, 2019
Rate this article
Views    2678

In one of my previous articles (Redis backed session), I described about how Steeltoe helps in backing ASP.NET Session to Redis. The article also describes more about ASP.NET Session, 12-factors-process, etc. Please read through the article once, if not already, before continuing further.

This article explains how to make Redis as the session store of your ASP.NET application when running in PCF, without any code changes. If you are familiar with Cloud Foundry, you should be already familiar with buildpacks and its staging process. If not, please refer to cloud foundry, buildpacks and buildpack staging process, before continuing further.

One problem you notice in the previous approach is that you have to do some code changes and manual configuration changes, to get Session backed by Redis. That problem will be solved completely using this supply buildpack approach. This especially helps when doing a lift and shift, with minimal or no code change.

How does this buildpack work?

This is a supply buildpack that will detect the bounded redis service instance and modifies the web.config with the sessionState and machineKey sections.

  • Uses Steeltoe connectors to build the connection configuration
  • Any existing sessionState section(s) will be replaced with a custom one with valid connection string
  • Any existing machineKey section(s) will be replaced with new validation and decryption keys

Highlights

  • No code change required to persist session to redis, when pushing any ASP.NET application to PCF
  • So it reduces the effort in lifting and shifting a legacy ASP.NET application to PCF

What do you need to make use of?

  • PCF environment with redis tile in market place
  • A redis service instance created
  • cf push access to the PCF environment

How to use it?

To enable redis backed session in the application, please follow the below steps.

  • Install nuget package Microsoft.Web.RedisSessionStateProvider in your application, preferrably the latest one.
  • Add the buildpack in your application manifest.yml as in the example below. You can pick the latest release here.
---
applications:
- name: redis-buildpack-sample
  memory: 1024M
  stack: windows2016
  buildpacks:
   - https://github.com/alfusinigoj/redis-session-aspnet-buildpack/releases/download/1.0.0/redis-session-buildpack-win-x64.zip
   - https://github.com/cloudfoundry/hwc-buildpack/releases/download/v3.1.8/hwc-buildpack-windows2016-v3.1.8.zip
  services:
   - my_redis_service
  • To bind the application to the redis service to the application, add your redis service instance name in the manifest.yml as above
  • Push the application to PCF, you will be seeing logs as below
=================== Redis Session Buildpack execution started ==================
================================================================================
-----> Removing existing machineKey configuration...
-----> Creating machineKey section with new validation, decryption keys and SHA1 validation...
-----> Removing existing session configurations...
-----> Found redis connection 'xxxxxxxxxxxxxxx,password=xxxxxxxxxxxxxxxxxxx,allowAdmin=false,abortConnect=true,resolveDns=false,ssl=false'
-----> Creating sessionState section with the above connection string...
================================================================================
=================== Redis Session Buildpack execution completed ================
Hope you had fun coding!

Subscribe To Our Newsletter
Loading

Leave a comment