How to Share Session/Application State Across Different ASP.NET Web Applications

by Jayram Tallamraju


This article gives the details of how to share single Application/Session state across different ASP.NET web application projects developed. This is required for web applications developed separately for the sake of modularity and require integration into a single website at a later stage.

It is better to develop each module independent of other. If there is information that needs to be shared across modules, it can be achieved through database and/or Session/Application variables, depending on the requirements. With Visual Studio, each new ASP.NET web application project creates a new virtual directory. This means creating separate Session/Application state for each module/application. Drawback of this approach is there is no way to share same Session/Application across these different modules developed for same website.

Well, it is not a drawback if each module is truly a web application by itself and not part of single website or does not share any thing in common. But what happens when it comes to simple things like login? Mostly applications developed for single website are required to support Single Sign on (SSO) or Share/check some Session/Application variables.

Most of the cases, each module is created as web application, just to allow multiple teams to develop different functionality in parallel, and when it comes to deployment, each module is expected to be part of one single application. Sharing single login session across different modules without much programming effort is the goal of this article.

To allow isolated development of each module and integrate all the modules into single web site, you can do the following:

Develop modules independently:

Create each module as new ASP.NET web application (Using Visual Studio). It is possible that each module might be developed in different location or by different team. Having independent standalone projects make testing of each module easy.

At this point in development phase, each module will have its own virtual directory and it is fine. Focus at this point is developing and testing the functionality of module itself. But things will change at the time of integration and deployment.

Integrating different ASP.NET web applications:

When it is time to Integrate different modules into single application:

  1. Using Administrative Tools -> Internet Services Manager: Delete all virtual directories previously created for each module by Visual Studio .net.
  2. Create new ASP.NET Web application (Ex: MasterWeb) project.
  3. Open explorer and copy each module project directory, below MasterWeb project directory.
  4. In each module project directory, open "<ModuleProjectName>.csproj.webinfo" file
  5. Modify URLPath value in .csproj.webinfo file to:

    <Web URLPath = http://localhost/MasterWeb/<ModuleProjectName>/<ModuleProjectName>.csproj" />

    Why? Since there is no virtual directory created for each module, module project file should have right URL so that Visual Studio can open and compile the project.

  6. Open Web.Config file and remove all sections except <appSettings> section (You can move required sections to parent directory Web.Config file). Web.Config in Parent directory of this project will have the common settings required for all modules. Any module specific settings can be overridden in Web.Config file in module project directory (Ex: <appSettings>/connectionString value in may be different for each module).
  7. Make sure that each module .csproj.webinfo file has changes in Step 5.
  8. Make sure that Web.Config file in parent directory (MasterWeb) has all the correct settings required for all modules.
  9. Make sure that each module has Web.Config file with module specific settings in <appSettings> section. If module does not require module specific data, it can share common configuration from parent application (MasterWeb).
  10. Delete any Global.asax* files from each module.
  11. Rebuild all modules and make sure that all module project output assembly files are stored under <MasterWeb>/bin directory (Ref: Step 2).

Integration Testing:

Run the main MasterWeb application and from http://MachineName/MasterWeb and access each module by http://MachineName/MasterWeb/Module1/ module specific directories.

NOTE: All above changes should allow having single Session/Application across different modules. This will allow development of each module separately and integrating them at a later stage. Once deployed, this approach takes less memory as same Session/Application state is used across all modules. Solution described takes: "SAME BUT DIFFERENT" approach during development phase and "DIFFERENT BUT SAME" approach during integration/deployment phase.

Debugging the website:

Open MasterWeb application project in Visual Studio and add all the module projects from sub-directories (Add->Existing Projects). Save this into single solution on Visual Studio. You can compile/debug individual projects or all of them as you prefer.

Assumptions:

It assumed in this article that, most of the .NET/ASP.NET development is done using Microsoft Visual Studio.NET (IDE) and readers are familiar with Visual Studio.NET/ASP.NET.

Placeholders in the document:

"<", ">" brackets in the document represent that actual name should be replaced in place of the string enclosed with "<>". Example: "<MachineName>" should be replaced with actual machine name like "localhost" etc.



About the Author

From ASP101

Articles originally posted on ASP101.com

Comments

  • Pass session from one project to other in same solution.

    Posted by Anuj K on 03/20/2015 03:47am

    Sir, I have two project in single solution such as Main_Project and Sub_Project. In Main_Project i have Login.aspx page where i am writing value in Session after that i am redirecting to page which is in Sub_Project and in page load i want to read the session value which i have stored in previous page of project one. For this purpose i have used SQLServer session mode where the session value has been saved in database but i am not able to read this value in next page. So please reply with solution.

    Reply
  • erger

    Posted by hrtherr on 06/20/2013 03:02am

    wfrwefewfewf

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

  • You may not realize the complexity you could be creating when developing mobile apps – many companies don't initially. You could be doubling your IT costs for development and delivery; not to mention, risking sales, productivity and brand satisfaction. Read this IBM-commissioned Forrester Study to understand the key cost drivers of mobile app delivery -- for both customer-facing and enterprise applications. Find out how you could lower costs and increase success with the right strategy and investment.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date