Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 66 lines (57 sloc) 2.703 kb
92787b3 @SteveSanderson Listener now pukes in release mode (precaution)
authored
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5 using System.Reflection;
6 using System.Web;
54bee14 @SteveSanderson Initial state
authored
7 using System.Runtime.Remoting;
8 using System.Runtime.Remoting.Channels;
9 using DeleporterCore.Configuration;
f9b9f30 @SteveSanderson Ensure that, even though IHttpModule.Init() can fire multiple times (one...
authored
10 using System.Threading;
54bee14 @SteveSanderson Initial state
authored
11
12 namespace DeleporterCore.Server
13 {
14 public class DeleporterServerModule : IHttpModule
15 {
16 private IChannel _remotingChannel;
f9b9f30 @SteveSanderson Ensure that, even though IHttpModule.Init() can fire multiple times (one...
authored
17 private static int _hasCreatedChannel = 0;
54bee14 @SteveSanderson Initial state
authored
18
92787b3 @SteveSanderson Listener now pukes in release mode (precaution)
authored
19 public void Init(HttpApplication app)
54bee14 @SteveSanderson Initial state
authored
20 {
92787b3 @SteveSanderson Listener now pukes in release mode (precaution)
authored
21 if (WasCompiledInDebugMode(app))
22 {
f9b9f30 @SteveSanderson Ensure that, even though IHttpModule.Init() can fire multiple times (one...
authored
23 if (Interlocked.Exchange(ref _hasCreatedChannel, 1) == 0)
24 {
25 // Start listening for connections
26 RemotingConfiguration.RegisterWellKnownServiceType(typeof (DeleporterService), NetworkConfiguration.ServiceName, WellKnownObjectMode.Singleton);
27 _remotingChannel = NetworkConfiguration.CreateChannel();
28 ChannelServices.RegisterChannel(_remotingChannel, false);
29 }
92787b3 @SteveSanderson Listener now pukes in release mode (precaution)
authored
30 } else
31 throw new InvalidOperationException("You should not enable Deleporter on production web servers. As a security precaution, Deleporter won't run if your ASP.NET application was compiled in Release mode. You need to remove DeleporterServerModule from your Web.config file. If you need to bypass this, the only way is to edit the Deleporter source code and remove this check.");
54bee14 @SteveSanderson Initial state
authored
32 }
33
92787b3 @SteveSanderson Listener now pukes in release mode (precaution)
authored
34 #region Checking for debug mode
35 private static bool WasCompiledInDebugMode(object value)
36 {
37 // In case the app class is auto-generated from a Global.asax file, check its base classes too, going down until we hit ASP.NET itself
38 var assembliesToCheck = GetInheritanceChain(value.GetType()).Select(x => x.Assembly).TakeWhile(x => x != typeof (HttpApplication).Assembly).Distinct();
39 return assembliesToCheck.All(AssemblyWasCompiledInDebugMode);
40 }
41
42 private static bool AssemblyWasCompiledInDebugMode(Assembly assembly)
43 {
44 var debuggableAttrib = assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().SingleOrDefault();
45 return debuggableAttrib != null && debuggableAttrib.IsJITTrackingEnabled;
46 }
47
48 private static IEnumerable<Type> GetInheritanceChain(Type type)
49 {
50 while (type != null) {
51 yield return type;
52 type = type.BaseType;
53 }
54 }
55 #endregion
56
54bee14 @SteveSanderson Initial state
authored
57 public void Dispose()
58 {
59 if (_remotingChannel != null)
60 ChannelServices.UnregisterChannel(_remotingChannel);
61 }
62 }
63 }
64
65
Something went wrong with that request. Please try again.