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: Enter a service endpoint: Select Settings and enter a name: Click Finish. … 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

Write to Text File in C#

First, create a new console app: Next, add: using System.IO; Now add code: using (StreamWriter writer = new StreamWriter(“filename.txt”)) { writer.Write(“1”); writer.WriteLine(“2”); writer.WriteLine(“3”); writer.Write(“4”); } Now go to the directory you ran the code from. You will see the filename.txt file created: This code can also be written as: StreamWriter writer = new StreamWriter(“filename.txt”); writer.Write(“1”); writer.WriteLine(“2”); writer.WriteLine(“3”); writer.Write(“4”); writer.Flush(); writer.Close();  

Installing and Using ILSpy

ILSpy is an assembly browser and decompiler. Here we will go through installing and using ILSpy. First, download the binaries from the ILSpy website: This will download a ZIP file. Extract the file: Run the ISpy.exe: Click to open a file: Here I have selected an exe compiled from code I previously wrote. You can see we can now see the decompiled code within ILSpy:  

Serializing an Object in C#

Serialization is the process of converting an object into a byte stream. Once serialized, it can be transmitted to a file, a database or to memory or send it across a network. For example, you may want to convert data to XML. Serialization allows you to store the state of the object; you can then recreate it as needed. Let’s go through a simple example of serializing a Car object. … Continue reading Serializing an Object in C#

Abstract Classes in C#

Abstract classes are a type of class in C# that contains a base class implementation. The abstract class is not instantiated, and are inherited by subclasses to provide a detailed implementation. Consider the example below. We have an abtract class called Vehicle in our console app. We try to create an instance of the abtract class Vehicle: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Carl.AbstractClass { … Continue reading Abstract Classes in C#