Re: Keeping objects between requests within the same session
From: John A. Fuqua (johnf_at_igofigure.com)
Date: 03/13/04
- Next message: al: "MDI form in ASP.NET?"
- Previous message: NWx: "Re: Securing access to other files in an ASP.NET application"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 13 Mar 2004 12:41:06 -0800
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.Runtime
{
/// <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 ContextFactory
{
public ContextFactory()
{
//
// TODO: Add constructor logic here
//
}
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.Runtime
{
/// <summary>
/// Summary description for IApplicationContext.
/// </summary>
public interface IApplicationContext
{
object GetObject(string objectName);
bool SetObject(string objectName, object myObject);
}
}
using System;
namespace iGoBusinessManager.Runtime
{
/// <summary>
/// Summary description for WindowsContext.
/// </summary>
public class WindowsContext : IApplicationContext
{
public WindowsContext()
{
//
// TODO: Add constructor logic here
//
}
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 exception
//then return false
return false;
}
}
}
}
using System;
using iGoBusinessManager.EntityObjects.Entities;
namespace iGoBusinessManager.Runtime
{
/// <summary>
/// Summary description for Sample.
/// </summary>
public class Sample
{
public Sample()
{
//
// TODO: Add constructor logic here
//
}
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 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
"Teemu Keiski" <joteke@aspalliance.com> wrote in message
news:OCzt57K$DHA.1548@TK2MSFTNGP12.phx.gbl...
> Hi,
>> HttpContext.Current.Session
>> --
> Teemu Keiski
> MCP, Microsoft MVP (ASP.NET), AspInsiders member
> ASP.NET Forum Moderator, AspAlliance Columnist
>> "Don" <unknown@oblivion.com> wrote in message
> news:wqt%b.605594$JQ1.508433@pd7tw1no...
> 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
>>>
- Next message: al: "MDI form in ASP.NET?"
- Previous message: NWx: "Re: Securing access to other files in an ASP.NET application"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|