summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/Transport/Web.cs
diff options
context:
space:
mode:
authorBrandon West <brawest@gmail.com>2015-04-22 11:05:28 -0600
committerBrandon West <brawest@gmail.com>2015-04-22 11:05:28 -0600
commitbdee5c4979a5c019669fe05c85e2fee460af9946 (patch)
tree8cfba69e8ef3b5803cd07fa73004f1a610b5fe76 /SendGrid/SendGridMail/Transport/Web.cs
parent142e30b0af624b750f693bb1bb0af83206037326 (diff)
downloadsendgrid-csharp-bdee5c4979a5c019669fe05c85e2fee460af9946.zip
sendgrid-csharp-bdee5c4979a5c019669fe05c85e2fee460af9946.tar.gz
sendgrid-csharp-bdee5c4979a5c019669fe05c85e2fee460af9946.tar.bz2
Improve performance by reusing connections per instance of web transport
Diffstat (limited to 'SendGrid/SendGridMail/Transport/Web.cs')
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs25
1 files changed, 10 insertions, 15 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index 6f01b11..4d2f484 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -20,9 +20,9 @@ namespace SendGrid
//TODO: Make this configurable
public const String Endpoint = "https://api.sendgrid.com/api/mail.send.xml";
-
+
private readonly NetworkCredential _credentials;
- private readonly TimeSpan _timeout;
+ private readonly HttpClient _client;
#endregion
@@ -30,11 +30,8 @@ namespace SendGrid
/// Creates a new Web interface for sending mail
/// </summary>
/// <param name="credentials">SendGridMessage user parameters</param>
- public Web(NetworkCredential credentials)
- {
- _credentials = credentials;
- _timeout = TimeSpan.FromSeconds(100);
- }
+ public Web(NetworkCredential credentials)
+ : this(credentials, TimeSpan.FromSeconds(100)) { }
/// <summary>
/// Creates a new Web interface for sending mail.
@@ -44,7 +41,11 @@ namespace SendGrid
public Web(NetworkCredential credentials, TimeSpan httpTimeout)
{
_credentials = credentials;
- _timeout = httpTimeout;
+ _client = new HttpClient();
+
+ var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
+ _client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
+ _client.Timeout = httpTimeout;
}
/// <summary>
@@ -53,16 +54,10 @@ namespace SendGrid
/// <param name="message"></param>
public async Task DeliverAsync(ISendGrid message)
{
- var client = new HttpClient();
-
- var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
-
- client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
-
var content = new MultipartFormDataContent();
AttachFormParams(message, content);
AttachFiles(message, content);
- var response = await client.PostAsync(Endpoint, content);
+ var response = await _client.PostAsync(Endpoint, content);
await CheckForErrorsAsync(response);
}