Monday, 25 November 2013

Targeting a Specific .NET Runtime using the Startup Settings Schema

This post highlights the power of the startup element in an app.config or web.config file. It is a must-know knowledge for any .NET aficionado.

The startup settings in an app.config or web.config file specify which CLR should be used to run the system.

The aforementioned settings appear in an element called simply startup. Within startup, we have two possible entries, one of which is deprecated.

The element includes the supportedRuntime and requiredRuntime elements. Each element has a version and SKU (from "stock keeping unit" in inventory management). The requiredRuntime is actually, to all intents and purposes, deprecated, and not meant to be used for .NET versions other than 1.0.

Note that if you have a configSections element in your configuration, this will need to come before the startup settings element.

Sometimes, you may see the startup element even at the end of a config file!

More rules regarding the configuration file schema can be found here. Another useful article oriented to .NET 4.5 development is the following.

Meet the "DAR-th Vader" of Entity Framework which tries to Dynamically Load All Assemblies

This is done via the EDM.DefaultAssemblyResolver when executing GetAllDiscoverableAssemblies. The EDM.DefaultAssemblyResolver (aka the "DAR") can also be likened to the Gollum in Lord of the Rings, at least as far as assemblies are concerned, using an "inspect-only" bind.

Some other useful methods to know in this process:

EntityConnection->GetMetadataWorkspace; returns a "metadata workspace" (or MW). The MW is created when an EntityConnection is created. Metadata is then loaded into memory and used by the EDM application to access information about the underlying data source. When GetMetadataWorkspace is called, the metadata is "locked". The docs for System.Data.Objects say that the MW is not CLS-compliant so will likely not be compatible across .NET languages.

Unfortunately, this metadata workspace sometimes seems to contain irrelevant information for the purposes of data connection.

Troubleshooting .NET Deployment - How the .NET Runtime Locates Assemblies

This is a summary of the msdn article focusing on deployment of .NET applications. To quote directly (capitalisation for emphasis):  "to SUCCESSFULLY deploy your .NET Framework application, you MUST understand HOW the CLR locates and BINDS to the assemblies that make up your application".

Additionally, the runtime, "BY DEFAULT, attempts to bind to the EXACT VERSION of an assembly that the application was BUILT WITH". It then adds that this behaviour can be overwritten in configuration settings.


JavaScript, CSS3 and HTML5 Could be the New Silverlight Slash De-Facto RIA Replacement

Critics say HTML5 will destroy Silverlight but this is only a half-truth.

Many Silverlight aficionados will not change the good habits they have learned writing MVVM applications for the web, and may instead adapt their skills to new JavaScript/HTML5 frameworks that borrow style and structure from the Silverlight world. So Silverlight will still live, even when all the Silverlight applications are gone.

Now the web developer must face the necessity to be familiar with at least one JavaScript framework such as Angular or Knockout. JavaScript graphics libraries such as Raphael and Bonsai, too, will become useful.

As an example of "Silverlight-similarity" amongst these frameworks, read about how data binding is done in Angular, to transform HTML and make it more XAML-istic.

Reflection-Only Load Context

Loading an assembly into a reflection-only load context, means loading it for inspection rather than execution (according to MSDN).  The load can be performed on a string, revealing the FQDN of the assembly, or an array of bytes, in the underlying COFF-format.

ReflectionOnlyLoad(string)
ReflectionOnlyLoad(byte[])

To load for execution, it must be loaded into an execution context. Note that dependencies are not automatically loaded in Reflection-only load.

Sunday, 24 November 2013

The MSIL Assembler - ilasm

The MSIL Assembler compiles IL code into a portable executable (PE) file. Its usage is as follows:

ilasm [options] filename

The options include flags for specific processor architectures e.g. /arm for ARM CPU, /itanium for Intel Itanium and /x64 for 64-bit AMD processor. Assembly of IL is essentially what a JIT compiler, such as csc, does.

Saturday, 23 November 2013

An Alternative to Threads: Tasks in Task Parallel Library

.NET 4 introduces an alternative to Threads, known as Tasks, which is part of mscorlib. This is the new and preferred way to do asynchronous programming in .NET.

Task factories are the new tool of choice to create Tasks (the new incarnation of Threads) and start them immediately. They can also be used to create task continuations. A continuation task is a task that invokes another task on completion. The task that completes before the next task is called is known as the antecedent.

A task can be created using the syntax:  Task.Factory.StartNew( () => ...).

The above syntax says we must supply a delegate as a lambda expression a.k.a. anonymous function, or object of type Action, to the StartNew method of the Factory object (default instance of TaskFactory).

The StartNew method returns an object of type Task.  Task.WaitAll takes a list of tasks and blocks until all the tasks specified have completed. It can be called with an optional extra argument, specifying the wating time timeout in milliseconds (stored as an Int32, implemented as a struct in mscorlib, though you already knew that).

Because now we are talking about tasks not threads, we can no longer talk about multithreading, and to talk about multitasking seems rather an over-use of the term. Therefore, we need a new term, and for that we have been given the new conjunction, Task parallelism. 

Friday, 22 November 2013

Asynchronous Programming in a Pre and Post Dot Net 4 World

Dot net 4 recommends using Tasks over Threads. But what vestiges of the pre-dot net four programming universe retain their relevance in the "dot-net four and above" programming world? For that we must turn to the interfaces of System.Threading.

IAsyncResult emerged as early as Dot net 1.1 in Visual Basic, C# and Managed C++, literally as the "return type of methods that implement an asynchronous operation". An IAsyncResult has a property AsyncState that contains information about the asynchronous operation. The boolean IsCompleted indicates whether the operation has completed.

gacutil

What TWO caches is the gacutil useful for exploring?

gacutil is useful for two things - viewing and manipulating the Global Assembly Cache, a repository for .NET shared libraries, and the Download Cache, which downloads "managed objects" referenced in the HTML object element in web pages accessed from IE.

Where is the gac located?

In the pre-Dot Net 4 world, the gac was located, by default, in %windir%\assembly.  In Dot Net 4 onwards, this moved to %windir%\Microsoft.NET\assembly.

What BASIC commands can be done using gacutil?

gacutil /l  -- list contents of GAC
gacutil /ldl  -- list contents of download cache

Wednesday, 13 November 2013

The Giga in Gigabyte and Gigahertz

From the Greek meaning Giant

Giga comes from the Greek word meaning "giant".

A Billion, A Thousand Million, or Ten to the Power Nine

It's a prefix in the metric system representing a "thousand million" or more colloquially, a "billion", or ten to the power nine, or ten to the power ten minus one. It was used in the film Back to the Future ("Wait a minute, Doc. Are you telling me you built a time machine..out of a Delorean?").

The Giga in CPU Clock Rates

Say, a CPU has a clock rate of 2 GHz. It means it has a clock rate of 2 billion Hertz. This is the "frequency" of the oscillator crystal (crystal oscillators produce sine wave signals with very precise frequency).

The Giga in Computer Speed

One Gigaflop is one billion floating point operations a second.

What's next after Giga?

After Giga, the next notch is Tera. A tera is ten to the power twelve or a million million a.k.a. a trillion. It has the symbol T. It is derived from the Greek teras meaning "monster".

Windows Kernel Memory Usage on a 32-bit Vista box vs. 64-bit Windows 7 box

In the 32-bit world, address space discipline is paramount. Vista dedicates 2GB of RAM to the Windows kernel, or fifty percent of the total virtual address space given to a 32 bit program (owing to pointers' addressing constraints). The other half is used by the thread currently running on the CPU. In the 64 bit world, kernel implementers can play more fast and loose with memory.

Dude, where's my RAM? Hint: Ask SuperFetch

The  RAM-guzzling nostrum known as SuperFetch, which some refer to a "kernel enhancement", made its Windows debut in Vista in 2007. Basically its concept is to cache frequently used applications or application data in memory to reduce boot time. Turn it off with services.msc - it's meant to speed things up, but instead can slow things down.

Tuesday, 5 November 2013

.NET 4 Security Model Acknowledges Mistakes were Made with CAS

.NET 4 changed *many* aspects of the .NET security model. One important change was the reducing the complexity of Code Access Security (CAS) by shifting responsibilities from the runtime to the developers. This certainly impacted code using Assembly.Load in System.Reflection (thank you mscorlib!)

The .NET Security Blog is definitely worth revisiting from time to time to get the latest download on .NET Security design decisions.

Saturday, 2 November 2013

WoW64

WoW64 (Windows-on-Windows 64 bit) allows you to launch 32-bit applications on all the 64-bit Windows versions with approximately 2% performance loss in terms of speed.

What was new in Windows 7?

Windows 7 may be old news now but it is worth recollecting what was new in this landmark version of the OS. First the basics:
  • Windows 7 is the successor to Vista, also known as Windows 6. 
  • Businesses are offered Professional, Ultimate and Enterprise versions on Windows 7. 
New features in Windows 7.
  • BitLocker comes with Windows 7 and is designed to protect hard drives from accidental loss or theft by encrypting the hard drive; it is available in Ultimate and Enterprise.
  • DirectAccess that allows you to work on Sharepoint documents on a remote computer without the traditional hassles of VPN.
Windows 7 has 32-bit (x86) and 64-bit (x64) builds and many organizations have chosen to deploy 64-bit builds for network future-proofing.

The AppData Directory in Windows and How to Keep it Clean

The AppData directory is Windows is a dumping ground for applications to store data. You can access it from cmd.exe using %appdata%.  Sometimes stuff you have uninstalled leaves stuff here so review it periodically and clean it up.

The AppData folder uses three locations for settings data:
  • Local
  • LocalLow
  • Roaming
Roaming profiles are typically (not always) copied from a central server to the Roaming subdirectory in AppData. Local is for local profiles, and LocalLow is for less trusted applications running on local profile. An example of something which might be in LocalLow includes JREs, Apple QuickTime and Adobe Acrobat.

The annoying thing is that the AppData directory is typically hidden on Windows, and even once you've located it, there are many hidden subdirectories.

Media files take up the most memory. Here are some typical offenders:
  • FLVService is one application that can rapidly occupy space in AppData.
  • AppData\Local\Microsoft\Media Player\Transcoded Files Cache: you'll be surprised how many tmp files get created here