-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoapServer.cs
284 lines (237 loc) · 10.3 KB
/
SoapServer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright © Bryan Apellanes 2015
*/
using System;
using System.Net.NetworkInformation;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Xml;
using Naizari.Configuration;
using Naizari.Service;
namespace Naizari
{
/// <summary>
/// This class was extracted from reusable patterns identified in
/// ConfigurationService and provides convenience methods for
/// quickly creating a soap server that runs as a Windows service.
/// </summary>
public class SoapServer : ServiceExe
{
public SoapServer()
{
//The Dynamic and/or Private Ports are those from 49152 through 65535
}
/// <summary>
/// The fully qualified name of the Interface that is implemented by
/// the Type specified in the ServiceType property. The implemented interface
/// must have the [ServiceContract] attribute along with other attributes
/// defining the service provided by the ServiceType. See System.Servicemodel
/// for more information. This is Windows Communication Foundation (WCF) stuff.
/// </summary>
public string ImplementedContract { get; set; }
/// <summary>
/// The Type of the web service implementer.
/// </summary>
public Type ServiceType { get; set; }
protected Thread soapServiceThread;
AutoResetEvent threadSleeper = new AutoResetEvent(false); // will be used to "sleep" the soapServiceThread
ServiceHost host;
protected override void OnStart(string[] args)
{
Log("Starting service");
threadSleeper = new AutoResetEvent(false);
this.BindingType = BindingType.WsHttp;
soapServiceThread = new Thread(new ThreadStart(StartSoapService));
soapServiceThread.IsBackground = true;
soapServiceThread.Start();
Log("Service started");
}
protected void StartSoapService()
{
Log("Service thread started");
try
{
string stringUri = GetBaseAddress(this);
if (string.IsNullOrEmpty(stringUri))
return;
Uri baseAddressUri = new Uri(stringUri);
if (ServiceType != null)
{
host = new ServiceHost(ServiceType, baseAddressUri);
Log("Providing soap service of type " + ServiceType.Name);
}
else
{
Log("ServiceType was not specified.");
return;
}
EndpointAddress endpoint = new EndpointAddress(stringUri);
host.AddServiceEndpoint(ImplementedContract, GetBinding(), stringUri);
// enable wsdl
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = baseAddressUri;
host.Description.Behaviors.Add(smb);
// end enable wsdl
// enable debug info
((ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)]).IncludeExceptionDetailInFaults = true;
// end enable debug info
host.Open();
string addresses = string.Empty;
foreach (Uri uri in host.BaseAddresses)
{
addresses += uri.ToString() + "\r\n";
}
Log("Responding to addresses:\r\n" + addresses);
threadSleeper.WaitOne(); // this is apparently unecessary according to testing but it isn't hurting anything
}
catch (Exception ex)
{
Log("An error occurred.", ex);
}
}
protected static string GetBaseAddress(SoapServer instance)
{
SoapConfig config = null;
try
{
if (instance != null)
instance.Log("Getting configured port");
config = new SoapConfig();
DefaultConfiguration.SetProperties(config, true);
if (instance != null)
instance.Log(serviceName + " port set to " + config.Port.ToString());
}
catch (Exception ex)
{
if (instance != null)
instance.Log(ex.Message);
return null;
}
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
// testing with localhost - change to computerProperties.HostName
string stringUri = string.Format("http://{0}:{1}", computerProperties.HostName, config.Port);
return stringUri;
}
protected void Log(string message)
{
Log(message, null);
}
protected void Log(string message, Exception ex)
{
try
{
if (windowsLogger == null)
CreateLog();
if (ex != null)
windowsLogger.AddEntry(message, ex);
else
windowsLogger.AddEntry(message);
}
catch (Exception fatal)
{
FileLog(fatal);
}
}
protected override void OnStop()
{
host.Close(new TimeSpan(3000));
threadSleeper.Set();
soapServiceThread.Abort();
}
public BindingType BindingType
{
get;
protected set;
}
public bool UseTransportSecurity { get; set; }
protected Binding GetBinding()
{
BindingType bindingType = this.BindingType;
switch (bindingType)
{
case BindingType.Invalid:
throw new InvalidOperationException("Invalid BindingType specified.");
case BindingType.Basic:
return GetBasicBinding();
case BindingType.WsHttp:
return GetWSHttpBinding();
}
return null;
}
public static BasicHttpBinding GetBasicBinding()
{
return GetBasicBinding(false);
}
public static BasicHttpBinding GetBasicBinding(bool useTransportSecurity)
{
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.CloseTimeout = new TimeSpan(0, 1, 0);
binding.OpenTimeout = new TimeSpan(0, 5, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 5, 0);
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferPoolSize = 5242880;
binding.MaxReceivedMessageSize = 5242880;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = Encoding.UTF8;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
readerQuotas.MaxDepth = 32;
readerQuotas.MaxStringContentLength = 8192;
readerQuotas.MaxArrayLength = 16384;
readerQuotas.MaxBytesPerRead = 4096;
readerQuotas.MaxNameTableCharCount = 16384;
binding.ReaderQuotas = readerQuotas;
binding.Security.Mode = useTransportSecurity ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = "";
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
return binding;
}
public static WSHttpBinding GetWSHttpBinding()
{
WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message, false);
binding.CloseTimeout = new TimeSpan(0, 1, 0);
binding.OpenTimeout = new TimeSpan(0, 5, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 5, 0);
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferPoolSize = 5242880;
binding.MaxReceivedMessageSize = 5242880;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = Encoding.UTF8;
binding.TransactionFlow = false;
binding.UseDefaultWebProxy = true;
binding.AllowCookies = false;
XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
readerQuotas.MaxDepth = 32;
readerQuotas.MaxStringContentLength = 5242880;
readerQuotas.MaxArrayLength = 16384;
readerQuotas.MaxBytesPerRead = 4096;
readerQuotas.MaxNameTableCharCount = 16384;
binding.ReaderQuotas = readerQuotas;
binding.ReliableSession.Ordered = true;
binding.ReliableSession.InactivityTimeout = new TimeSpan(0, 10, 0);
binding.Security.Mode = SecurityMode.Message;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = string.Empty;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
binding.Security.Message.NegotiateServiceCredential = true;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
binding.Security.Message.EstablishSecurityContext = true;
return binding;
}
}
}