How to detect that my library is being used under ASP.NET

S

Stephen Walch

I have a managed library that needs to behave differently when used in an
ASP.NET web page as apposed to a WinForms app. What is the most efficient
way to detect this at run time? (Bonus question: is there a way to detect
this at design time?)

Thanks,
Steve
 
N

Nick

I believe you can find out details about the process you are loaded into.
Might be worth looking in the System.Diagnostics namespace. I am curious,
why does your library need to behave differently depending on the host?
 
T

Teemu Keiski

Hi,
First way that comes to my mind is to check if web context exists. That
would be something like:

If Not System.Web.HttpContext.Current Is Nothing Then
...
End If

Downside is that this requires reference to System.Web assembly.

Another would be checking what Environment.UserName returns, though this of
course ain't 100% reliable on cases when system is configured from defaults.
with default settings this returns ASPNET in Win2k , and probably
NETWORK_SERVICE with Win2k3 Server, but of course if process is configured
for another user, this isn't reliable. With WinForm this would return the
user that started the application.

Third could be as was said, checking that in which process app is running:

Dim proc As System.Diagnostics.Process =
System.Diagnostics.Process.GetCurrentProcess()

proc.ProcessName would return name of the process the app runs under. With
ASP.NEt on Win2k machine it would be aspnet_wp. On Win2k3 it would be the
same IIS's process by default.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top