473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to determine if context is Windows Forms or Web Forms

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
Nov 17 '05 #1
6 3389
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

"Arsen V." <ar***@community.nospam> escribió en el mensaje
news:OG**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2
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

"Arsen V." wrote:
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

Nov 17 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Arsen V." <ar***@community.nospam> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #4
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/de...l/caching1.asp

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Os**************@TK2MSFTNGP14.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Arsen V." <ar***@community.nospam> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #5
Jeremy,

Good idea on using the Caching Application block.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeremy Williams" <je*********@netscape.net> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
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/de...l/caching1.asp

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:Os**************@TK2MSFTNGP14.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Arsen V." <ar***@community.nospam> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
> 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
>
>



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

---
hB

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mh | last post by:
Hi Folks- I'm trying to do a simple emulation of unix "locate" functionality in python for windows. Problem is I don't want to crawl/index optical drives. Do any of the windows people out...
1
by: Ryan Joseph So | last post by:
When you right click your mouse on the textbox it will popup a contextmenu then you can either cut, copy, paste. Is there a way to determine what process did the user choose on the context menu?...
2
by: cmrchs | last post by:
Hi, I am trying to create a site that will be used by both employees AND external users, so both types of users are trying to access the same pages. In the case of an employee on the intranet,...
0
by: Rafael Veronezi | last post by:
I would like to know if there's a way to implement both Windows Integrated and Forms authentication in my Asp.net application. It's an Intranet application wich I would like to enable from inside...
1
by: JIM.H. | last post by:
Hello, Is there a sample application in C# for windows and forms authentication, I need to create a login screen on the internet for our employees? Thanks, Jim.
2
by: cmrchs | last post by:
Hi, I am trying to create a site that will be used by both employees and external users, so both types of users trying to access the same pages. In the case of an employee on the intranet,...
1
by: ABC | last post by:
I have a new project which is a web site used by Internal and External users (login required users) and public users (no login required users). On internal users, all users login network using...
1
by: Allen Maki | last post by:
Hi Everybody, I need your help. I have MS Visual Studio .NET 2003. I noticed that if I added a context menu to a form. It will not work, unless you add the line:
1
by: Velvet | last post by:
I am looking for a tutorial or code samples to create a cross between Windows and Forms authentication. What we would like to do is utilize the Windows authentication for loggin into an...
1
by: Chris | last post by:
Hi, we run a webapplication where users must log in. So the web.config contains: <authentication mode="Forms"/and IIS is set to Anonymous authentification. Now we want the same application to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.