I get in a lot of fights.
async/await anyway. Despite async/await being over 5 years old (AOTW), there still remains a lot of confusion regarding some basic functionality.
I do not presume to be an in-depth expert. Some of the things I read from Stephen Cleary or Jon Skeet make my eyes go crossed. But for your average, everyday usage, it doesn’t need to be so misunderstood.
I am not going to rehash the excellent articles that explain these things. I am going to share a WPF application that demonstrates some of the commonly misunderstood pitfalls so you can actually observe the things you read about by the experts like Stephen Cleary, Stephen Toub, Jon Skeet.
Introducing “WPFGui”…. This is a simple Windows Presentation Foundation application that will attempt to update the UI on each of the button clicks. Of course I don’t need to update the UI directly with MVVM, but this is a contrived example after all….
Dependency injection is great.
Making your business logic dependent on interfaces is great.
But, did you ever find it cumbersome to mock those interfaces such that all of your business logic is well exercised?
Yeah, me too. In this article I’ll demonstrate how I organize my mocks to keep the bloat to a minimum.
Async programming has become ubiquitous and the standard tool for making async HTTP requests with C# is
HttpClient from the
System.Net.Http namespace. Examples are aplenty, but good examples are few and far between. Because
IDisposable we are conditioned to new it up in a using statement, make the call and get out as fast as possible.
This is WRONG.
This blog post will pull together advice from the few good resources online that touch on various aspects of why you are using it wrong, and how to use it correctly.
If you are coming to this blog post you probably already know what Serilog is and you need to write to a “sink” that is not already provided in the list of Available Sinks. The list is quite long so definitely look closely before developing your own because there is most likely one already built for your needs.
But what if there isn’t? Continue reading
As developers, we are often faced with legacy code bases that were never designed with unit testing in mind. Of course, we now know the value of unit tests and we want to test as much as possible. A good place to start implementing tests in old code is to write tests around each bug and feature. Do not try to refactor the entire pile of rubble, but instead begin with tests around just the code you are adding or changing.
Here I will show you a simple technique to refactor “just enough” to get some tests running without introducing unacceptable risk in your modifications. The examples with be in C#, but the technique applies everywhere.
The async and await keywords have been around for a while (since .Net 4.5) yet there still remains a fair amount of confusion regarding how it works. First off, while the compiler is arguably doing something akin to magic, you could achieve much the same results with BeginInvoke and EndInvoke. The async/await way of doing things makes the code more natural to write and reason about but there is nothing new in the operating system to make this magic.
A basic thing async/await does for you is to allow scalability by allowing your thread to go off and do other work while you wait on some asynchronous activity. It does not mean you spin up new threads and do processing in parallel (though, you could). I’ll describe this in non-technical terms to make it clear.
Suppose you have a butler and we will say he is a synchronous butler. Your butler has been tasked with folding your underwear but you decide to order pizza so you tell your butler to stop folding and go wait at the door for the delivery. Your butler is effectively blocked from doing anything while the pizza is cooked and delivered. When the doorbell rings he will open the door, pay the driver, serve your pizza then get back to folding your spiderman underoos.
What a waste. Why can’t your butler keep folding clothes while the pizza is on its way? If you got the asynchronous model he easily could. He would call to order your pizza, put the task of answering the doorbell on hold, then go do other work. When the doorbell rings, he will pick up his task of serving you pizza where he left off and get the door.
That is the very basics of async/await. When you await a task the thread is free to do other work but it will come back and finish up when it needs to. This provides scalability by not wasting threads and forcing them to sit around idle. You are allowing your application and the operating system to operate more efficiently.
In this article I will explain the sometimes surprising behavior of the ADO.Net
SqlParameter and particularly the effects of
ParameterDirection, null value and DBNull.Value on how the parameter is sent to the database.
Here are some handy code snippets for inserting a table into a Word document through a Task Pane Add-In. If you don’t know what that is, you can get up and running with this hello world example.
I demonstrate inserting at the selection, inserting at a named binding and inserting HTML. There is another way, you can insert raw Office Open XML (OOXML), and while that provides the most power and flexibility, it is also by far the most complex way to do it. Continue reading
I recently had the need to execute a .jar file from within an Azure WebJob built with C#. I found a few resources that showed how to upload a .zip file, for instance, and have that run a .bat file as a job. Or, how to create a Java web app. But my webjob needed to do more than just run a .jar, and I didn’t have the time to get up to speed on Java and the Azure Storage SDK for Java. As the Google let me down, here I demonstrate the solution. Continue reading
T-SQL stored procedures that return a single a value (scalar) or a single row can benefit from the use of OUTPUT parameters. This is not appropriate for multiple row result sets, but if you just need one value, or the results of a single row, you can do it most efficiently with OUTPUT parameters as opposed to SELECTing into a DataReader or DataSet. Continue reading