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
Octavio Hernandez - 05 Aug 2005 15:08 GMT
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
> Hello,
>
[quoted text clipped - 12 lines]
> Thanks,
> Arsen
Brian Delahunty - 05 Aug 2005 15:11 GMT
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
> Hello,
>
[quoted text clipped - 10 lines]
> Thanks,
> Arsen
Nicholas Paldino [.NET/C# MVP] - 05 Aug 2005 16:19 GMT
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.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hello,
>
[quoted text clipped - 12 lines]
> Thanks,
> Arsen
Jeremy Williams - 05 Aug 2005 17:09 GMT
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/cac
hing1.asp
> Arsen,
>
[quoted text clipped - 27 lines]
> > Thanks,
> > Arsen
Nicholas Paldino [.NET/C# MVP] - 05 Aug 2005 17:19 GMT
Jeremy,
Good idea on using the Caching Application block.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
>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
[quoted text clipped - 40 lines]
>> > Thanks,
>> > Arsen
hB - 06 Aug 2005 10:24 GMT
else if simple work, do hashtable on both type of apps (consistent
interface that we need).
[Probably Cache object of type Static-Singleton]
---
hB