Event-driven .NET: How to consume an SSE endpoint with HttpClient

Server-Sent Events (SSE) allow a client to subscribe to server events. It creates a one-way stream from the server to the client. When the server has new updates for the client, it writes them to the stream. This is an alternative to the client polling the server for updates. Use the following to consume an … Read more Event-driven .NET: How to consume an SSE endpoint with HttpClient

C# – Cannot use a lambda expression as an argument to a dynamically dispatched operation

Problem You are trying to use a lambda expression on a dynamic object and get the following compiler error: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type. As an example, the following code causes this error: Solution Cast the … Read more C# – Cannot use a lambda expression as an argument to a dynamically dispatched operation

C# – How to unit test code that uses HttpClient

HttpClient is a dependency. Like any other dependency, you need to pass it into your code (aka dependency injection). By passing it in, you can mock it out in unit tests. There are two approaches to mocking it out: Wrap the HttpClient and mock out the wrapper. Use a real HttpClient with a mocked out … Read more C# – How to unit test code that uses HttpClient

C# – Cannot convert null to type parameter ‘T’ because it could be a non-nullable value type

Problem You’re trying to return null from a generic method and you’re getting the following compiler error: Cannot convert null to type parameter ‘T’ because it could be a non-nullable value type. Consider using ‘default(T)’ instead You can’t return null because the compiler doesn’t know if T is nullable. Solution There are a few options … Read more C# – Cannot convert null to type parameter ‘T’ because it could be a non-nullable value type

C# – Can’t pass decimal parameter in DataTestMethod

I have a parameterized unit test with decimal parameters. When I run the test, I get the following exception: System.ArgumentException: Object of type ‘System.Double’ cannot be converted to type ‘System.Decimal’. Solution Change the parameters to doubles and convert them to decimals inside the test method. Why is it throwing an exception? You have to pass … Read more C# – Can’t pass decimal parameter in DataTestMethod

C# – Parameterized tests with MSTest v2

Here’s an example of how to parameterize your tests using the built-in MSTest v2 test framework: There are 3 steps: Add parameters to your test method. Use [DataTestMethod] instead of [TestMethod]. For each test case, add [DataRow(…)] to pass in the parameters for that test case. What parameters can you pass in? You pass in … Read more C# – Parameterized tests with MSTest v2

C# – How to make concurrent requests with HttpClient

The HttpClient class was designed to be used concurrently. It’s thread-safe and can handle multiple requests. You can fire off multiple requests from the same thread and await all of the responses, or fire off requests from multiple threads. No matter what the scenario, HttpClient was built to handle concurrent requests. To use HttpClient effectively … Read more C# – How to make concurrent requests with HttpClient

Event-driven .NET: use FileSystemWatcher instead of polling for new files

You can use FileSystemWatcher to get notified of file system changes, such as when a file is created. Here’s an example of reacting to when a JSON file is created: Compared to polling, this event-driven approach with FileSystemWatcher is more efficient and simpler. In the remainder of this article I’ll show an example of how … Read more Event-driven .NET: use FileSystemWatcher instead of polling for new files

C# – How to check if a type has a default constructor

A default constructor is a constructor that doesn’t have parameters. Therefore, to check if a type has a default constructor, you can use reflection to loop through the constructors and see if there are any with no parameters, like this: In this article I’ll show an example of loading types that implement a specific interface … Read more C# – How to check if a type has a default constructor