forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppHostHttpListenerBase.cs
More file actions
83 lines (68 loc) · 3 KB
/
Copy pathAppHostHttpListenerBase.cs
File metadata and controls
83 lines (68 loc) · 3 KB
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
using System;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using ServiceStack.Host;
using ServiceStack.Host.Handlers;
using ServiceStack.Host.HttpListener;
namespace ServiceStack
{
/// <summary>
/// Inherit from this class if you want to host your web services inside a
/// Console Application, Windows Service, etc.
///
/// Usage of HttpListener allows you to host webservices on the same port (:80) as IIS
/// however it requires admin user privillages.
/// </summary>
public abstract class AppHostHttpListenerBase
: HttpListenerBase
{
public static int ThreadsPerProcessor = 16;
public static int CalculatePoolSize()
{
return Environment.ProcessorCount * ThreadsPerProcessor;
}
public string HandlerPath { get; set; }
protected AppHostHttpListenerBase(string serviceName, params Assembly[] assembliesWithServices)
: base(serviceName, assembliesWithServices) { }
protected AppHostHttpListenerBase(string serviceName, string handlerPath, params Assembly[] assembliesWithServices)
: base(serviceName, assembliesWithServices)
{
HandlerPath = handlerPath;
}
protected override Task ProcessRequestAsync(HttpListenerContext context)
{
if (string.IsNullOrEmpty(context.Request.RawUrl))
return ((object)null).AsTaskResult();
var operationName = context.Request.GetOperationName().UrlDecode();
var httpReq = context.ToRequest(operationName);
var httpRes = httpReq.Response;
var handler = HttpHandlerFactory.GetHandler(httpReq);
var serviceStackHandler = handler as IServiceStackHandler;
if (serviceStackHandler != null)
{
var restHandler = serviceStackHandler as RestHandler;
if (restHandler != null)
{
httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
}
var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
//Matches Exceptions handled in HttpListenerBase.InitTask()
return task;
}
return new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo)
.AsTaskException();
}
public override void OnConfigLoad()
{
base.OnConfigLoad();
Config.HandlerFactoryPath = string.IsNullOrEmpty(HandlerPath)
? null
: HandlerPath;
Config.MetadataRedirectPath = string.IsNullOrEmpty(HandlerPath)
? "metadata"
: PathUtils.CombinePaths(HandlerPath, "metadata");
}
}
}