How to get global.asax build result or a new application instance?

  • Thread starter Thread starter Marvin Landman
  • Start date Start date
M

Marvin Landman

Hi,

I have no active HttpContext but I need an application instance. I've
tried BuildManager.GetCompiledType without success.

On http://support.microsoft.com/kb/328534 I've found
HttpApplicationFactory.GetApplicationInstance internal method that works
for me but I would prefer a public method if there is one.

Thank you.

Regards,
Marvin
 
Is this what you're looking for?
System.Web.HttpContext context = System.Web.HttpContext.Current;
 
Paul said:
Is this what you're looking for?
System.Web.HttpContext context = System.Web.HttpContext.Current;

No. I need an object derived from HttpApplication but I would like to
use the one defined in global.asax.
 
Is this what you're looking for?
No. I need an object derived from HttpApplication but I would like to use
the one defined in global.asax.

So System.Web.HttpContext.Current.ApplicationInstance ?

Else you may want to give some details about the context ;-) so that we can
understand what you need. For now I assume you want to get at the
application instance for the current request at a place that doesn't expose
explicitely this information (that likely from a class library rather than
from a page).
 
Patrice said:
So System.Web.HttpContext.Current.ApplicationInstance ?

Else you may want to give some details about the context ;-) so that we can
understand what you need. For now I assume you want to get at the
application instance for the current request at a place that doesn't expose
explicitely this information (that likely from a class library rather than
from a page).

Thank you for your response.

This would work but I would like to do the reverse. Create a HttpContext
and a HttpApplication rather than obtain an existing one.

Note that both of them is possible, I was able to use them but my
problem is that global.asax can define an application class derived from
HttpApplication that is used by ASP.NET in that case instead of a
generic HttpApplication.

So my question is that am I able to instantiate or obtain the type of
the class defined in global.asax without having a reference to an
application instance? (If I had an instance I could use GetType and
Activator.)

Marvin
 
Hi Marvin,

What my understanding of your requirement is that, you would like to get
the instance when the Application is started by the HttpApplicationFactory
instead of the instance of the current application. We know that when the
first request for the application arrives, the factory class extracts
information about the type of the application (the global.asax class),
creates the application state and fires the Application_OnStart event. The
factory selects a HttpApplication instance from the pool (like your said, a
internal method HttpApplicationFactory.GetApplicationInstance) and assigns
it to process the request. If no objects are available, a new
HttpApplication object is created. So, the requirement is seemed like how
to control the HttpApplicationFactory to select the different application
instances(maybe built by ourselves) for the request from the same
process(the pool).

Please correct me if I'm misunderstanding you.

--
Sincerely,

Zhi-Qiang Ni

Microsoft Online Support


==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================

--------------------
| Date: Wed, 18 Nov 2009 10:59:57 +0100
| From: Marvin Landman <[email protected]>
| User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
| MIME-Version: 1.0
| Subject: Re: How to get global.asax build result or a new application
instance?
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<#[email protected]>
| In-Reply-To: <#[email protected]>
| Content-Type: text/plain; charset=windows-1252; format=flowed
| Content-Transfer-Encoding: 7bit
| Message-ID: <e#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: catv-80-98-131-226.catv.broadband.hu 80.98.131.226
| Lines: 1
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP05.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:94343
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Patrice wrote:
| >>> Is this what you're looking for?
| >>> System.Web.HttpContext context = System.Web.HttpContext.Current;
| >> No. I need an object derived from HttpApplication but I would like to
use
| >> the one defined in global.asax.
| >
| > So System.Web.HttpContext.Current.ApplicationInstance ?
| >
| > Else you may want to give some details about the context ;-) so that we
can
| > understand what you need. For now I assume you want to get at the
| > application instance for the current request at a place that doesn't
expose
| > explicitely this information (that likely from a class library rather
than
| > from a page).
|
| Thank you for your response.
|
| This would work but I would like to do the reverse. Create a HttpContext
| and a HttpApplication rather than obtain an existing one.
|
| Note that both of them is possible, I was able to use them but my
| problem is that global.asax can define an application class derived from
| HttpApplication that is used by ASP.NET in that case instead of a
| generic HttpApplication.
|
| So my question is that am I able to instantiate or obtain the type of
| the class defined in global.asax without having a reference to an
| application instance? (If I had an instance I could use GetType and
| Activator.)
|
| Marvin
|
 
Is this to run unit tests ?
This would work but I would like to do the reverse. Create a HttpContext
and a HttpApplication rather than obtain an existing one.

By defintion the httpContext is the context of the current web request. I'm
not sure how (or why) you could create this if you don't have a current
request. The same goes for the app. It is created as needed when a first
request hit the web site.
Note that both of them is possible, I was able to use them but my problem
is that global.asax can define an application class derived from
HttpApplication that is used by ASP.NET in that case instead of a generic
HttpApplication.

It doesn't look the same thing. You can have global.asax to use a type
derived from httpApplication and you are able to get at this instance IMO by
just getting at HttpContext.CurrentApplicationInstance. You have just to
cast to the appropriate type.
So my question is that am I able to instantiate or obtain the type of the
class defined in global.asax without having a reference to an application
instance? (If I had an instance I could use GetType and Activator.)

It's still a bit unclear if the problem is in gettting the type you want
(AFAIK you'll just need to cast) or in really instanciating the object
without any real web request (for testing ?)

What is your preferred language (C# or VB ?) in case I would have a small
code sample to show that uses an HttpApplication derived class and that
obtain this same class in another context so that we can start from concrete
code to understand your rerquirements...
 
Thank you very much for your response.

HttpApplicationFactory.GetApplicationInstance works but it is an
internal method and I wonder if there is a more proper way of getting an
application instance or the type defined in global.asax.

But I am also interested in making HttpApplicationFactory to select a
different application than the one declared in global.asax (i.e. a
custom one) if that's possible.

Marving

Zhi-Qiang Ni[MSFT] rote:
 
Hi Marvin,

We need to confirm your requirement firstly.

After the request is processed into the web application, the current
HttpApplication instance would be created or selected by the
HttpApplicaitonFactory automatically, we cannot control this action. The
current HttpApplication is unique for the web application. Although we can
create a HttpContext and HttpApplication in the global.asax, it is
recommended that we define some properties in current HttpContext and
access them, but it is impossible to replace current instance with the
custom one. That means there is no public function that can get the custom
HttpApplication instance defined in the global.asax.

However, if you want to run Unit-Test or run the web application from other
programs (for example, a win form program). We can define custom
HttpContext firstly and then process request ourselves. Thus, the web
application will use the custom HttpContext as its current instance.

For example:
Request.Context =CustomContext;
HttpRuntime.ProcessRequest(Request);

--
Sincerely,

Zhi-Qiang Ni

Microsoft Online Support


==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================

--------------------
| Date: Fri, 20 Nov 2009 14:57:47 +0100
| From: Marvin Landman <[email protected]>
| User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
| MIME-Version: 1.0
| Subject: Re: How to get global.asax build result or a new application
instance?
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<#[email protected]>
<e#[email protected]>
<[email protected]>
| In-Reply-To: <[email protected]>
| Content-Type: text/plain; charset=windows-1252; format=flowed
| Content-Transfer-Encoding: 7bit
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: catv-80-98-131-226.catv.broadband.hu 80.98.131.226
| Lines: 1
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP05.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:94431
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thank you very much for your response.
|
| HttpApplicationFactory.GetApplicationInstance works but it is an
| internal method and I wonder if there is a more proper way of getting an
| application instance or the type defined in global.asax.
|
| But I am also interested in making HttpApplicationFactory to select a
| different application than the one declared in global.asax (i.e. a
| custom one) if that's possible.
|
| Marving
|
| Zhi-Qiang Ni[MSFT] rote:
| > Hi Marvin,
| >
| > What my understanding of your requirement is that, you would like to
get
| > the instance when the Application is started by the
HttpApplicationFactory
| > instead of the instance of the current application. We know that when
the
| > first request for the application arrives, the factory class extracts
| > information about the type of the application (the global.asax class),
| > creates the application state and fires the Application_OnStart event.
The
| > factory selects a HttpApplication instance from the pool (like your
said, a
| > internal method HttpApplicationFactory.GetApplicationInstance) and
assigns
| > it to process the request. If no objects are available, a new
| > HttpApplication object is created. So, the requirement is seemed like
how
| > to control the HttpApplicationFactory to select the different
application
| > instances(maybe built by ourselves) for the request from the same
| > process(the pool).
| >
| > Please correct me if I'm misunderstanding you.
| >
|
 
Thank you all very much for your help.
there is no public function that can get the custom
HttpApplication instance defined in the global.asax

This was what I was looking for but now that you have confirmed that
this is not possible, I'll use HttpRuntime.ProcessRequest instead of
taking a shortcut to the custom HttpApplication instance.

My issue is now resolved.

Marvin
 
Back
Top