-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathClient.cs
More file actions
80 lines (63 loc) · 2.85 KB
/
Client.cs
File metadata and controls
80 lines (63 loc) · 2.85 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
// Copyright (c) Microsoft Corporation. All Rights Reserved.
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(SessionMode = SessionMode.Required)]
internal interface IEchoService
{
[OperationContract]
string Echo(string value);
}
public static class CustomHeader
{
public const string HeaderName = "InstanceId";
public const string HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press <enter> to open a channel and send a request.");
Console.ReadLine();
MessageHeader shareableInstanceContextHeader = MessageHeader.CreateHeader(
CustomHeader.HeaderName,
CustomHeader.HeaderNamespace,
Guid.NewGuid().ToString());
#if NET6_0_OR_GREATER
CustomBinding binding = new CustomBinding(new TextMessageEncodingBindingElement(), new TcpTransportBindingElement());
EndpointAddress endpointAddress = new EndpointAddress(new Uri("net.tcp://localhost:9000/echoservice"));
ChannelFactory<IEchoService> channelFactory = new ChannelFactory<IEchoService>(binding, endpointAddress);
#else
ChannelFactory<IEchoService> channelFactory = new ChannelFactory<IEchoService>("echoservice");
#endif
IEchoService proxy = channelFactory.CreateChannel();
using (new OperationContextScope((IClientChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.Add(shareableInstanceContextHeader);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Service returned: " + proxy.Echo("Apple"));
Console.ForegroundColor = ConsoleColor.Gray;
}
((IChannel)proxy).Close();
Console.WriteLine("Channel No 1 closed.");
Console.WriteLine(
"Press <ENTER> to send another request from a different channel " +
"to the same instance. ");
Console.ReadLine();
proxy = channelFactory.CreateChannel();
using (new OperationContextScope((IClientChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.Add(shareableInstanceContextHeader);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Service returned: " + proxy.Echo("Apple"));
Console.ForegroundColor = ConsoleColor.Gray;
}
((IChannel)proxy).Close();
Console.WriteLine("Channel No 2 closed.");
Console.WriteLine("Press <ENTER> to complete test.");
Console.ReadLine();
}
}
}