How to determine if context is Windows Forms or Web Forms

A

Arsen V.

Hello,

I have a localization class that I want to use from either Web or Windows
Forms apps. Currently it stores some information in the HttpRuntime.Cache
object. I want to be able to determine the current context in which the
class is called. If it is called through ASP.NET, I want it to use the
HttpRuntime, but if it is called through Windows Forms app, I want it to use
a private static Hashtable.

What is the best way for an object to determine what kind of application is
calling it.

Thanks,
Arsen
 
O

Octavio Hernandez

Arsen,

I think there is no (easy) way to determine the kind of app using a library.
I think the best you can do is to define a constructor for your main class
where the user of the library can explicitly indicate the kind of app he is
developing. Something like:

namespace MyLibrary
{
public enum ClientType
{ ClientTypeNotSpecified, ClientTypeConsoleApp, ClientTypeWinForms,
ClientTypeWebForms, ClientTypeWebService }

public class MainClass
{
private ClientType clientType;
public MainClass(ClientType clientType)
{
this.clientType = clientType;
}
// rest of the methods use clientType to determine the type of app..
}
}

Regards - Octavio
 
G

Guest

I'm just about to run out the door now so I can't check to see if this is
100% correct but if memory serves me correctly you can use

System.Threading.Thread.CurrentContext

to get the current context. Not sure what you can do after that... sorry for
not being more help but I was just about to leave...

Brian Delahunty
Ireland

http://briandela.com/blog
 
N

Nicholas Paldino [.NET/C# MVP]

Arsen,

With all due respect to the previous posters, they are wrong in their
answers. You can check the static Current property of the HttpContext
class. If it returns null, then you are not running in an ASP.NET
environment.

However, I will say this, you should at the least abstract out the
interface to access the cache/hashtable. I *think* that you can use the
ASP.NET cache in non ASP.NET situations (I don't know what makes me think
that, so don't kill me if it is not true), and if not, you could easily
generate your own cache with similar semantics.

Hope this helps.
 
J

Jeremy Williams

I never had any luck getting the ASP.NET caching mechanism to work in a
WinForms app (without jumping through hoops like hosting ASP.NET from the
WinForms app).

I definitely agree that the implementation should be abstracted.
Alternately, the OP could use a "universal" caching mechanism, such as the
MS Caching Application Block:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/caching1.asp

Nicholas Paldino said:
Arsen,

With all due respect to the previous posters, they are wrong in their
answers. You can check the static Current property of the HttpContext
class. If it returns null, then you are not running in an ASP.NET
environment.

However, I will say this, you should at the least abstract out the
interface to access the cache/hashtable. I *think* that you can use the
ASP.NET cache in non ASP.NET situations (I don't know what makes me think
that, so don't kill me if it is not true), and if not, you could easily
generate your own cache with similar semantics.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arsen V. said:
Hello,

I have a localization class that I want to use from either Web or Windows
Forms apps. Currently it stores some information in the HttpRuntime.Cache
object. I want to be able to determine the current context in which the
class is called. If it is called through ASP.NET, I want it to use the
HttpRuntime, but if it is called through Windows Forms app, I want it to
use
a private static Hashtable.

What is the best way for an object to determine what kind of application
is
calling it.

Thanks,
Arsen
 
N

Nicholas Paldino [.NET/C# MVP]

Jeremy,

Good idea on using the Caching Application block.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeremy Williams said:
I never had any luck getting the ASP.NET caching mechanism to work in a
WinForms app (without jumping through hoops like hosting ASP.NET from the
WinForms app).

I definitely agree that the implementation should be abstracted.
Alternately, the OP could use a "universal" caching mechanism, such as the
MS Caching Application Block:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/caching1.asp

in
message news:[email protected]...
Arsen,

With all due respect to the previous posters, they are wrong in their
answers. You can check the static Current property of the HttpContext
class. If it returns null, then you are not running in an ASP.NET
environment.

However, I will say this, you should at the least abstract out the
interface to access the cache/hashtable. I *think* that you can use the
ASP.NET cache in non ASP.NET situations (I don't know what makes me think
that, so don't kill me if it is not true), and if not, you could easily
generate your own cache with similar semantics.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arsen V. said:
Hello,

I have a localization class that I want to use from either Web or Windows
Forms apps. Currently it stores some information in the HttpRuntime.Cache
object. I want to be able to determine the current context in which the
class is called. If it is called through ASP.NET, I want it to use the
HttpRuntime, but if it is called through Windows Forms app, I want it
to
use
a private static Hashtable.

What is the best way for an object to determine what kind of
application
is
calling it.

Thanks,
Arsen
 
H

hB

else if simple work, do hashtable on both type of apps (consistent
interface that we need).
[Probably Cache object of type Static-Singleton]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top