The .NET Core platform has been growing in interest and popularity lately and is, as of this writing, on build 2.0. I wanted to share with you how to get started on an application a little more complicated than the simple "Hello World". In this series, I will continue to build onto this application from just creating a simple file watcher to adding in some Azure cognitive services, moving all the code into Service Fabric, and pushing the whole workload into Azure.

Why .NET Core?

Before we begin talking about the actual implementation of a .NET Core application, we must consider the why. Why should you consider moving your workload to .NET Core instead of the full .NET Framework? Unfortunately, the answer is: it depends. If you are working in a shop where you are not planning on putting any .NET code onto a box other than a Windows box, and your performance is great with the full .NET Framework, then great. There is no compelling reason for you to move to Core, other than it is new and shiny.

However, if you want to be able to move your code to different platforms such as Linux, or a macOS machine as well as a Windows machine, then .NET Core maybe is something worth looking at. Or if your current .NET stack is just overkill for an application and it is hurting your performance then .NET Core may be worth a look. This isn't a silver bullet to fix problems that exist with the .NET Framework. .NET Core is just another useful tool that can be leveraged in the right circumstances.

Getting Started

There are many good tutorials out there on how to get started with .NET Core using the command line and using an editor that may not be as heavy as Visual Studio. I am going to be using Visual Studio 2015 to create my project.

If you are keeping up with your Visual Studio updates you should have the .NET Core templates available to you when you start a new project. For this example, I am going to use the console application project template for .NET Core.

If you do not see the project types you can go out to the Microsoft site and download it there.

Once you have a new project created you are going to see a few differences in the project structure between a traditional .NET application and a .NET core application. There are two folders, solution items and src. src contains, as you would expect, your source code for your application. The solution items contains one file global.json. If you open that file you see a listing of the projects and the SDK version you are currently working on.

In the src folder you'll see your console application like you would expect, with one additional file project.json. Similar to global.json this file contains some metadata about your project including dependencies and versions of the .NET Framework.

From here we can write our program as you normally would.


namespace FileWatcher
{
 public class Program
 {
 public static void Main(string[] args)
 {

 string folderToWatch = @"C:\fileWatcher\dropFolder\";
 string archiveFolder = @"C:\fileWatcher\archive\";

 Console.WriteLine("Starting monitor of folder {0}", folderToWatch);

 int timeToSleep = 10000; //10 seconds

 if (!Directory.Exists(folderToWatch)) Console.WriteLine("Folder to watch does not exist {0}", folderToWatch);

 while (true)
 {
 int fileCount = Directory.GetFiles(folderToWatch).Count();

 if (fileCount == 0)
 {
 System.Threading.Thread.Sleep(timeToSleep);
 }
 else {
 var files = Directory.GetFiles(folderToWatch);
 foreach (var f in files) {
 var fileInfo = new FileInfo(f);

 Console.WriteLine("Found file {0} of size {1} bytes", fileInfo.FullName, fileInfo.Length);
 fileInfo.MoveTo(archiveFolder + fileInfo.Name);
 } 

 }
 }

 }
 }
}

I have written a simple console application that monitors a drop folder for any file. If it finds a file it will look at the size of the file print it to the console and then move it to an archive folder. The program itself isn't terribly interesting but let's look at what we get when we compile our application.

If you have followed other tutorials and found that when doing all the compilations for .NET Core from command line you don't get an exe like visual studio generates for you. This is just syntactic goodness. The .NET Core framework doesn't require an exe. To run our application from the console we would just run:


dotnet FileWatcher.dll

When I run this from my command line, my program starts to monitor the folder for any new contents and moves it to the archive folder.

Conclusion

If you had followed along this far you have made your very first .NET Core application. You may be thinking to yourself, well that didn't really feel any different than any other .NET application you have written. That is the whole point. Most of the .NET Framework is available in .NET Core and your developers can start coding a .NET Core application right away if it meets your workload, as I mentioned up above.

The next post in this series will start to do some more interesting things building on top of this application. We will look at calling Azure Cognitive Services in order to tell us how many faces are in a picture and any emotion associated with a face.