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

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

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
 
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.
 
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
 
Hans said:
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?
 
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 said:
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!
 
MuZZy said:
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.
 
Nicholas Paldino said:
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]
- (e-mail address removed)

MuZZy said:
So is there any 100% reliable way to find that out?
 
Back
Top