Best Way to Know Code is in ASP.NET?

X

xenophon

I have a class that is used in an ASP.NET app, a WinForms app, and a
Win32 Service. What is the best way to tell what environment the code
is currently instanced in?

Thanks.
 
E

Eliyahu Goldin

Check static property System.Web.HttpContext.Current. If it is null, you are
not in Asp.Net.

Eliyahu
 
A

Alvin Bruney - ASP.NET MVP

Check static property System.Web.HttpContext.Current. If it is null, you
are
not in Asp.Net.

hmmm, that's risky.

Context.Current evaluating to null only means that there is no current
context. There may well have been a context object a few milliseconds
before, or during page processing, but after the page is processed and sent
to the client the current context will not be available anymore. In this
case, it most definitely does not mean that the page is not running as a web
application.
An alternative approach would be to amend the class construct to take an
identifier indicating who is making the call.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
S

Steven Cheng[MSFT]

Hi Xenophon,

As Alvin has mentioend, using HttpContext.Current may not be 100%
accurate, however, if your class component is only used in asp.net
application during the request's serverside pipeline, that should be ok.
In addition, there're also some other means such as use the current
executing process's ProcessName( asp.net worker process should always be
aspnet_wp or w3wp...)

Also, since asp.net web application's configuration file is always
web.config, this is also a possbile approach to check(though not 100%
accurate either...) , e.g:

Response.Write("<br/>" +
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

Hope also helps.Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Best Way to Know Code is in ASP.NET?
| Date: Wed, 9 Nov 2005 15:23:42 -0500
| Lines: 52
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.326
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.326
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 71.174.110-65.q9.net 65.110.174.71
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:356786
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| > Check static property System.Web.HttpContext.Current. If it is null, you
| are
| > not in Asp.Net.
|
| hmmm, that's risky.
|
| Context.Current evaluating to null only means that there is no current
| context. There may well have been a context object a few milliseconds
| before, or during page processing, but after the page is processed and
sent
| to the client the current context will not be available anymore. In this
| case, it most definitely does not mean that the page is not running as a
web
| application.
|
| > > I have a class that is used in an ASP.NET app, a WinForms app, and a
| > > Win32 Service. What is the best way to tell what environment the code
| > > is currently instanced in?
| An alternative approach would be to amend the class construct to take an
| identifier indicating who is making the call.
|
| --
| Regards,
| Alvin Bruney [MVP ASP.NET]
|
| [Shameless Author plug]
| The Microsoft Office Web Components Black Book with .NET
| Now Available @ www.lulu.com/owc
| Forth-coming VSTO.NET - Wrox/Wiley 2006
| -------------------------------------------------------
|
|
|
| | > Check static property System.Web.HttpContext.Current. If it is null, you
| are
| > not in Asp.Net.
| >
| > Eliyahu
| >
| > | > >
| > > I have a class that is used in an ASP.NET app, a WinForms app, and a
| > > Win32 Service. What is the best way to tell what environment the code
| > > is currently instanced in?
| > >
| > > Thanks.
| > >
| >
| >
|
|
|
 
S

Steven Cheng[MSFT]

Hi Xenophon,

How are you doing on this question? Does our suggestions help you a little?
If there're anything unclear or need any further assistance, please feel
free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 97818272
| References: <[email protected]>
<[email protected]>
<[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Thu, 10 Nov 2005 03:51:18 GMT
| Subject: Re: Best Way to Know Code is in ASP.NET?
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 87
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:356911
| NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
|
| Hi Xenophon,
|
| As Alvin has mentioend, using HttpContext.Current may not be 100%
| accurate, however, if your class component is only used in asp.net
| application during the request's serverside pipeline, that should be ok.
| In addition, there're also some other means such as use the current
| executing process's ProcessName( asp.net worker process should always be
| aspnet_wp or w3wp...)
|
| Also, since asp.net web application's configuration file is always
| web.config, this is also a possbile approach to check(though not 100%
| accurate either...) , e.g:
|
| Response.Write("<br/>" +
| AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
|
| Hope also helps.Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
| --------------------
| | From: "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc>
| | References: <[email protected]>
| <[email protected]>
| | Subject: Re: Best Way to Know Code is in ASP.NET?
| | Date: Wed, 9 Nov 2005 15:23:42 -0500
| | Lines: 52
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.3790.326
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.326
| | Message-ID: <[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | NNTP-Posting-Host: 71.174.110-65.q9.net 65.110.174.71
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.framework.aspnet:356786
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | > Check static property System.Web.HttpContext.Current. If it is null,
you
| | are
| | > not in Asp.Net.
| |
| | hmmm, that's risky.
| |
| | Context.Current evaluating to null only means that there is no current
| | context. There may well have been a context object a few milliseconds
| | before, or during page processing, but after the page is processed and
| sent
| | to the client the current context will not be available anymore. In this
| | case, it most definitely does not mean that the page is not running as
a
| web
| | application.
| |
| | > > I have a class that is used in an ASP.NET app, a WinForms app, and a
| | > > Win32 Service. What is the best way to tell what environment the
code
| | > > is currently instanced in?
| | An alternative approach would be to amend the class construct to take an
| | identifier indicating who is making the call.
| |
| | --
| | Regards,
| | Alvin Bruney [MVP ASP.NET]
| |
| | [Shameless Author plug]
| | The Microsoft Office Web Components Black Book with .NET
| | Now Available @ www.lulu.com/owc
| | Forth-coming VSTO.NET - Wrox/Wiley 2006
| | -------------------------------------------------------
| |
| |
| |
| | | | > Check static property System.Web.HttpContext.Current. If it is null,
you
| | are
| | > not in Asp.Net.
| | >
| | > Eliyahu
| | >
| | > | | > >
| | > > I have a class that is used in an ASP.NET app, a WinForms app, and a
| | > > Win32 Service. What is the best way to tell what environment the
code
| | > > is currently instanced in?
| | > >
| | > > Thanks.
| | > >
| | >
| | >
| |
| |
| |
|
|
 

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

Top