There's a lot of exposition here - but it's needed.
Not sure how many people are aware of this, but, the Razor code editor in Visual Studio causes your website to be 'test-fired' up to just before the Application_Start
event - and this is causing some annoying issues in my current project, which uses WebActivator to do a lot of the site initialisation.
Update - On closer inspection it's not just Razor - it looks like it's Visual Studio-wide - hence the change of title
I need to be able to detect when website code is being run by Visual Studio and not by a web server.
To demonstrate - do the following (exactly as written to ensure it reproduces):
RazorPageBugTest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication6.App_Start;
using System.IO;
[assembly: WebActivator.PreApplicationStartMethod(
typeof(RazorPageBugTest), "Start", Order = 0)]
namespace MvcApplication6.App_Start
{
public static class RazorPageBugTest
{
public static void Start()
{
using (var writer = File.Open(@"c:\trace.txt", FileMode.Create))
{
using (var tw = new StreamWriter(writer))
{
tw.AutoFlush = true;
tw.WriteLine("Written at {0}", DateTime.Now);
}
}
}
}
}
Notice that this code is not something that would usually work on a web server anyway - given that it's writing to drive C: (indeed this might not work on your machine if you don't run VS as administrator).
So that demonstrates the problem here - the Razor code editor is firing up an AppDomain and, effectively, shelling the website in order to get intellisense (including things like stuff in the App_Helpers folder etc). Now it doesn't actually trigger the Application_Start
method - but when you have WebActivator in the project as well, any of its pre-application-start methods are triggered.
In my case this is causing huge issues - high CPU usage and memory usage in devenv.exe
(equivalent to the website having just been started) because I initialise DI containers and god-knows what else at this point, but one in particular is proving really annoying.
My website has a component that listens on the network for status and cache-invalidation messages from other machines in the web farm. In QA and Live environments this listener never fails to open the port and listen - on my dev machine, however, it frequently fails - saying the port's already in use. When I look for the process that's holding it open - it's always devenv.exe
.
You guessed it - I start this listener in a WebActivator-initialised boot-strapping call.
So the question is - does anyone know of a way to detect that the code isn't being run by a 'proper' web host so I can stop it being run? I'm particularly hopeful for this as it'll also mean that my Visual Studio and Razor editing experience will become a damn-site faster as a result!
As a bonus, any fix could legitimately be sent to David Ebbo - author of WebActivator - so he can stick it earlier in his stack to prevent the problem completely!
Update
I have just added an issue to the WebActivator page on GitHub to see if David Ebbo can push either my fix, or a better one, down into the WebActivator component.
Guess what the value of Process.GetCurrentProcess().ProcessName
will be in this case?
Answer: Microsoft.VisualStudio.Web.Host
.
So if there's a VisualStudio
in the name of the process, you could safely scrape it.
Also check this article
out about the hosting process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With