Keeping objects between requests within the same session

  • Thread starter Thread starter Don
  • Start date Start date
D

Don

I have an ASP.NET program that references a VB.NET DLL. I had originally
planned for my DLL to put things in the HttpContext.Current collection so
that the ASP.NET can access them throughout an entire session (according to
a tip I saw on a website somewhere), but it turns out that it only lasts for
the current page request. If another page is requested, the contents of
HttpContext.Current are cleared. Except for the lack of proper scope,
HttpContext.Current gives me the functionality that I need.

Is there another class I can throw this stuff into, similar to the way
HttpContext.Current works, that will let the objects last for the entire
session and not just the current request? In ASP.NET there's a Session
class which has the right scope, but I don't know how -- or even if it's
possible -- to access that class from by VB.NET DLL.

- Don
 
Don said:
Is there another class I can throw this stuff into, similar to the way
HttpContext.Current works, that will let the objects last for the entire
session and not just the current request? In ASP.NET there's a Session
class which has the right scope, but I don't know how -- or even if it's
possible -- to access that class from by VB.NET DLL.

I forgot to mention that I need to be able to recreate this class in a
Windows app. I mean to do this so as to make it seem like a person running
a Windows app is like one session at a website. Again, HttpContext.Current
worked perfectly for everything...except that it just had the wrong scope
for my ASP.NET application.

- Don
 
Hi,

HttpContext.Current.Session

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

I have an ASP.NET program that references a VB.NET DLL. I had originally
planned for my DLL to put things in the HttpContext.Current collection so
that the ASP.NET can access them throughout an entire session (according to
a tip I saw on a website somewhere), but it turns out that it only lasts for
the current page request. If another page is requested, the contents of
HttpContext.Current are cleared. Except for the lack of proper scope,
HttpContext.Current gives me the functionality that I need.

Is there another class I can throw this stuff into, similar to the way
HttpContext.Current works, that will let the objects last for the entire
session and not just the current request? In ASP.NET there's a Session
class which has the right scope, but I don't know how -- or even if it's
possible -- to access that class from by VB.NET DLL.

- Don
 
I tried using that, but when working with my Windows App accessing the DLL,
the HttpContext.Current.Session resolves to Nothing. Is there some way to
initialize it? When I had my code working with just HttpContext.Current, I
needed to put the following line at the start of my Windows app:

HttpContext.Current = New HttpContext(Nothing, Nothing)

to initialize it, so to speak (I imagine this is explicitly doing what, in a
way, the environment is doing for my ASP.NET program). I don't know what to
do to initialize HttpContext.Current.Session at the start of my Windows app,
though.

- Don
 
After doing some research for my own project, it appears the following is the only solution

In a Windows application context information can be stored in the AppDomain.CurrentDomain which inherits from System.MarshalByRefObject, which is completely different object hierarchy for the web world. So in order to have your application work accross both platforms you must create an abstract layer for your context and your application will interact with the abstract/interface object. To accomplish this you will need a factory object that determines what environment you are in and call the appropriate class to return the abstract context. Here is a code example in C#, error handling and comments were not included in order to keep the size of the post small. All you need to do is implement the concrete class for dealing with the web environment. Constructive critisism is welcome

using System
namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for ContextFactory that is used to determine what type of context I will be dealing with
/// This is a static call and will be used in the application as the following: ContextFactory.GetContext(
/// You can set this up as a singleton if you like
/// </summary
public class ContextFactor

public ContextFactory(

/
// TODO: Add constructor logic her
/


public static IApplicationContext GetContext()

// You would normally have code to dermine the what context I am in and create the appropriate object
// In this example I am just creating the WindowsContext class and returning it as IApplicationContext
return (IApplicationContext)Activator.CreateInstance(Type.GetType("WindowsContext"))




using System

namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for IApplicationContext
/// </summary
public interface IApplicationContex

object GetObject(string objectName)
bool SetObject(string objectName, object myObject)



using System

namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for WindowsContext
/// </summary
public class WindowsContext : IApplicationContex

public WindowsContext(

/
// TODO: Add constructor logic her
/


public object GetObject(string objectName)

return AppDomain.CurrentDomain.GetData(objectName)


public bool SetObject(string objectName, object myObject)


try

AppDomain.CurrentDomain.SetData(objectName, objectName)
return true

catch (Exception ex

//do something with the exceptio

//then return fals
return false





using System
using iGoBusinessManager.EntityObjects.Entities

namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for Sample
/// </summary
public class Sampl



public Sample(

/
// TODO: Add constructor logic her
/


public Customer _CurrentCustomer

get

IApplicationContext myContext = ContextFactory.GetContext()
return (Customer)myContext.GetObject("Customer")


set

IApplicationContext myContext = ContextFactory.GetContext()
myContext.SetObject("Customer", value)








----- Don wrote: ----

I tried using that, but when working with my Windows App accessing the DLL
the HttpContext.Current.Session resolves to Nothing. Is there some way t
initialize it? When I had my code working with just HttpContext.Current,
needed to put the following line at the start of my Windows app

HttpContext.Current = New HttpContext(Nothing, Nothing

to initialize it, so to speak (I imagine this is explicitly doing what, in
way, the environment is doing for my ASP.NET program). I don't know what t
do to initialize HttpContext.Current.Session at the start of my Windows app
though

- Do
 
Back
Top