473,396 Members | 2,076 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,396 software developers and data experts.

How to determine if I am in a web or a windows application

Hello to all,

I use a lib class to handle all my exeption errors ocured in my objects.
Since my objects are used in windows applications as well as in web
applications, I would like to determine in my lib class if she is serving a
web or a windows application. Is there a neat way to detect this.

Greets

Jean.Paul
Sep 21 '06 #1
8 1406
Well, you could query System.Web.HttpContext.Current, but personally I tend
to design libraries to be ignorant of their caller, and just do their own
job. Is there something specific you want to do differently? As a different
design (such as passing in some kind of service interface) may be more
appropriate.

Marc
Sep 21 '06 #2
Marc,

Well, I have to tell my users what error they encountered, in a winform i
can use a MessageBox and in a webform I have to use a popup. Thats the mean
reason.
The data object have to live on there own without knowing where they are
used, just my exepton handler has to know where the object was used.

greets
jp

"Marc Gravell" <ma**********@gmail.comschreef in bericht
news:u3**************@TK2MSFTNGP03.phx.gbl...
Well, you could query System.Web.HttpContext.Current, but personally I
tend to design libraries to be ignorant of their caller, and just do their
own job. Is there something specific you want to do differently? As a
different design (such as passing in some kind of service interface) may
be more appropriate.

Marc

Sep 21 '06 #3
"Jean Paul Mertens" <ON****@newsgroups.nospamschrieb:
I use a lib class to handle all my exeption errors ocured in my objects.
Since my objects are used in windows applications as well as in web
applications, I would like to determine in my lib class if she is serving
a web or a windows application. Is there a neat way to detect this.
Check out 'System.Reflection.Assembly.GetEntryAssembly'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 21 '06 #4
In most scenarios this will do:

/// <value>Indicates that the application is running in ASP.NET</summary>

public static bool IsWeb

{

get {return System.Web.HttpContext.Current != null;}

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
Hello to all,

I use a lib class to handle all my exeption errors ocured in my objects.
Since my objects are used in windows applications as well as in web
applications, I would like to determine in my lib class if she is serving
a web or a windows application. Is there a neat way to detect this.

Greets

Jean.Paul


Sep 21 '06 #5
In my view, library functions (except for explicit UI libraries such as
System.Windows.Forms) should never attempt to communicate with the user, and
the need to do so is a design confusion. Especially via MessageBox. The
calling UI itself should handle such things.

Reasons?
1: it makes the code hard to re-use and unreliable. Think of a service: it
isn't a web-app, but there is no user to talk to.
2: it can interrupt transactions causing blocking - e.g. if I create a
transaction, and then something goes wrong causing an error to be logged,
and *that* in turn does a MessageBox, and I have gone to get a coffee, then
the system could be leaving an open transaction, as until MessageBox
completes the code can't get back to the end of my transaction
3: it just makes too many assumptions about the environment, threading
model, etc

My advice: log it as you are, but then re-throw the exception all the way to
the UI, and simply have the UI catch this error and display accordingly.
This way, all "finally" and "using" blocks will have "done their thing"
*before* we start talking to the user.

If you *must* do this (perhaps to seek confirmation), then a service
interface may be helpful - but even this smacks of not passing enough
information to the method to let it do its job (or fail) completely;
thinking on the fly:

public interface IErrorService {
bool SupportsMessaging {get;}
MessageResult Message(....); // think "DialogResult", but without winforms
dependency
}

Then your WinForm and Web app can provide an IErrorService instance (with
the web not supporting messages, and the win supporting it and implementing
via MessageBox.Show; you could even use the IServiceProvider approach,
perhaps as a settable property on a static class, to avoid having to pass
around lots of services.

Marc
Sep 21 '06 #6
Marc,

Indeed, I let mij lib log the exeption and I retrow it to my UI with the
correct errormessage combining all possible errors that ocured. In the lib I
put the different ways to display it to my users in different static
functions. If I then use my dataobject I can decide in the upper-application
what way to use to prevent the user from what is going on.

tnx to all,

Jean Paul

"Marc Gravell" <ma**********@gmail.comschreef in bericht
news:OR**************@TK2MSFTNGP03.phx.gbl...
In my view, library functions (except for explicit UI libraries such as
System.Windows.Forms) should never attempt to communicate with the user,
and the need to do so is a design confusion. Especially via MessageBox.
The calling UI itself should handle such things.

Reasons?
1: it makes the code hard to re-use and unreliable. Think of a service: it
isn't a web-app, but there is no user to talk to.
2: it can interrupt transactions causing blocking - e.g. if I create a
transaction, and then something goes wrong causing an error to be logged,
and *that* in turn does a MessageBox, and I have gone to get a coffee,
then the system could be leaving an open transaction, as until MessageBox
completes the code can't get back to the end of my transaction
3: it just makes too many assumptions about the environment, threading
model, etc

My advice: log it as you are, but then re-throw the exception all the way
to the UI, and simply have the UI catch this error and display
accordingly. This way, all "finally" and "using" blocks will have "done
their thing" *before* we start talking to the user.

If you *must* do this (perhaps to seek confirmation), then a service
interface may be helpful - but even this smacks of not passing enough
information to the method to let it do its job (or fail) completely;
thinking on the fly:

public interface IErrorService {
bool SupportsMessaging {get;}
MessageResult Message(....); // think "DialogResult", but without
winforms dependency
}

Then your WinForm and Web app can provide an IErrorService instance (with
the web not supporting messages, and the win supporting it and
implementing via MessageBox.Show; you could even use the IServiceProvider
approach, perhaps as a settable property on a static class, to avoid
having to pass around lots of services.

Marc


Sep 21 '06 #7
PS

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Marc,

Indeed, I let mij lib log the exeption and I retrow it to my UI with the
correct errormessage combining all possible errors that ocured. In the lib
I put the different ways to display it to my users in different static
functions. If I then use my dataobject I can decide in the
upper-application what way to use to prevent the user from what is going
on.
Seems like a code smell to me. You have one big exception handler object
that you are using a switch statement in. You should have a WinForms
exception handler and a WebApp exception handler. Only one of these
exception handlers will ever register to listen for exceptions and then do
it's stuff when needed.

PS

>
tnx to all,

Jean Paul

"Marc Gravell" <ma**********@gmail.comschreef in bericht
news:OR**************@TK2MSFTNGP03.phx.gbl...
>In my view, library functions (except for explicit UI libraries such as
System.Windows.Forms) should never attempt to communicate with the user,
and the need to do so is a design confusion. Especially via MessageBox.
The calling UI itself should handle such things.

Reasons?
1: it makes the code hard to re-use and unreliable. Think of a service:
it isn't a web-app, but there is no user to talk to.
2: it can interrupt transactions causing blocking - e.g. if I create a
transaction, and then something goes wrong causing an error to be logged,
and *that* in turn does a MessageBox, and I have gone to get a coffee,
then the system could be leaving an open transaction, as until MessageBox
completes the code can't get back to the end of my transaction
3: it just makes too many assumptions about the environment, threading
model, etc

My advice: log it as you are, but then re-throw the exception all the way
to the UI, and simply have the UI catch this error and display
accordingly. This way, all "finally" and "using" blocks will have "done
their thing" *before* we start talking to the user.

If you *must* do this (perhaps to seek confirmation), then a service
interface may be helpful - but even this smacks of not passing enough
information to the method to let it do its job (or fail) completely;
thinking on the fly:

public interface IErrorService {
bool SupportsMessaging {get;}
MessageResult Message(....); // think "DialogResult", but without
winforms dependency
}

Then your WinForm and Web app can provide an IErrorService instance (with
the web not supporting messages, and the win supporting it and
implementing via MessageBox.Show; you could even use the IServiceProvider
approach, perhaps as a settable property on a static class, to avoid
having to pass around lots of services.

Marc


Sep 21 '06 #8
I agree with this one. (aka, Eliyahu's)

I use this one fairly often.

Keep in mind which namespace/assembly its in.
PS

You normally dont' need to post to 4 or more groups.


"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:eG**************@TK2MSFTNGP03.phx.gbl...
In most scenarios this will do:

/// <value>Indicates that the application is running in ASP.NET</summary>

public static bool IsWeb

{

get {return System.Web.HttpContext.Current != null;}

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
Hello to all,

I use a lib class to handle all my exeption errors ocured in my objects.
Since my objects are used in windows applications as well as in web
applications, I would like to determine in my lib class if she is
serving
a web or a windows application. Is there a neat way to detect this.

Greets

Jean.Paul


Sep 22 '06 #9

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

Similar topics

3
by: Web Webon | last post by:
Hi everybody! I wonder if this is possible? I need to determine if a client is using "windows classic folders" or anything else. If I instantiate a Shell ActiveX object is there a way of...
19
by: Lauren Quantrell | last post by:
In Windows XP display Properties/Appearance users can chose between: Windows XP Style or Windows Classic Style. Is there a way in VBA to determine the user's selection? The reason: Toolbars...
3
by: Sisyphus | last post by:
Hi, Is there anything in the ANSI C standard covering the means by which I can determine the operating system and compiler being used (at the preprocess stage) ? If so, then how might I...
3
by: dje | last post by:
How do I determine if the current process is a Web Application or Windows Application? I've tried: System.Diagnostics.Process process=System.Diagnostics.Process.GetCurrentProcess(); but it...
6
by: Arsen V. | last post by:
Hello, I have a localization class that I want to use from either Web or Windows Forms apps. Currently it stores some information in the HttpRuntime.Cache object. I want to be able to determine...
4
by: Dave | last post by:
Greetings, I have a web application that will be hosted on our intranet. I would like to determine, via code the user's windows login name and domain in the following format: DOMAIN\loginname...
3
by: Dean Slindee | last post by:
I have a exception handling class that could be called from either a windows project app or a console project app. Is there any way for this class to determine which type of app called it without...
7
by: Doru Roman | last post by:
Hi, What is the fastest way to evaluate manually the result in this case: int a, b, c; a = 255; b = 122; c = a & b; The only way I know is transforming each number into the binary value...
4
by: Jean Paul Mertens | last post by:
Hello to all, I use a lib class to handle all my exeption errors ocured in my objects. Since my objects are used in windows applications as well as in web applications, I would like to determine...
8
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I want to determine the Windows color scheme that is being used by my app. This means that I need to determine the following: 1. Does the OS support themes? 2. Are themes currently enabled...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...

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.