Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Smithers - 07 Aug 2007 15:42 GMT
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
Marc Gravell - 07 Aug 2007 15:49 GMT
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
Nicholas Paldino [.NET/C# MVP] - 07 Aug 2007 15:51 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> I'm writing a code library that needs to be reused between a Windows Forms
> application and and an ASP.NET Web application.
[quoted text clipped - 8 lines]
>
> Thanks
Ralph - 07 Aug 2007 16:00 GMT
> 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
Brian - 07 Aug 2007 17:09 GMT
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
> I'm writing a code library that needs to be reused between a Windows Forms
> application and and an ASP.NET Web application.
[quoted text clipped - 8 lines]
>
> Thanks
Samuel R. Neff - 07 Aug 2007 21:59 GMT
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.

>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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.