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
70 lines (59 loc) · 2.5 KB
/
Copy pathAppHostHttpListenerBase.cs
File metadata and controls
70 lines (59 loc) · 2.5 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
using System;
using System.Net;
using System.Reflection;
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 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 void ProcessRequest(HttpListenerContext context)
{
if (string.IsNullOrEmpty(context.Request.RawUrl)) return;
var operationName = context.Request.GetOperationName();
var httpReq = new ListenerRequest(operationName, context.Request);
var httpRes = new ListenerResponse(context.Response);
var handler = HttpHandlerFactory.GetHandler(httpReq);
var serviceStackHandler = handler as IServiceStackHttpHandler;
if (serviceStackHandler != null)
{
var restHandler = serviceStackHandler as RestHandler;
if (restHandler != null)
{
httpReq.OperationName = operationName = restHandler.RestPath.RequestType.Name;
}
serviceStackHandler.ProcessRequest(httpReq, httpRes, operationName);
httpRes.Close();
return;
}
throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo);
}
public override void OnConfigLoad()
{
base.OnConfigLoad();
Config.ServiceStackHandlerFactoryPath = string.IsNullOrEmpty(HandlerPath)
? null
: HandlerPath;
Config.MetadataRedirectPath = string.IsNullOrEmpty(HandlerPath)
? "metadata"
: PathUtils.CombinePaths(HandlerPath, "metadata");
}
}
}