How to determine if context is Windows Forms or Web Forms

Discussion in 'ASP .Net' started by Arsen V., Aug 5, 2005.

  1. Arsen V.

    Arsen V. Guest

    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
    Arsen V., Aug 5, 2005
    #1
    1. Advertising

  2. 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
    >
    >
    Octavio Hernandez, Aug 5, 2005
    #2
    1. Advertising

  3. 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
    >
    >
    >
    =?Utf-8?B?QnJpYW4gRGVsYWh1bnR5?=, Aug 5, 2005
    #3
  4. 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
    >
    >
    Nicholas Paldino [.NET/C# MVP], Aug 5, 2005
    #4
  5. 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 [.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
    > >
    > >

    >
    >
    Jeremy Williams, Aug 5, 2005
    #5
  6. 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/default.asp?url=/library/en-us/dnpag2/html/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
    >> >
    >> >

    >>
    >>

    >
    >
    Nicholas Paldino [.NET/C# MVP], Aug 5, 2005
    #6
  7. Arsen V.

    Brock Allen Guest

    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
    Brock Allen, Aug 5, 2005
    #7
  8. Arsen V.

    hB Guest

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

    ---
    hB
    hB, Aug 6, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,798
    jenny
    May 25, 2004
  2. =?Utf-8?B?RGFubnkgTWlsbGVy?=

    determine ASP.NET worker process user context programatically?

    =?Utf-8?B?RGFubnkgTWlsbGVy?=, Sep 30, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,835
    Karl Seguin
    Oct 3, 2005
  3. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    701
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  4. Flip Rayner
    Replies:
    1
    Views:
    649
    bruce barker
    Jan 23, 2007
  5. Peña, Botp
    Replies:
    1
    Views:
    229
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page