-
Notifications
You must be signed in to change notification settings - Fork 9
/
Program.cs
149 lines (133 loc) · 6.33 KB
/
Program.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
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using DemoService;
using Microsoft.ServiceFabric.Services.Remoting.Client;
using Microsoft.ServiceFabric.Services.Remoting.V2.FabricTransport.Client;
using ServiceFabric.Remoting.CustomHeaders;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
//FixedHeaderValues();
DynamicHeaderValues();
//SimulateAndLogException();
}
static void FixedHeaderValues()
{
Console.WriteLine("Press any key to start .... ");
Console.ReadLine();
// Create a new instance of CustomHeaders that is passed on each call.
var customHeaders = new CustomHeaders
{
{"Header1", DateTime.Now},
{"Header2", Guid.NewGuid()}
};
var serviceUri = new Uri("fabric:/ServiceFabric.Remoting.CustomHeaders.DemoApplication/DemoService");
var proxyFactory = new ServiceProxyFactory(handler =>
new ExtendedServiceRemotingClientFactory(
new FabricTransportServiceRemotingClientFactory(remotingCallbackMessageHandler: handler), customHeaders));
var proxy = proxyFactory.CreateServiceProxy<IDemoService>(serviceUri);
var actorResponse = proxy.SayHelloToActor().GetAwaiter().GetResult();
Console.WriteLine($"Actor said '{actorResponse}'");
Console.WriteLine("Press any key to stop. ");
Console.ReadLine();
}
static void DynamicHeaderValues()
{
Console.WriteLine("Press any key to start .... ");
var key = Console.ReadLine();
// Create a factory to provide a new CustomHeaders instance on each call
var customHeadersProvider = new Func<CustomHeaders>(() => new CustomHeaders
{
{"Header1", DateTime.Now},
{"Header2", Guid.NewGuid()},
{"PressedKey", key}
});
var serviceUri = new Uri("fabric:/ServiceFabric.Remoting.CustomHeaders.DemoApplication/DemoService");
var proxyFactory = new ServiceProxyFactory(handler =>
new ExtendedServiceRemotingClientFactory(
new FabricTransportServiceRemotingClientFactory(remotingCallbackMessageHandler: handler), customHeadersProvider)
{
// Optional, log the call before being handled
BeforeSendRequestResponseAsync = requestInfo =>
{
var sw = new Stopwatch();
sw.Start();
Console.WriteLine($"BeforeSendRequestAsync {requestInfo.Service} {requestInfo.Method}");
return Task.FromResult<object>(sw);
},
// Optional, log the call after being handled
AfterSendRequestResponseAsync = responseInfo =>
{
var sw = (Stopwatch)responseInfo.State;
Console.WriteLine($"AfterSendRequestAsync {responseInfo.Service} {responseInfo.Method} took {sw.ElapsedMilliseconds}ms");
return Task.CompletedTask;
}
});
var proxy = proxyFactory.CreateServiceProxy<IDemoService>(serviceUri);
while (true)
{
// the proxy is reused, but the header data is changed as the provider
// is invoked during each SayHelloToActor call.
var actorResponse = proxy.SayHelloToActor().GetAwaiter().GetResult();
Console.WriteLine($"Actor said '{actorResponse}'");
Console.WriteLine("Press any key to restart (q to quit).... ");
key = Console.ReadLine();
if (key.ToLowerInvariant() == "q")
break;
}
}
static void SimulateAndLogException()
{
Console.WriteLine("Press any key to start .... ");
var key = Console.ReadLine();
// Create a factory to provide a new CustomHeaders instance on each call
var customHeadersProvider = new Func<CustomHeaders>(() => new CustomHeaders
{
{"Header1", DateTime.Now},
{"Header2", Guid.NewGuid()},
{"PressedKey", key}
});
var serviceUri = new Uri("fabric:/ServiceFabric.Remoting.CustomHeaders.DemoApplication/DemoService");
var proxyFactory = new ServiceProxyFactory(handler =>
new ExtendedServiceRemotingClientFactory(
new FabricTransportServiceRemotingClientFactory(remotingCallbackMessageHandler: handler), customHeadersProvider)
{
// Optional, log the call before being handled
BeforeSendRequestResponseAsync = requestInfo =>
{
var sw = new Stopwatch();
sw.Start();
Console.WriteLine($"BeforeSendRequestAsync {requestInfo.Service} {requestInfo.Method}");
return Task.FromResult<object>(sw);
},
// Optional, log the call after being handled
AfterSendRequestResponseAsync = responseInfo =>
{
var sw = (Stopwatch)responseInfo.State;
Console.WriteLine($"AfterSendRequestAsync {responseInfo.Service} {responseInfo.Method} took {sw.ElapsedMilliseconds}ms, and got exception {responseInfo.Exception.Message}");
return Task.CompletedTask;
}
});
var proxy = proxyFactory.CreateServiceProxy<IDemoService>(serviceUri);
while (true)
{
try
{
proxy.ThrowException().GetAwaiter().GetResult();
}
catch (Exception e)
{
Console.WriteLine($"Finished with exception: {e.Message}");
}
Console.WriteLine("Press any key to restart (q to quit).... ");
key = Console.ReadLine();
if (key.ToLowerInvariant() == "q")
break;
}
}
}
}