Introduction to ASP.NET Core part 3: the configuration file


In the previous post we looked at how dependencies and dependency injection work in .NET Core. New libraries can be downloaded to the solution in various ways: through the NuGet GUI, The NuGet package manager console, by editing project.json directly or letting Visual Studio figure out if a certain library could solve a missing reference problem. Dependency injection can be achieved through a built-in IoC – Inversion of Control container – where we register the abstractions and the corresponding dependencies in Startup.cs. We can give various lifetimes to the dependencies: transient, scoped and singleton. Then whenever a class requires a dependency the IoC container will serve up the registered concrete implementation automatically.

In this post we’ll see how to add a configuration file to the project and how to read from it.

Read more of this post

Divide an integer into groups with C# .NET

Imagine that we have an integer and we want to divide it into equal groups of another integer and put any remainder to the end of the group. E.g. if we want to divide 100 into groups/batches of at most 15 then we’ll have the following array of integers:

15, 15, 15, 15, 15, 10

The following C# function will perform exactly that function:

Read more of this post

Introduction to ASP.NET Core part 2: dependencies and dependency injection


In the previous post we started looking at the anatomy of an ASP.NET Core application. .NET Core is a cross-platform, highly modular and lightweight application framework that is meant to be the future of application development in .NET. We identified some significant changes compared to traditional ASP.NET applications that we saw before. There are a couple of new JSON files such as global.json and project.json. Global.asax.cs is gone and has been totally replaced by Startup.cs. There’s still a web.config file but it’s not used for the same kind of configuration as before. A different json file which we haven’t seen yet will be used for that purpose. The file structure is also different in that the source files are stored in a folder called “src” by default. Note that .NET Core is still in preview mode at the time of writing this series.

In this post we’ll look at adding dependencies and using them via dependency injection and service registration.

Read more of this post

Using the let keyword in .NET LINQ to store variables within a statement

It happens that we have a LINQ statement where we want to refer to partial results by variable names while expressing some computation. The “let” keyword lets us do that. Those who are familiar for the F# language already know that “let” is an important keyword to bind some value to a variable.

Suppose we have the following list of integers:

List<int> integers = new List<int>()
	5, 7, 4, 6, 10, 4, 6, 4, 5, 12

Read more of this post

Introduction to ASP.NET Core part 1: anatomy of an empty web project


ASP.NET Core is a brand new portable web framework by Microsoft. It is similar to the “normal” ASP.NET versions we’ve seen so far, such as ASP.NET 4.5. ASP.NET 4.5 targets the .NET 4.5 framework and conversely ASP.NET Core is primarily targeted at the .NET Core framework. However, it can also target the other .NET frameworks like .NET 4.5 or 4.6. It is currently in preview mode at the time of writing this post but developers are free to play around with it already now. It’s good to get familiar with the new features because .NET Core is meant to be the future direction of .NET development. There’s a number of significant changes compared to the earlier versions of ASP.NET that .NET developers will need to get used to.

Currently .NET Core is a subset of the full-blown .NET framework meaning that not all features of .NET are available in it. However, as the product matures the gap between .NET Core and full .NET should diminish. Eventually all new .NET projects should target .NET Core.

.NET Core marks a significant shift from Microsoft’s past policies and attitude of targeting Windows only and keeping as much as possible secret and paid. .NET Core is cross-platform and designed to run on multiple operating systems: Windows, Mac OS and Linux. It is also open-source, you can view its public GitHub repository here.

Read more of this post

How to compress and decompress files with GZip in .NET C#

You have probably seen compressed files with the “gz” extension. These are files that hold a single compressed file according to the GZIP specifications.

GZip files are represented by the GZipStream object in .NET. It’s important to note that the GZip format doesn’t support adding multiple files to the same .gz file. If you need to insert multiple files in a GZip file then you’ll need to create a “tar” file first which bundles the individual files and then compresses the tar file itself. The result will be a “.tar.gz” file. At present tar files are not supported in .NET. They are supported by the ICSharpCode SharpZipLib library available here. We’ll look at tar files in another post soon.

With that in mind let’s see how a single file can be gzipped:

Read more of this post

How to find various machine-level system information with C# .NET

The Environment class holds a range of properties that help you describe the system your app is running on. Here come some examples with inline comments:

//returns true on my PC as it is a 64-bit OS
bool is64BitOperatingSystem = Environment.Is64BitOperatingSystem;
//returns the machine name, in my case ANDRAS1
string machineName = Environment.MachineName;
//returns information about the operating system version, build, major, minor etc.
OperatingSystem os = Environment.OSVersion;
//returns the platform id as an enumeration, in my case it's Win32NT
PlatformID platform = os.Platform;			
//the currently installed service pack, Service Pack 1 in my case
string servicePack = os.ServicePack;
//the toString version of the OS, this is "Microsoft Windows NT 6.1.7601 Service Pack 1" on this PC
string version = os.VersionString;

//I have 4 processors on this PC
int processorCount = Environment.ProcessorCount;

//returns 2 logical drives: C: and D:
string[] logicalDrives = Environment.GetLogicalDrives();

//this is how to find all environmental variables of the system and iterate through them
IDictionary envVars = Environment.GetEnvironmentVariables();
foreach (string key in envVars.Keys)
	//e.g. the JAVA_HOME env.var is set to "C:\Progra~1\Java\jdk1.7.0_51\"
	Debug.WriteLine(string.Concat("key: ", key, ": ", envVars[key]));

//retrieve the current CLR version, in my case it's "4.0.30319.18444"
Version clrVersion = Environment.Version;

View all posts related to diagnostics here.


A great site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Love to use Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: