Beware mixing Tasks with async/await
I recently came across an issue within a pipeline of asynchronous method calls where an attempt to deserialize a response from json to a known type may throw an exception, usually when the response is not json. For example, this may be the case when using something like nginx as a proxy for authentication purposes and failed authentication returns a HTML page. A simple reproducible example would be
void Main()
{
MainAsync().Wait();
}
async Task MainAsync()
{
var result = await TryDeserializeAsync().ConfigureAwait(false);
Console.WriteLine(result);
}
Task<double?> TryDeserializeAsync()
{
try {
return DeserializeAsync();
}
catch {
// swallow any exceptions thrown.
}
return null;
}
async Task<double?> DeserializeAsync()
{
await Task.Delay(100);
throw new Exception("Serialization error");
}
Here we await TryDeserializeAsync()
which in turn calls the actual DeserializeAsync()
implementation, swallowing any exception that may occur in deserialization and simple returning null
. That's the plan anyway.
c# (16)
asp.net mvc (8)
javascript (6)
jquery (5)
search (4)
elasticsearch (4)
nest (3)
openai (2)
async (2)
asynchrony (2)
await (2)
f# (2)
dependency injection (2)
castle windsor (2)
model binding (2)
validation (2)
llm (1)
csharp (1)
ocr (1)
translation (1)
vector search (1)
qdrant (1)
x509 (1)
certificate (1)
ssl (1)
tls (1)
bouncy castle (1)
statiq (1)
github (1)
query (1)
array (1)
internalsvisibleto (1)
dotnet (1)
msbuild (1)
friend assemblies (1)
monitoring (1)
windows (1)
process (1)
procmon (1)
vagrant (1)
winrm (1)
powershell (1)
task parallel library (1)
tpl (1)
gnaf (1)
addresses (1)
australia (1)
sql server (1)
akka (1)
akka.net (1)
reactive (1)
actor model (1)
agents (1)
versioning (1)
optimistic concurrency (1)
geospatial (1)
geojson (1)
geometry (1)
resharper (1)
semanticmerge (1)
git (1)
refactoring (1)
clean code (1)
windows services (1)
topshelf (1)
masstransit (1)
email (1)
acceptance tests (1)
specflow (1)
smtp4dev (1)
bdd (1)
wsfederationauthenticationmodule (1)
classic asp (1)
windows identity foundation (1)
wif (1)
nuget (1)
asp.net web api (1)
parameter binding (1)
csv (1)
valueprovider (1)
signal r (1)
ndc 2012 (1)
conferences (1)
events (1)
razor (1)
continuation passing style (1)
captcha (1)
routing (1)
modelmetadata (1)
podcasts (1)