473,416 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Best Way to Know Code is in ASP.NET?


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.

Nov 19 '05 #1
4 1187
Check static property System.Web.HttpContext.Current. If it is null, you are
not in Asp.Net.

Eliyahu

"xenophon" <xe******@online.nospam> wrote in message
news:rs********************************@4ax.com...

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.

Nov 19 '05 #2
> 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
-------------------------------------------------------

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl... Check static property System.Web.HttpContext.Current. If it is null, you are not in Asp.Net.

Eliyahu

"xenophon" <xe******@online.nospam> wrote in message
news:rs********************************@4ax.com...

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.


Nov 19 '05 #3
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.Configura tionFile);

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: <rs********************************@4ax.com>
<u2**************@TK2MSFTNGP12.phx.gbl>
| 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: <e$**************@TK2MSFTNGP09.phx.gbl>
| 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!TK2MSFT NGP09.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
| -------------------------------------------------------
|
|
|
| "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
| news:u2**************@TK2MSFTNGP12.phx.gbl...
| > Check static property System.Web.HttpContext.Current. If it is null, you
| are
| > not in Asp.Net.
| >
| > Eliyahu
| >
| > "xenophon" <xe******@online.nospam> wrote in message
| > news:rs********************************@4ax.com...
| > >
| > > 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.
| > >
| >
| >
|
|
|

Nov 19 '05 #4
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: <rs********************************@4ax.com>
<u2**************@TK2MSFTNGP12.phx.gbl>
<e$**************@TK2MSFTNGP09.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (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: <rE**************@TK2MSFTNGXA02.phx.gbl>
| 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.Configura tionFile);
|
| 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: <rs********************************@4ax.com>
| <u2**************@TK2MSFTNGP12.phx.gbl>
| | 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: <e$**************@TK2MSFTNGP09.phx.gbl>
| | 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!TK2MSFT NGP09.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
| | -------------------------------------------------------
| |
| |
| |
| | "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
| | news:u2**************@TK2MSFTNGP12.phx.gbl...
| | > Check static property System.Web.HttpContext.Current. If it is null,
you
| | are
| | > not in Asp.Net.
| | >
| | > Eliyahu
| | >
| | > "xenophon" <xe******@online.nospam> wrote in message
| | > news:rs********************************@4ax.com...
| | > >
| | > > 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.
| | > >
| | >
| | >
| |
| |
| |
|
|

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
5
by: Andrew S. Giles | last post by:
I thought I would post here, as I am sure someone, somewhere has run into this problem, and might have a good solution for me. I am writing an applicaiton in C# that will accept data and then put...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
4
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
20
by: Joe | last post by:
Is any one charting packing considered to be the "best"? We've used ChartFX but wasn't too happy about the way data had to be populated along with some other issues which slip my mind right now and...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.