-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move namespace Leaf.Core.Extensions.System to Leaf.Core.Extensions.
New methods: ThreadSafeUI.IsCanceledByUser(Exception). New extensions: SmtpClient.SendMailAsync(MailMessage, CancellationToken); string.IsNullOrEmpty(); string.HasContent(); Task.Caf() = Task.ConfigureAwait(false);
- Loading branch information
1 parent
15081b9
commit e8af6d3
Showing
10 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e/Extensions/System/DateTimeExtensions.cs → Leaf.Core/Extensions/DateTimeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../Extensions/System/ExceptionExtensions.cs → Leaf.Core/Extensions/ExceptionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Net.Mail; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Leaf.Core.Extensions.Net | ||
{ | ||
public static class SmtpClientExtensions | ||
{ | ||
/// <summary> | ||
/// Sends the specified message to an SMTP server for delivery as an asynchronous operation. | ||
/// </summary> | ||
/// <param name="client"> | ||
/// The <see cref="SmtpClient"/> instance. | ||
/// </param> | ||
/// <param name="message">The <see cref="MailMessage"/> to send.</param> | ||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <para><paramref name="client"/> is <see langword="null"/>.</para> | ||
/// <para>-or-</para> | ||
/// <para><paramref name="message"/> is <see langword="null"/>.</para> | ||
/// </exception> | ||
public static Task SendMailAsync(this SmtpClient client, MailMessage message, CancellationToken cancellationToken) | ||
{ | ||
if (client == null) throw new ArgumentNullException(nameof(client)); | ||
if (message == null) throw new ArgumentNullException(nameof(message)); | ||
if (!cancellationToken.CanBeCanceled) return client.SendMailAsync(message); | ||
|
||
var tcs = new TaskCompletionSource<object>(); | ||
var registration = default(CancellationTokenRegistration); | ||
SendCompletedEventHandler handler = null; | ||
|
||
handler = (sender, e) => { | ||
if (e.UserState != tcs) | ||
return; | ||
|
||
try | ||
{ | ||
if (handler == null) | ||
return; | ||
|
||
client.SendCompleted -= handler; | ||
handler = null; | ||
} | ||
finally | ||
{ | ||
registration.Dispose(); | ||
|
||
if (e.Error != null) | ||
tcs.TrySetException(e.Error); | ||
else if (e.Cancelled) | ||
tcs.TrySetCanceled(); | ||
else | ||
tcs.TrySetResult(null); | ||
} | ||
}; | ||
|
||
client.SendCompleted += handler; | ||
|
||
try | ||
{ | ||
client.SendAsync(message, tcs); | ||
registration = cancellationToken.Register(client.SendAsyncCancel); | ||
} | ||
catch | ||
{ | ||
client.SendCompleted -= handler; | ||
registration.Dispose(); | ||
throw; | ||
} | ||
|
||
return tcs.Task; | ||
} | ||
|
||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tensions/System/NumberFormatExtensions.cs → ...Core/Extensions/NumberFormatExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
|
||
namespace Leaf.Core.Extensions | ||
{ | ||
public static class TaskExtensions | ||
{ | ||
public static ConfiguredTaskAwaitable Caf(this Task task) => task.ConfigureAwait(false); | ||
public static ConfiguredTaskAwaitable<T> Caf<T>(this Task<T> task) => task.ConfigureAwait(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters