-
Notifications
You must be signed in to change notification settings - Fork 558
/
WcfService.cs
480 lines (412 loc) · 17.9 KB
/
WcfService.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class WcfService : IWcfService
{
public String EchoWithTimeout(String message, TimeSpan serviceOperationTimeout)
{
System.Threading.Thread.Sleep(serviceOperationTimeout);
return message;
}
public Message MessageRequestReply(Message message)
{
var reader = message.GetReaderAtBodyContents();
Message requestmessage = Message.CreateMessage(
message.Version,
"http://tempuri.org/IWcfService/MessageRequestReplyResponse",
reader.ReadString() + "[service] Request received, this is my Reply.");
return requestmessage;
}
public String Echo(String message)
{
return message;
}
public ComplexCompositeType EchoComplex(ComplexCompositeType message)
{
return message;
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public MessageInspector_CustomHeaderAuthentication.ResultObject<string> UserGetAuthToken()
{
MessageInspector_CustomHeaderAuthentication.ResultObject<string> resultObject = null;
try
{
HttpRequestMessageProperty property;
object obj;
MessageProperties properties = new MessageProperties(OperationContext.Current.IncomingMessageProperties);
if (properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
property = obj as HttpRequestMessageProperty;
WebHeaderCollection collection = property.Headers;
string authValue = collection.Get(Enum.GetName(typeof(HttpRequestHeader), HttpRequestHeader.Authorization));
if (authValue == "Not Allowed")
{
resultObject = MessageInspector_CustomHeaderAuthentication.ResultObject<string>.CreateFailureObject<string>();
resultObject.Result = resultObject.ResultMessage;
}
else if (authValue == "Allow")
{
resultObject = MessageInspector_CustomHeaderAuthentication.ResultObject<string>.CreateSuccessObject<string>();
resultObject.Result = resultObject.ResultMessage;
}
}
else
{
resultObject = MessageInspector_CustomHeaderAuthentication.ResultObject<string>.CreateFailureObject<string>();
resultObject.Result = "ERROR";
resultObject.ResultMessage = "No HttpRequestMessageProperty was found on the incoming Message.";
}
}
catch (Exception ex)
{
resultObject = MessageInspector_CustomHeaderAuthentication.ResultObject<string>.CreateFailureObject<string>();
resultObject.Result = ex.ToString();
resultObject.ResultMessage = ex.Message;
}
return resultObject;
}
public Dictionary<string, string> ValidateMessagePropertyHeaders()
{
Dictionary<string, string> headerCollection = new Dictionary<string, string>();
try
{
HttpRequestMessageProperty property;
object obj;
MessageProperties properties = new MessageProperties(OperationContext.Current.IncomingMessageProperties);
if (properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
property = obj as HttpRequestMessageProperty;
WebHeaderCollection collection = property.Headers;
string[] headers = collection.AllKeys;
foreach (string s in headers)
{
string[] values = collection.GetValues(s);
headerCollection.Add(s, String.Join(",", values));
}
}
else
{
headerCollection.Add("ERROR", "No HttpRequestMessageProperty was found!");
}
}
catch (Exception ex)
{
headerCollection.Add("ERROR", string.Format("An exception was thrown: {0}", ex.Message));
}
return headerCollection;
}
public void TestFault(string faultMsg)
{
throw new FaultException<FaultDetail>(new FaultDetail(faultMsg));
}
public void TestFaultInt(int faultCode)
{
throw new FaultException<int>(faultCode);
}
public void TestFaults(string faultMsg, bool throwFaultDetail)
{
if (throwFaultDetail)
{
throw new FaultException<FaultDetail>(new FaultDetail(faultMsg));
}
else
{
throw new FaultException<FaultDetail2>(new FaultDetail2(faultMsg));
}
}
public object[] TestFaultWithKnownType(string faultMsg, object[] objects)
{
if (objects == null)
{
throw new FaultException<FaultDetail>(new FaultDetail(faultMsg));
}
return objects;
}
public void ThrowInvalidOperationException(string message)
{
throw new InvalidOperationException(message);
}
public ReplyBankingData MessageContractRequestReply(RequestBankingData bt)
{
ReplyBankingData bankingData = new ReplyBankingData();
bankingData.accountName = bt.accountName;
bankingData.transactionDate = bt.transactionDate;
bankingData.amount = bt.amount;
return bankingData;
}
public ReplyBankingDataNotWrapped MessageContractRequestReplyNotWrapped(RequestBankingData bt)
{
ReplyBankingDataNotWrapped bankingData = new ReplyBankingDataNotWrapped();
bankingData.accountName = bt.accountName;
bankingData.transactionDate = bt.transactionDate;
bankingData.amount = bt.amount;
return bankingData;
}
public ReplyBankingDataWithMessageHeader MessageContractRequestReplyWithMessageHeader(RequestBankingData bt)
{
ReplyBankingDataWithMessageHeader bankingData = new ReplyBankingDataWithMessageHeader();
bankingData.accountName = bt.accountName;
bankingData.transactionDate = bt.transactionDate;
bankingData.amount = bt.amount;
bankingData.extraValues = bt.accountName;
return bankingData;
}
public ReplyBankingDataWithMessageHeaderNotNecessaryUnderstood MessageContractRequestReplyWithMessageHeaderNotNecessaryUnderstood(RequestBankingData bt)
{
ReplyBankingDataWithMessageHeaderNotNecessaryUnderstood bankingData = new ReplyBankingDataWithMessageHeaderNotNecessaryUnderstood();
bankingData.accountName = bt.accountName;
bankingData.transactionDate = bt.transactionDate;
bankingData.amount = bt.amount;
bankingData.extraValues = bt.accountName;
return bankingData;
}
public TestHttpRequestMessageProperty EchoHttpRequestMessageProperty()
{
object obj;
MessageProperties properties = new MessageProperties(OperationContext.Current.IncomingMessageProperties);
if (properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
HttpRequestMessageProperty property = (HttpRequestMessageProperty)obj;
if (property != null)
{
TestHttpRequestMessageProperty testProperty = new TestHttpRequestMessageProperty();
testProperty.SuppressEntityBody = property.SuppressEntityBody;
testProperty.Method = property.Method;
testProperty.QueryString = property.QueryString;
WebHeaderCollection collection = property.Headers;
foreach (string s in collection.AllKeys)
{
string[] values = collection.GetValues(s);
testProperty.Headers.Add(s, String.Join(",", values));
}
return testProperty;
}
}
return null;
}
public string GetRestartServiceEndpoint()
{
BasicHttpBinding binding = new BasicHttpBinding();
Guid guid = Guid.NewGuid();
string localHost = "http://localhost";
string path = "/WindowsCommunicationFoundationTest/" + WindowsIdentity.GetCurrent().Name.Split('\\').Last() + "/" + guid.ToString();
ServiceHost host = new ServiceHost(typeof(WcfRestartService));
host.AddServiceEndpoint(typeof(IWcfRestartService), binding, localHost + path);
host.Open();
// Add the ServiceHost instance to a static dictionary so that it can be used by restart service operation to close the ServiceHost
WcfRestartService.serviceHostDictionary.Add(guid, host);
// Return the unique endpoint for this ServiceHost instance of the WcfRestartService
return "http://[HOST]" + path;
}
public string GetRequestCustomHeader(string customHeaderName, string customHeaderNamespace)
{
string value = null;
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
// look at headers on incoming message
for (int i = 0; i < headers.Count; ++i)
{
MessageHeaderInfo h = headers[i];
if (h.Name == customHeaderName &&
h.Namespace == customHeaderNamespace)
{
System.Xml.XmlReader xr = headers.GetReaderAtHeader(i);
value = xr.ReadElementContentAsString();
return value;
}
}
return value;
}
public Dictionary<string, string> GetIncomingMessageHeaders()
{
Dictionary<string, string> infos = new Dictionary<string, string>();
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
// look at headers on incoming message
for (int i = 0; i < headers.Count; ++i)
{
MessageHeaderInfo h = headers[i];
System.Xml.XmlReader xr = headers.GetReaderAtHeader(i);
string value = xr.ReadElementContentAsString();
infos.Add(string.Format("{0}//{1}", h.Namespace, h.Name), value);
}
return infos;
}
public string GetIncomingMessageHeadersMessage(string customHeaderName, string customHeaderNS)
{
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
MesssageHeaderCreateHeaderWithXmlSerializerTestType header = headers.GetHeader<MesssageHeaderCreateHeaderWithXmlSerializerTestType>(customHeaderName, customHeaderNS);
if (header != null)
{
return header.Message;
}
return string.Empty;
}
public XmlMessageContractTestResponse EchoMessageResponseWithMessageHeader(XmlMessageContractTestRequest request)
{
var result = new XmlMessageContractTestResponse(request.Message);
return result;
}
public XmlMessageContractTestResponse EchoMessageResquestWithMessageHeader(XmlMessageContractTestRequestWithMessageHeader request)
{
var result = new XmlMessageContractTestResponse(request.Message);
return result;
}
public string EchoXmlSerializerFormat(string message)
{
return message;
}
public string EchoXmlSerializerFormatSupportFaults(string message, bool pleaseThrowException)
{
if (!pleaseThrowException)
{
return message;
}
else
{
throw new Exception(message);
}
}
public string EchoXmlSerializerFormatUsingRpc(string message)
{
return message;
}
public XmlCompositeType GetDataUsingXmlSerializer(XmlCompositeType composite)
{
composite.StringValue = composite.StringValue;
return composite;
}
public LoginResponse Login(LoginRequest request)
{
var response = new LoginResponse();
response.@return = request.clientId + request.user + request.pwd;
return response;
}
public object[] EchoItems(object[] objects)
{
return objects;
}
public object[] EchoItems_Xml(object[] objects)
{
return objects;
}
public object[] EchoItems_Xml1(object[] objects)
{
return objects;
}
public XmlVeryComplexType EchoXmlVeryComplexType(XmlVeryComplexType complex)
{
return complex;
}
public Stream GetStreamFromString(string data)
{
return StringToStream(data);
}
public string GetStringFromStream(Stream stream)
{
string data = StreamToString(stream);
return data;
}
public Stream EchoStream(Stream stream)
{
string data = StreamToString(stream);
return StringToStream(data);
}
public string EchoMessageParameter(string name)
{
return string.Format("Hello {0}", name);
}
public void ReturnContentType(string contentType)
{
var outgoingMessageProperties = OperationContext.Current.OutgoingMessageProperties;
object httpResponseMessagePropertyObj;
if (!outgoingMessageProperties.TryGetValue(HttpResponseMessageProperty.Name, out httpResponseMessagePropertyObj))
{
httpResponseMessagePropertyObj = new HttpResponseMessageProperty();
outgoingMessageProperties.Add(HttpResponseMessageProperty.Name, httpResponseMessagePropertyObj);
}
var httpRespononseMessageProperty = (HttpResponseMessageProperty)httpResponseMessagePropertyObj;
httpRespononseMessageProperty.Headers[HttpResponseHeader.ContentType] = contentType;
}
public string EchoCookie()
{
string cookie = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Cookie];
return cookie;
}
// Returns the current time and also sets the cookie named 'name' to be the same time returned.
public string EchoTimeAndSetCookie(string name)
{
string cookie = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Cookie];
string value = DateTime.Now.ToString();
cookie = string.Format("{0}={1}", name, value);
WebOperationContext.Current.OutgoingResponse.Headers.Add(string.Format("Set-Cookie: {0};", cookie));
return value;
}
public bool IsHttpKeepAliveDisabled()
{
MessageProperties properties = new MessageProperties(OperationContext.Current.IncomingMessageProperties);
var property = (HttpRequestMessageProperty)properties[HttpRequestMessageProperty.Name];
WebHeaderCollection collection = property.Headers;
string connectionValue = collection.Get(Enum.GetName(typeof(HttpRequestHeader), HttpRequestHeader.Connection));
return connectionValue.Equals("Close", StringComparison.OrdinalIgnoreCase);
}
public Dictionary<string, string> GetRequestHttpHeaders()
{
var headers = new Dictionary<string, string>();
object httpReqMessagePropObj;
if (OperationContext.Current.IncomingMessageProperties.TryGetValue(HttpRequestMessageProperty.Name,
out httpReqMessagePropObj))
{
var httpRequestMessageProperty = (HttpRequestMessageProperty)httpReqMessagePropObj;
foreach (var headerName in httpRequestMessageProperty.Headers.AllKeys)
{
headers.Add(headerName, httpRequestMessageProperty.Headers[headerName]);
}
}
return headers;
}
private static string StreamToString(Stream stream)
{
var reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
private static Stream StringToStream(string str)
{
var ms = new MemoryStream();
var sw = new StreamWriter(ms, new UTF8Encoding(false));
sw.Write(str);
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
public System.Threading.Tasks.Task EchoReturnTask()
{
return System.Threading.Tasks.Task.Run(() =>
{
System.Threading.Tasks.Task.Delay(1).Wait();
});
}
}
}