Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to determine if context is Windows Forms or Web Forms

Reply
Thread Tools

How to determine if context is Windows Forms or Web Forms

 
 
Arsen V.
Guest
Posts: n/a
 
      08-05-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
 
      08-05-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." <> escribió en el mensaje
news:...
> 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
 
      08-05-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
 
      08-05-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]
-

"Arsen V." <> wrote in message
news:...
> 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
 
      08-05-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]" <> wrote in
message news:...
> 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]
> -
>
> "Arsen V." <> wrote in message
> news:...
> > 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
 
      08-05-2005
Jeremy,

Good idea on using the Caching Application block.


--
- Nicholas Paldino [.NET/C# MVP]
-

"Jeremy Williams" <> wrote in message
news:...
>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]" <> wrote
> in
> message news:...
>> 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]
>> -
>>
>> "Arsen V." <> wrote in message
>> news:...
>> > 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
 
      08-05-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
 
      08-06-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

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
return HttpStatusCode.ServiceUnavailable in the context.context.Response.StatusCode (Http response code 503) Flip Rayner ASP .Net 1 01-23-2007 06:35 AM
Strange Context Error: Context 0x197ee0 is disconnected in VS 2005 =?Utf-8?B?U3VuU21pbGU=?= ASP .Net 0 01-10-2006 03:59 PM
determine ASP.NET worker process user context programatically? =?Utf-8?B?RGFubnkgTWlsbGVy?= ASP .Net 3 10-03-2005 11:44 AM
Context.Items vs Context.Handler (passing values between pages) VS_NET_DEV ASP .Net 2 05-25-2004 01:16 PM
nuby: determine method passed and determine the receiver that received the method Peña, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57