473,806 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1208
Check static property System.Web.Http Context.Current . If it is null, you are
not in Asp.Net.

Eliyahu

"xenophon" <xe******@onlin e.nospam> wrote in message
news:rs******** *************** *********@4ax.c om...

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.Http Context.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.co m> wrote in message
news:u2******** ******@TK2MSFTN GP12.phx.gbl... Check static property System.Web.Http Context.Current . If it is null, you are not in Asp.Net.

Eliyahu

"xenophon" <xe******@onlin e.nospam> wrote in message
news:rs******** *************** *********@4ax.c om...

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.Cur rent 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.Curre ntDomain.SetupI nformation.Conf igurationFile);

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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: 71.174.110-65.q9.net 65.110.174.71
| Path: TK2MSFTNGXA02.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP09.phx. gbl
| Xref: TK2MSFTNGXA02.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:3567 86
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
|
| > Check static property System.Web.Http Context.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.co m> wrote in message
| news:u2******** ******@TK2MSFTN GP12.phx.gbl...
| > Check static property System.Web.Http Context.Current . If it is null, you
| are
| > not in Asp.Net.
| >
| > Eliyahu
| >
| > "xenophon" <xe******@onlin e.nospam> wrote in message
| > news:rs******** *************** *********@4ax.c om...
| > >
| > > 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.publi c.dotnet.framew ork.aspnet
| Message-ID: <rE************ **@TK2MSFTNGXA0 2.phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
| Lines: 87
| Path: TK2MSFTNGXA02.p hx.gbl
| Xref: TK2MSFTNGXA02.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:3569 11
| NNTP-Posting-Host: tomcatimport2.p hx.gbl 10.201.218.182
|
| Hi Xenophon,
|
| As Alvin has mentioend, using HttpContext.Cur rent 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.Curre ntDomain.SetupI nformation.Conf igurationFile);
|
| 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.publi c.dotnet.framew ork.aspnet
| | NNTP-Posting-Host: 71.174.110-65.q9.net 65.110.174.71
| | Path: TK2MSFTNGXA02.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP09.phx. gbl
| | Xref: TK2MSFTNGXA02.p hx.gbl
| microsoft.publi c.dotnet.framew ork.aspnet:3567 86
| | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
| |
| | > Check static property System.Web.Http Context.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.co m> wrote in message
| | news:u2******** ******@TK2MSFTN GP12.phx.gbl...
| | > Check static property System.Web.Http Context.Current . If it is null,
you
| | are
| | > not in Asp.Net.
| | >
| | > Eliyahu
| | >
| | > "xenophon" <xe******@onlin e.nospam> wrote in message
| | > news:rs******** *************** *********@4ax.c om...
| | > >
| | > > 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
9468
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
5
5058
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 it into an Excel spreadsheet. Easy, right? Well it is, until you have to get the data from another application that is written in Borland C++ PowerBuilder 5. The situation is that the Borland Code isnt going to get re-written (too expensive,...
10
3489
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 somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net 2005 created any AssemblyInfo.cs file.
4
1844
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 logged in, on each and every page, and redirects the user to a login page if s/he's not logged in. The login page will also take care of some standard setup, such as choosing/populating a user profile. I used to use <!-- #include ... --for this,...
16
2824
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
17
3050
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 else? The reason for this question is that I had an assignment in which I was
13
2513
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 experience. We employ some parts of XP such as pair programming, and this should help. Other than books, does anyone have any suggestions? His skill set is pretty antiquated and we need to get him up to speed as quickly as possible so any suggestions...
20
10057
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 Dundas has bugs and doesn't do a good enough job displaying axis labels and is very slow to paint large numbers of series and data points. We're currently evaluating ProEssentials which we are happy with but it's not a native .NET package. ...
41
2890
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 created and then added to "component" objects to build up the component definition. My dilemma comes in deciding how to read/write data to the "item"
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.