summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Hosting/AspNetHost.cs
blob: e4cdc9b12b016602c0487d1e9e68fd58e9af9a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//-----------------------------------------------------------------------
// <copyright file="AspNetHost.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Hosting {
	using System;
	using System.IO;
	using System.Net;
	using System.Threading;
	using System.Web;
	using System.Web.Hosting;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Test.OpenId;

	/// <summary>
	/// Hosts an ASP.NET web site for placing ASP.NET controls and services on
	/// for more complete end-to-end testing.
	/// </summary>
	internal class AspNetHost : MarshalByRefObject, IDisposable {
		private HttpHost httpHost;

		/// <summary>
		/// Initializes a new instance of the <see cref="AspNetHost"/> class.
		/// </summary>
		/// <remarks>
		/// DO NOT CALL DIRECTLY.  This is only here for ASP.NET to call.
		/// Call the static <see cref="AspNetHost.CreateHost"/> method instead.
		/// </remarks>
		[Obsolete("Use the CreateHost static method instead.")]
		public AspNetHost() {
			this.httpHost = HttpHost.CreateHost(this);
		}

		public Uri BaseUri {
			get { return this.httpHost.BaseUri; }
		}

		public static AspNetHost CreateHost(string webDirectory) {
			AspNetHost host = (AspNetHost)ApplicationHost.
				CreateApplicationHost(typeof(AspNetHost), "/", webDirectory);
			return host;
		}

		public string ProcessRequest(string url) {
			return this.httpHost.ProcessRequest(url);
		}

		public string ProcessRequest(string url, string body) {
			return this.httpHost.ProcessRequest(url, body);
		}

		public void BeginProcessRequest(HttpListenerContext context) {
			ThreadPool.QueueUserWorkItem(state => { ProcessRequest(context); });
		}

		public void ProcessRequest(HttpListenerContext context) {
			try {
				using (TextWriter tw = new StreamWriter(context.Response.OutputStream)) {
					HttpRuntime.ProcessRequest(new TestingWorkerRequest(context, tw));
				}
			} catch (Exception ex) {
				Logger.Http.Error("Exception in AspNetHost", ex);
				throw;
			}
		}

		public void CloseHttp() {
			this.httpHost.Dispose();
		}

		#region IDisposable Members

		public void Dispose() {
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected void Dispose(bool disposing) {
			if (disposing) {
				this.CloseHttp();
			}
		}

		#endregion
	}
}