471,317 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Find out if classis used in asp.net or windows app

Hi,

I need to be able to find out if the assembly is currently used by an
asp.net app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey
Dec 13 '05 #1
7 926
Muzzy,

Check the static Current property on the HttpContext class. If it
returns true, then you are in ASP.NET, otherwise, you are not.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MuZZy" <tn*@newsgroups.nospam> wrote in message
news:uo**************@TK2MSFTNGP14.phx.gbl...
Hi,

I need to be able to find out if the assembly is currently used by an
asp.net app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey

Dec 13 '05 #2
> Hi,

I need to be able to find out if the assembly is currently used by an asp.net
app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey


If it is used in an asp.net app, then usually HttpContext.Current is
not null. This check is not 100% perfect: if the current call is
started by the timer, or too early in the response chain, then there is
no Context (yet) so you will get a null although it really *is* a
web-app.

Hans Kesting
Dec 13 '05 #3
Hans Kesting wrote:
Hi,

I need to be able to find out if the assembly is currently used by an
asp.net app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net
app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey


If it is used in an asp.net app, then usually HttpContext.Current is not
null. This check is not 100% perfect: if the current call is started by
the timer, or too early in the response chain, then there is no Context
(yet) so you will get a null although it really *is* a web-app.

Hans Kesting


So is there any 100% reliable way to find that out?
Dec 13 '05 #4
Muzzy,

Use the Current property on the HttpContext class. The conditions that
Hans is referring to are conditions that are most likely not going to come
up. First, you shouldn't be using timers in an ASP.NET app, and even if you
were, the callback is not going to have a context associated with it, since
the code is not running in response to a request. The second situation is
too early in the call chain, but quite honestly, I don't know where Hans is
indicating this can be. The highest up in the call chain that can be
customized is through the IHttpHandler interface, and at that point, the
HttpContext is passed to you.

It's going to work 99.9999999% of the time.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MuZZy" <tn*@newsgroups.nospam> wrote in message
news:uq*************@TK2MSFTNGP12.phx.gbl...
Hans Kesting wrote:
Hi,

I need to be able to find out if the assembly is currently used by an
asp.net app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net
app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey


If it is used in an asp.net app, then usually HttpContext.Current is not
null. This check is not 100% perfect: if the current call is started by
the timer, or too early in the response chain, then there is no Context
(yet) so you will get a null although it really *is* a web-app.

Hans Kesting


So is there any 100% reliable way to find that out?

Dec 13 '05 #5
Nicholas Paldino [.NET/C# MVP] wrote:
Muzzy,

Use the Current property on the HttpContext class. The conditions that
Hans is referring to are conditions that are most likely not going to come
up. First, you shouldn't be using timers in an ASP.NET app, and even if you
were, the callback is not going to have a context associated with it, since
the code is not running in response to a request. The second situation is
too early in the call chain, but quite honestly, I don't know where Hans is
indicating this can be. The highest up in the call chain that can be
customized is through the IHttpHandler interface, and at that point, the
HttpContext is passed to you.

It's going to work 99.9999999% of the time.

Thank you, Nicholas!
Dec 13 '05 #6
MuZZy wrote:
Hi,

I need to be able to find out if the assembly is currently used by an
asp.net app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey


Ok, thank you all guys! I'm going to be using your suggestion.
Dec 13 '05 #7
"Nicholas Paldino [.NET/C# MVP]" wrote:
Muzzy,

Use the Current property on the HttpContext class. The conditions that
Hans is referring to are conditions that are most likely not going to come
up. First, you shouldn't be using timers in an ASP.NET app, and even if you
were, the callback is not going to have a context associated with it, since
the code is not running in response to a request. The second situation is
too early in the call chain, but quite honestly, I don't know where Hans is
indicating this can be. The highest up in the call chain that can be
customized is through the IHttpHandler interface, and at that point, the
HttpContext is passed to you.
or IHttpModule, but even then you should have the Context I believe. plus
that's probably not where you want to use your object factory anyway.

and in addition to the timer, you won't have HttpContext any time you
execute on a thread other than the thread handling the incoming request,
including shared thread pool threads.

It's going to work 99.9999999% of the time.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MuZZy" <tn*@newsgroups.nospam> wrote in message
news:uq*************@TK2MSFTNGP12.phx.gbl...
Hans Kesting wrote:
Hi,

I need to be able to find out if the assembly is currently used by an
asp.net app or a windows app.
Assembly contains a class which has an object factory, but this object
factory should throw an exception if used in asp.net app.

//-------------
public class CLogin
{
public static string m_sUser;
public static string m_sServer;
public static string m_sDatabase;
CLogin(sUser, sServer, sDatabase)
{
m_sUser = sUser;
m_sServer = sServer;
m_sDatabase = sDatabase;
}
}
//------------
public class CConnection
{
public CConnection Create()
{
if (!SomeCheckIfCalledFromWindowsApp())
throw new Exception("Do not use this factory in asp.net
app!");

return new CConnection(CLogin.m_sUser, CLogin.m_sServer,
CLogin.m_sDatabase);
}
}
//-------------

Any ideas would be appreciated!

Thank you,
Andrey

If it is used in an asp.net app, then usually HttpContext.Current is not
null. This check is not 100% perfect: if the current call is started by
the timer, or too early in the response chain, then there is no Context
(yet) so you will get a null although it really *is* a web-app.

Hans Kesting


So is there any 100% reliable way to find that out?


Dec 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by SoftComplete Development | last post: by
3 posts views Thread by Professor Frink | last post: by
3 posts views Thread by Poewood | last post: by
5 posts views Thread by Mike Labosh | last post: by

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.