Skip to content

Commit 757f462

Browse files
author
Cory Thompson
committed
#7 Improve HttpClient Usage
1 parent 225797f commit 757f462

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/WebPushClient.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,27 @@ public class WebPushClient
1313
const int DefaultTtl = 2419200;
1414

1515
private string _gcmAPIKey = null;
16+
private HttpClient _httpClient = null;
1617
private VapidDetails _vapidDetails = null;
1718

1819
public WebPushClient()
1920
{
2021

2122
}
2223

24+
protected HttpClient httpClient
25+
{
26+
get
27+
{
28+
if (_httpClient == null)
29+
{
30+
_httpClient = new HttpClient();
31+
}
32+
33+
return _httpClient;
34+
}
35+
}
36+
2337
/// <summary>
2438
/// When sending messages to a GCM endpoint you need to set the GCM API key
2539
/// by either calling setGCMAPIKey() or passing in the API key as an option
@@ -227,9 +241,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
227241
public void SendNotification(PushSubscription subscription, string payload = null, Dictionary<string, object> options = null)
228242
{
229243
HttpRequestMessage request = GenerateRequestDetails(subscription, payload, options);
230-
HttpClient client = new HttpClient();
231-
232-
Task<HttpResponseMessage> webTask = client.SendAsync(request);
244+
Task<HttpResponseMessage> webTask = httpClient.SendAsync(request);
233245
webTask.Wait();
234246

235247
HttpResponseMessage response = webTask.Result;

0 commit comments

Comments
 (0)