Skip to content

Commit dc40c80

Browse files
Httpclient issue - object initializing multiple times (fixed) and usi… (#36)
* Httpclient issue - object initializing multiple times (fixed) and using default timeout. * non-default timeout removed.
1 parent b74a158 commit dc40c80

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

OptimizelySDK/Event/Dispatcher/HttpClientEventDispatcher45.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ public class HttpClientEventDispatcher45 : IEventDispatcher
2626
public ILogger Logger { get; set; } = new DefaultLogger();
2727

2828
/// <summary>
29-
/// Timeout for the HTTP request (10 seconds)
29+
/// HTTP client object.
3030
/// </summary>
31-
const int TIMEOUT_MS = 10000;
31+
private static readonly HttpClient Client;
32+
33+
/// <summary>
34+
/// Constructor for initializing static members.
35+
/// </summary>
36+
static HttpClientEventDispatcher45()
37+
{
38+
Client = new HttpClient();
39+
}
3240

3341
/// <summary>
3442
/// Dispatch an Event asynchronously
@@ -38,26 +46,20 @@ private async void DispatchEventAsync(LogEvent logEvent)
3846
try
3947
{
4048
string json = logEvent.GetParamsAsJson();
41-
42-
using (var client = new HttpClient())
49+
var request = new HttpRequestMessage
4350
{
44-
client.Timeout = TimeSpan.FromMilliseconds(TIMEOUT_MS);
45-
46-
var request = new HttpRequestMessage
47-
{
48-
RequestUri = new Uri(logEvent.Url),
49-
Method = HttpMethod.Post,
50-
// The Content-Type header applies to the Content, not the Request itself
51-
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"),
52-
};
51+
RequestUri = new Uri(logEvent.Url),
52+
Method = HttpMethod.Post,
53+
// The Content-Type header applies to the Content, not the Request itself
54+
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"),
55+
};
5356

54-
foreach (var h in logEvent.Headers)
55-
if (h.Key.ToLower() != "content-type")
56-
request.Content.Headers.Add(h.Key, h.Value);
57+
foreach (var h in logEvent.Headers)
58+
if (h.Key.ToLower() != "content-type")
59+
request.Content.Headers.Add(h.Key, h.Value);
5760

58-
var result = await client.SendAsync(request);
59-
result.EnsureSuccessStatusCode();
60-
}
61+
var result = await Client.SendAsync(request);
62+
result.EnsureSuccessStatusCode();
6163
}
6264
catch (Exception ex)
6365
{

OptimizelySDK/Event/Dispatcher/WebRequestEventDispatcher35.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ public class WebRequestClientEventDispatcher35 : IEventDispatcher
2525
// TODO Catch and Log Errors
2626
public Logger.ILogger Logger { get; set; }
2727

28-
/// <summary>
29-
/// Timeout for the HTTP request (10 seconds)
30-
/// </summary>
31-
private const int TIMEOUT_MS = 10000;
32-
3328
private HttpWebRequest Request = null;
3429

3530
/// <summary>
@@ -43,7 +38,6 @@ public void DispatchEvent(LogEvent logEvent)
4338
Request = (HttpWebRequest)WebRequest.Create(logEvent.Url);
4439

4540
Request.UserAgent = "Optimizely-csharp-SDKv01";
46-
Request.Timeout = TIMEOUT_MS;
4741
Request.Method = logEvent.HttpVerb;
4842

4943
foreach (var h in logEvent.Headers)

0 commit comments

Comments
 (0)