HttpClient GetAsync, PostAsync, SendAsync in C#

HttpClient is a library in the Microsoft .NET framework 4+ that is used for GET and POST requests. Let’s go through a simple example of using HttpClient to GET and POST JSON from a web application. First, we will create our client application. We will create a new console app in Visual Studio: Add the System.Net.Http namespace. We will pull down JSON data from a REST service: Now, to read this, we … Continue reading HttpClient GetAsync, PostAsync, SendAsync in C#

How to Remove Duplicates from a C# List

To remove duplicates from a C# list using Linq, do the following. Define a new list. Ours has 7 elements, with “2” and “4” repeating Use Distinct().ToList() to make a new list with the distinct elements Print out to the console This outputs:  

C# – Await and Async

In a previous post, we looked at the Task Parallel Library. In this post we will look at Await and Async in C#. In our previous example, when clicking a button we will wait 5 seconds, then display a message to the user, and without locking up the UI. We will tell our function that it will run asynchronously using the async keyword, and use await to tell the compiler … Continue reading C# – Await and Async

Write to Windows Event Viewer from C#

First, create a new console app: Next, add: using System.Diagnostics; Notice the Event Viewer in Windows has several areas: Windows Logs, which are: Application Security Setup System Forwarded Events And within each log, there are: Keywords Date and Time Source Event Id Task Category To write to the application log, use the code: static void Main(string[] args) { string Event = “Application has started”; using (EventLog eventLog = new EventLog(“Application”)) … Continue reading Write to Windows Event Viewer from C#

OData Connected Service in Visual Studio 2017

Here we will use the OData Connected Service to connect to an OData feed from Visual Studio 2017. First, install the extension. Select Tools->Extensions and Updates: Select Online and search for OData Connected Service: Restart Visual Studio to complete the install. Create a new Visual Studio console app: Right Click and select Add->Connected Service: Select OData Connected Service: We will connect to: The service looks like: Enter the service … Continue reading OData Connected Service in Visual Studio 2017

OData Client Code Generator

The OData v4 Client Code Generator is an extension that can be used to generate OData code for use in applications. To install it, open Visual Studio 2015 and under Visual Studio Gallery, search for OData Client Code Generator: Click to Install: The extension is now installed: To use the extension, we will create a new Windows Console project: Add a new item and under Code select OData Client: Files … Continue reading OData Client Code Generator

Create a WCF Windows Service

Here we will create a WCF web service hosted as a windows service. We will go through step by step how to do this. First, create a new WCF Service Application: This will create the project: If we select the Service1.svc page and press F5 to run this, it will open the WCF Test Client. From here, we can select the GetData() method and enter a value. Invoking the method will … Continue reading Create a WCF Windows Service

Dapper ORM C# Application

Dapper is a lightweight ORM. Here we will go through an example of connecting Dapper to SQL Server in a C# application. First, we will create a new Windows Console App: Go to Tools, NuGet Package Manager: Search for Dapper and select Install: You will see the following output: Next, create a class for the database object you will be connecting to. In our case, we will connect to the … Continue reading Dapper ORM C# Application

Introduction to Entity Framework

Microsoft’s Entity Framework is an Object Relational Mapping (ORM) framework for ADO.NET. Let’s go through an example of how to use Entity Framework. First, we will create a new Console application project: Go to Manage NuGet Packages for Solution: And select EntityFramework: Select the project and Install: Click OK and Accept: You will see the following messages: Once complete, you will see these new references in your project: We can … Continue reading Introduction to Entity Framework

C# – Task Parallel Library

In C#, there is a feature called Task Parallel Library that was introduced in .NET 4.0 and is useful for handling asynchronous calls. Let’s create a new C# Windows Forms application. Windows forms apps are good to demo this as they can get locked up with long running synchronous calls: Add a button to the Win Form: Let’s say we want to simulate some long running code. We will use … Continue reading C# – Task Parallel Library