PC Review


Reply
Thread Tools Rate Thread

How to determine if context is Windows Forms or Web Forms

 
 
Arsen V.
Guest
Posts: n/a
 
      5th Aug 2005
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


 
Reply With Quote
 
 
 
 
Octavio Hernandez
Guest
Posts: n/a
 
      5th Aug 2005
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." <(E-Mail Removed)> escribiķ en el mensaje
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
=?Utf-8?B?QnJpYW4gRGVsYWh1bnR5?=
Guest
Posts: n/a
 
      5th Aug 2005
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
>
>
>

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Aug 2005
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 Removed)

"Arsen V." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Jeremy Williams
Guest
Posts: n/a
 
      5th Aug 2005
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]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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 Removed)
>
> "Arsen V." <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Aug 2005
Jeremy,

Good idea on using the Caching Application block.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Jeremy Williams" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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]" <(E-Mail Removed)> wrote
> in
> message news:(E-Mail Removed)...
>> 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 Removed)
>>
>> "Arsen V." <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      5th Aug 2005
Why don't you just see if HttpContext.Current is null or not?

-Brock
DevelopMentor
http://staff.develop.com/ballen



> 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




 
Reply With Quote
 
hB
Guest
Posts: n/a
 
      6th Aug 2005
else if simple work, do hashtable on both type of apps (consistent
interface that we need).
[Probably Cache object of type Static-Singleton]

---
hB

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Forms - Opening forms within forms Dan Tallent Microsoft C# .NET 20 27th Aug 2008 06:25 PM
Re: Windows Forms - ListView context menu Tom P. Microsoft C# .NET 0 24th Jul 2008 07:54 PM
Windows Forms - Is there a way to determine the "context" in which WM_NCHITTEST Message is being used? NonNB Microsoft C# .NET 0 10th Apr 2007 07:42 AM
How to determine if context is Windows Forms or Web Forms Arsen V. Microsoft Dot NET Framework 6 6th Aug 2005 10:24 AM
How to determine if context is Windows Forms or Web Forms Arsen V. Microsoft C# .NET 6 6th Aug 2005 10:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:49 PM.