forked from Topshelf/Topshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceHandleProxy.cs
164 lines (142 loc) · 5.5 KB
/
ServiceHandleProxy.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2007-2012 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace Topshelf.Supervise
{
using System;
using System.Security;
using System.Security.Permissions;
using HostConfigurators;
using Logging;
using Runtime;
/// <summary>
/// The proxy handle into the separate AppDomain for the service. Uniquely identified
/// by a GUID name so that we can spin up the next instance on a restart/recycle allowing
/// the new instance to warm up before actually putting it into traffic.
/// </summary>
public class ServiceHandleProxy :
ServiceHandle
{
readonly HostControl _hostControl;
readonly LogWriter _log = HostLogger.Get<ServiceHandleProxy>();
readonly ServiceHandle _service;
readonly ServiceBuilderFactory _serviceBuilderFactory;
readonly HostSettings _settings;
AppDomain _appDomain;
string _appDomainName;
public ServiceHandleProxy(HostSettings settings, HostControl hostControl, ServiceBuilderFactory serviceBuilderFactory)
{
_settings = settings;
_hostControl = new HostControlProxy(hostControl);
_serviceBuilderFactory = serviceBuilderFactory;
_service = CreateServiceInAppDomain();
}
public void Dispose()
{
UnloadServiceAppDomain();
}
bool ServiceHandle.Start(HostControl hostControl)
{
return _service.Start(_hostControl);
}
bool ServiceHandle.Pause(HostControl hostControl)
{
return _service.Pause(_hostControl);
}
bool ServiceHandle.Continue(HostControl hostControl)
{
return _service.Continue(_hostControl);
}
bool ServiceHandle.Stop(HostControl hostControl)
{
return _service.Stop(_hostControl);
}
public void Shutdown(HostControl hostControl)
{
_service.Shutdown(_hostControl);
}
public void SessionChanged(HostControl hostControl, SessionChangedArguments arguments)
{
_service.SessionChanged(hostControl, arguments);
}
public void CustomCommand(HostControl hostControl, int command)
{
_service.CustomCommand(hostControl, command);
}
void UnloadServiceAppDomain()
{
try
{
_service.Dispose();
}
finally
{
try
{
AppDomain.Unload(_appDomain);
}
catch (Exception ex)
{
_log.Error("Failed to unload AppDomain", ex);
}
}
}
ServiceHandle CreateServiceInAppDomain()
{
var appDomainSetup = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName,
LoaderOptimization = LoaderOptimization.MultiDomainHost,
};
var permissionSet = new PermissionSet(PermissionState.Unrestricted);
permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
AppDomain appDomain = null;
try
{
_appDomainName = string.Format("{0}.{1}.{2}", "Topshelf", _settings.Name,
Guid.NewGuid().ToString("N"));
appDomain = AppDomain.CreateDomain(
_appDomainName,
null,
appDomainSetup,
permissionSet,
null);
return CreateServiceHandle(appDomain);
}
catch (Exception)
{
if (appDomain != null)
{
AppDomain.Unload(appDomain);
appDomain = null;
}
throw;
}
finally
{
if (appDomain != null)
_appDomain = appDomain;
}
}
ServiceHandle CreateServiceHandle(AppDomain appDomain)
{
Type type = typeof(ServiceHandleStub);
string assemblyName = type.Assembly.FullName;
string typeName = type.FullName ?? typeof(ServiceHandleStub).Name;
var serviceHandle = (ServiceHandleStub)appDomain.CreateInstanceAndUnwrap(assemblyName, typeName);
serviceHandle.Create(_serviceBuilderFactory, _settings, HostLogger.CurrentHostLoggerConfigurator);
return serviceHandle;
}
}
}