[Mono-list] HttpRuntime initialised check?

Robert Jordan robertj at gmx.net
Tue Aug 14 19:29:51 EDT 2007


Hi,

James Fitzsimons wrote:
> Hi all,
> 
> I am doing a bit of work trying to get Spring.NET (specifically the web
> support) running on mono. I am using 1.2.5 preview 3 of the mono runtime
> at the moment.
> 
> The problem is that in the spring code they attempt to check if the
> HttpRuntime has been fully initialised. They do this in a rather nasty
> fashion that will only ever work on the MS .NET runtime. The question I
> have for this list is:
> 
> Is there a "correct" way to check if the HttpRuntime has finished it's
> initialisation?
> 
> From this http://msdn2.microsoft.com/en-us/library/aa479328.aspx
> 
> "The HttpRuntime object initializes a number of internal objects that
> will help carry the request out. Helper objects include the cache
> manager (the Cache object) and the internal file system monitor used to
> detect changes in the source files that form the application. The
> HttpRuntime creates the context for the request and fills it up with any
> HTTP information specific to the request. The context is represented by
> an instance of the HttpContext class."
> 
> it looks like you could just check if the HttpContext wasn't null. Would
> that be a sufficient check?

The context will be undefined when queried from outside of a
request, so you cannot rely on it as an initialization check.

> 
> Thanks for any advice you can provide!
> James Fitzsimons
> 
> PS: offending Spring.NET code here:


This looks to me like a workaround either for a bug in MS.NET
or for a design glitch in String.NET, which seems to expect
to be able to do things before the first request has started.

Try first to comment out the whole #if-block or extend
the expression to ignore Mono:

	if (Type.GetType ("Mono.Runtime") == null
		&& HttpRuntime.AppDomainAppVirtualPath != null)


Robert


> 
>  static WebApplicationContext()
>     {
>         // register for ContextRegistry.Cleared event - we need to
> discard our cache in this case
>         ContextRegistry.Cleared += new
> EventHandler(OnContextRegistryCleared);
> 
> #if NET_2_0
>         if (HttpRuntime.AppDomainAppVirtualPath != null) // check if
> we're within an ASP.NET AppDomain!
>         {
>             // ensure HttpRuntime has been fully initialized!
>             // this is a problem,.if ASP.NET Web Administration
> Application is used. This app does not fully set up the AppDomain...
>             HttpRuntime runtime =
>                 (HttpRuntime)
>                 typeof(HttpRuntime).GetField("_theRuntime",
> BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
> 
>             bool beforeFirstRequest = false;
>             lock (HttpRuntime)
>             {
>                 beforeFirstRequest =
>                     (bool)
>                     typeof(HttpRuntime).GetField("_beforeFirstRequest",
> BindingFlags.Instance | BindingFlags.NonPublic).
>                         GetValue(runtime);
>             }
>             log.Debug("BeforeFirstRequest:" + beforeFirstRequest);
>             if (beforeFirstRequest)
>             {
>                 try
>                 {
>                     string firstRequestPath =
> HttpRuntime.AppDomainAppVirtualPath.TrimEnd('/') + "/dummy.context";
>                     log.Info("Forcing first request " +
> firstRequestPath);
>                     HttpRuntime.ProcessRequest(
>                         new SimpleWorkerRequest(firstRequestPath,
> string.Empty, new StringWriter()));
>                     log.Info("Successfully processed first request!");
>                 }
>                 catch (Exception ex)
>                 {
>                     log.Error("Failed processing first request", ex);
>                     throw;
>                 }
>             }
>         }
> #endif
>     }
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list



More information about the Mono-list mailing list