Chapter 20 Tech Edits
MODIFY
src/Chapter20.Tests/BaseProgramTests.cs
src/Chapter20.Tests/BaseProgramTests.cs
@@ -113,7 +113,7 @@ http://www.IntelliTect.com";
// Rethrowing rather than using
// if condition on the type
ExceptionDispatchInfo.Capture(
- aggregateException.InnerException!).Throw();
+ innerException!).Throw();
return true; // Identifhy whether the exception should report that it is handled or not.
});
@@ -139,7 +139,7 @@ http://www.IntelliTect.com";
// Rethrowing rather than using
// if condition on the type
ExceptionDispatchInfo.Capture(
- exception.InnerException!).Throw();
+ innerException!).Throw();
return true;
});
@@ -196,7 +196,7 @@ http://www.IntelliTect.com";
// Rethrowing rather than using
// if condition on the type
ExceptionDispatchInfo.Capture(
- exception.InnerException!).Throw();
+ innerException!).Throw();
return true;
});

MODIFY
src/Chapter20.Tests/ProgramWrapper.cs
src/Chapter20.Tests/ProgramWrapper.cs
@@ -33,5 +33,4 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Tests
return await FindTextInWebUriMethod(findText, urls, progressCallback);
}
}
-
}

MODIFY
src/Chapter20/Listing20.01.SynchronousWebRequestHighLatency.cs
src/Chapter20/Listing20.01.SynchronousWebRequestHighLatency.cs
@@ -15,7 +15,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_01
using WebClient webClient = new WebClient();
- byte[] downloadData = webClient.DownloadData(url);
+ byte[] downloadData =
+ webClient.DownloadData(url);
using MemoryStream stream = new MemoryStream(downloadData);
using StreamReader reader = new StreamReader(stream);
@@ -72,19 +73,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_01
Console.WriteLine(occurances);
}
-
- static public string FormatBytes(long bytes)
- {
- string[] magnitudes =
- new string[] { "GB", "MB", "KB", "Bytes" };
- long max =
- (long)Math.Pow(1024, magnitudes.Length);
-
- return string.Format("{1:##.##} {0}",
- magnitudes.FirstOrDefault(
- magnitude =>
- bytes > (max /= 1024)) ?? "0 Bytes",
- (decimal)bytes / (decimal)max);
- }
}
}

MODIFY
src/Chapter20/Listing20.02.AsynchronousHighLatencyUsingTaskAsynchronyPattern.cs
src/Chapter20/Listing20.02.AsynchronousHighLatencyUsingTaskAsynchronyPattern.cs
@@ -5,12 +5,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
using System.Net;
using System.Linq;
using System.Threading.Tasks;
- using System.Runtime.ExceptionServices;
static public class Program
{
- async static public Task<int> FindTextInWebUriAsync(
+ public async static Task<int> FindTextInWebUriAsync(
string findText, string url,
IProgress<DownloadProgressChangedEventArgs>? progressCallback = null)
{
@@ -84,18 +83,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
}
);
- try
- {
int occurances =
await FindTextInWebUriAsync(findText, url, progress);
Console.WriteLine(occurances);
}
- catch (AggregateException)
- {
- throw new InvalidOperationException(
- $"AggregateException not expected for the {nameof(FindTextInWebUriAsync)} async method.");
- }
- }
static public string FormatBytes(long bytes)
{

MODIFY
src/Chapter20/Listing20.03.AsynchronousWebRequestHighLatency.cs
src/Chapter20/Listing20.03.AsynchronousWebRequestHighLatency.cs
@@ -109,7 +109,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_03
// Rethrowing rather than using
// if condition on the type
ExceptionDispatchInfo.Capture(
- exception.InnerException!).Throw();
+ innerException!).Throw();
return true;
});

MODIFY
src/Chapter20/Listing20.06A.AsynchronousStreams.cs
src/Chapter20/Listing20.06A.AsynchronousStreams.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_05
{
using System;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading;
- using System.Runtime.CompilerServices;
- using AddisonWesley.Michaelis.EssentialCSharp.Shared;
+ using System.Threading.Tasks;
-class AsyncEncryptionCollection : IAsyncEnumerable<string?>
+ class AsyncEncryptionCollection : IAsyncEnumerable<string>
{
- public async IAsyncEnumerator<string?> GetAsyncEnumerator(
+ public async IAsyncEnumerator<string> GetAsyncEnumerator(
CancellationToken cancellationToken = default)
{
- yield return await Task<string>.Run(() => (string?)null);
+ // Mock code
+ yield return await Task<string>.Run(() => (string)"<filename>");
}
static public async void Main()
@@ -23,7 +20,7 @@ class AsyncEncryptionCollection : IAsyncEnumerable<string?>
new AsyncEncryptionCollection();
// ...
- await foreach (string? fileName in collection)
+ await foreach (string fileName in collection)
{
Console.WriteLine(fileName);
}

MODIFY
src/Chapter20/Listing20.07.CatchingExceptionFromAsyncVoidMethod.cs
src/Chapter20/Listing20.07.CatchingExceptionFromAsyncVoidMethod.cs
@@ -50,7 +50,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_07
static bool EventTriggered { get; set; }
public const string ExpectedExceptionMessage = "Expected Exception";
- public static void Main()
+ public static async void Main()
{
AsyncSynchronizationContext synchronizationContext =
@@ -62,11 +62,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_07
OnEvent(typeof(Program), new EventArgs());
-#if WithOutUsingResetEvent
- Task.Delay(1000); //
-#else
+//#if WithOutUsingResetEvent
+ Task.Delay(1000).Wait(); //
+//#else
synchronizationContext.ResetEvent.Wait();
-#endif
+//#endif
if (synchronizationContext.Exception != null)
{

Chapter 20 Tech Edits