How Can Library Code "know" it's hosting application type?

Discussion in 'ASP .Net' started by Smithers, Aug 7, 2007.

  1. Smithers

    Smithers Guest

    I'm writing a code library that needs to be reused between a Windows Forms
    application and and an ASP.NET Web application.

    The library needs to do a couple of things differently depending on the type
    of application the library has been loaded for.

    What is a reliable and non-hacking way for the code to make that
    determination (hosting application type)?

    Using .NET 3.5

    Thanks
     
    Smithers, Aug 7, 2007
    #1
    1. Advertising

  2. Smithers

    Marc Gravell Guest

    Well, ASP.NET often has an HttpContext.Current, but note that
    personally I find this a bit hacky. It also doesn't work for things
    like WCF hosted in IIS. I generally prefer to use a provider model,
    where-by those bits that depend on the architecture are abstracted
    through an interface (with some mechanism to register and obtain the
    provider); in the web app I register one provider (perhaps using an
    HttpModule to configure via web.config), and in the client I register
    another.

    But I guess it depends on how big the change is...

    Marc
     
    Marc Gravell, Aug 7, 2007
    #2
    1. Advertising

  3. Smithers,

    One way I know of is to set a reference to System.Web.dll and then check
    the static Current poperty of the HttpContext class. If this returns null,
    then there is no HttpContext, and no ASP.NET. Of course, this is only
    applicable if you are actually processing a page. Calling this outside of a
    request pipeline in ASP.NET will still return null, so if you need this for
    one-time initialization, you might have to look for another option.


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

    "Smithers" <> wrote in message
    news:...
    > I'm writing a code library that needs to be reused between a Windows Forms
    > application and and an ASP.NET Web application.
    >
    > The library needs to do a couple of things differently depending on the
    > type of application the library has been loaded for.
    >
    > What is a reliable and non-hacking way for the code to make that
    > determination (hosting application type)?
    >
    > Using .NET 3.5
    >
    > Thanks
    >
     
    Nicholas Paldino [.NET/C# MVP], Aug 7, 2007
    #3
  4. Smithers

    Ralph Guest

    "Smithers" <> wrote in message
    news:...
    > I'm writing a code library that needs to be reused between a Windows Forms
    > application and and an ASP.NET Web application.
    >
    > The library needs to do a couple of things differently depending on the

    type
    > of application the library has been loaded for.
    >


    You need to revisit your design. Those "couple of things" likely either
    don't belong in the library or need to be "passed" by the client at the time
    of use.

    -ralph
     
    Ralph, Aug 7, 2007
    #4
  5. Smithers

    Brian Guest

    In the past I had always checked the current process image name. If it was
    "w3wp.exe" then I knew it was running under IIS. I suggest you also check
    against "WebDev.WebServer.Exe" since this is what VS 2005 uses during
    development. If I find either of these 2 I assume I'm running in a web
    application - otherwise assume WinForms.

    - Brian
    "Smithers" <> wrote in message
    news:...
    > I'm writing a code library that needs to be reused between a Windows Forms
    > application and and an ASP.NET Web application.
    >
    > The library needs to do a couple of things differently depending on the
    > type of application the library has been loaded for.
    >
    > What is a reliable and non-hacking way for the code to make that
    > determination (hosting application type)?
    >
    > Using .NET 3.5
    >
    > Thanks
    >
     
    Brian, Aug 7, 2007
    #5
  6. It's also possible to run ASP.NET under Apache.

    http://weblogs.asp.net/israelio/archive/2005/09/11/424852.aspx

    Although much less common. :)

    Sam

    ------------------------------------------------------------
    We're hiring! B-Line Medical is seeking .NET
    Developers for exciting positions in medical product
    development in MD/DC. Work with a variety of technologies
    in a relaxed team environment. See ads on Dice.com.



    On Tue, 7 Aug 2007 11:09:33 -0500, "Brian" <>
    wrote:

    >In the past I had always checked the current process image name. If it was
    >"w3wp.exe" then I knew it was running under IIS. I suggest you also check
    >against "WebDev.WebServer.Exe" since this is what VS 2005 uses during
    >development. If I find either of these 2 I assume I'm running in a web
    >application - otherwise assume WinForms.
    >
     
    Samuel R. Neff, Aug 7, 2007
    #6
    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. Remi Delon
    Replies:
    0
    Views:
    814
    Remi Delon
    Jan 19, 2005
  2. Aravapalli

    Web Hosting - reseller hosting

    Aravapalli, Dec 20, 2007, in forum: HTML
    Replies:
    8
    Views:
    501
    Chaddy2222
    Dec 21, 2007
  3. Replies:
    2
    Views:
    120
  4. teo1991
    Replies:
    0
    Views:
    599
    teo1991
    Apr 2, 2009
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page