how to detect if an assembly is running in winform/webform?

  • Thread starter Thread starter Paolo Liverani
  • Start date Start date
P

Paolo Liverani

I am trying to create an assembly with general purpose function to use in
all of my projects.
One function should be called when an error is catch so that it can log the
error, send an email and pop-up a messagebox, but messagebox does nto work
in web form.
Is there any function that allows to check if an assembly is running in
webform and is there something similar to the messagebox for webform (I know
there messagebox in jscript but I would like to know if there is a better
solution).
Thank you,
Paolo Liverani
 
The simplest way is to walk the call stack and look for namespace
"System.Web" and so on. This is not foolproof however as it does not detect
the case where users have spun off worker threads that use your assembly. A
more robust solution would probably have to examine other threads, etc.

-Steve
 
Hi Paolo,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know whether an assembly is
being called by winform or webform projects. If there is any
misunderstanding, please feel free to let me know.

Stephen has provided us with a good solution by checking the call stack.
However, some classes in the System.Web namespace can be used by winform
applications. So I think we can judge whether an assembly is runnign in
winform/webform by checking the current process. Generally, ASP.NET
applictions are running under aspnet_wp.exe in IIS5 and w3wp.exe in IIS6.
So we can achieve this by checking the process name of the current process.
Please try the following code.

public static string WinOrWeb()
{
string strPN = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if(strPN == "w3wp" || strPN == "aspnet_wp")
return "WebForm";
else
return "WinForm";
}

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
I Kevin and Stephen,
I would like to thank both of you for your suggestions.
I think that I will use the one from Kevin.
-Paolo
 
Hi Paolo,

It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top