Adventures on the edge

Learning new development technologies
    "When living on the bleeding edge - you sometimes have to bleed" -BillKrat

Adding Dependency Injection (Autofac) to ASP.NET MVC Core 2

Where the built-in Inversion Of Control (IOC), aka Dependency Injection (DI), container of ASP.NET MVC will meet most needs, sometimes we’ll want to utilize more advanced features of alternate IOC containers.  In the example below we are configuring for the use of Autofac (line 56), using a pattern consistent with existing code – an extension method.


The extension method AddAutofacProvider code follows below:

Note on line 13 above that we have an optional “builderCallback” action.  This provides the Startup ConfigureService method access to the builder instance created above on line 16.   Below on lines 56-60 is an example of how it might be used:

Source code follows:

public static IServiceProvider AddAutofacProvider(
     this IServiceCollection services,
     Action<ContainerBuilder> builderCallback = null)
     // Instantiate the Autofac builder
     var builder = new ContainerBuilder();

    // If there is a callback use it for registrations

    // Populate the Autofac container with services

    // Create a new container with component registrations
     var container = builder.Build();
     var provider = new AutofacServiceProvider(container);

    // When application stops then dispose container
         .ApplicationStopped.Register(() => container.Dispose());

    // Return the provider
     return provider;

Configuring multiple environments with ASP.NET MVC Core 2

Source code available: HERE

With ASP.NET MVC Core 2, much of the configuration work is already done for us; it is configured for appsettings.{Environment}.json right out of the box.  Line 29 is all you’ll require for the Startup method.


I added the following extension method (line 53 above) to simplify my ConfigureService code [being consistent with existing code]:

public static void AddAppSettings(this IServiceCollection services, IConfiguration configuration)
     var section = configuration.GetSection(nameof(AppSettings));

My appsettings.development.json configuration file has a section “AppSettings” which contains one setting [as of this writing] it is “Environment”.   For consistency I created a AppSettings class that has a property name of Environment as required by the above “services.Configure<AppSettings>(section)” process; it will populate all AppSettings properties of the same name of the settings within the configuration file.

If you examine the Property page of your project you’ll find that the ASPNETCORE_ENVIRONMENT variable has already been set and defaults to “Development” – this will result in the appsettings.development.json file being used.   Since there are no other configuration files if you were to use any other name, e.g., “Production” it will default to the appsettings.json file.


All that remains is to use constructor injection to retrieve the configuration settings.   With default usage [out-of-the-box] you can use the “IOptions<AppSettings> settings” parameter as shown on line 29 of the image below.  To retrieve an instance of AppSettings you’ll need to use the Value property of settings (reference line 33 below).   However, there is more information available [by default] that I want to have available to my application, so I also registered an “IAppSettings appSettings” (line 30) implementation that will provide all of the environment variables as well (see red arrow for AppSettings).  Note: I provide both methods for demonstration purposes only, I will only be using IAppSettings in my code.


So if your only interested in the out-of-the-box usage, you are done here.  If you are interested in how to have the environment variables (in the case above 83 of them), please read on.

In the Startup class I not only modified ConfigureServices (as shown above) but also made an update to the Configure process as shown in bold below:


The UseAppsettings extension method (line 19 below) uses the service provider to obtain applicable services and then calls the AppSetting class Initialize method with an event argument that contains services.


The Initialize method above uses the event arguments (services) to retrieve and populate the ‘Values” property with the available settings.

Shift key stops working for Remote Desktop

Parallels is an excellent program for using an IMac for both Apple and PC development (more below), the only issue I encountered to date is that when I remote into my Slate [to develop in Visual Studio 2017] I lost the shift key functionality.
Adventures on the edge

Oddly the CAPS lock key worked but this was not a practical work-around.   I found that if I accessed the Control Center (#1-2 in image below), changed the Keyboard setting to “Optimize for games”, that the issue was resolved – the clue was the message below this dropdown setting.

Screen Shot 2017-12-08 at 7.55.36 AM

Where the IMac was purchased so I could develop in the Apple and Android environment (and my wife could use it for her business), I was pleasantly surprised to find out that if I went into “Full Screen” mode (#2 below) that I essentially had a full Windows experience with perks, e.g., I had downloaded an .rar file thinking I would have to find a utility to unpack it.  However, when I double-clicked the file it opened it while in my Windows environment using the Mac OS – very impressive.   “Coherence” is another impressive feature (#1 below), if you select this mode then your Windows applications will open under the Apple OS environment and I could access it via the Windows 10 environment (shared folder space).  With Coherence Windows and Apple applications open side-by-side as if the Windows application was native to it; it was odd seeing Visual Studio 2015 opening under the Mac OS.  

My personal preference is “Full Screen” mode as I can simply hold the control key and press the left and right arrows to switch between environments.  

Screen Shot 2017-12-09 at 5.45.54 AM

let and var in TypeScript

The advantage of the “let” keyword is that you cannot use a variable before it has been defined (safer).  In contrast “var” does not have to be defined.  The following will compile:

console.log('not defined = ' + notDefinedIsLegal);
var notDefinedIsLegal;

Note in image below [underlined in red] that “let” will complain "error TS2448: Block-scoped variable 'mustBeDefinedBeforeUse' used before its declaration"


In addition, "let" scopes to blocks. Below I define both variables in a block and the definition for the let is limited to the block of code it resides in - this is not the case with var as the output will demonstrate.

console.log('not defined = ' + notDefinedIsLegal);
var notDefinedIsLegal : string;

let mustBeDefinedBeforeUse = 'Hello World';
console.log('mustBeDefined = '+ mustBeDefinedBeforeUse);

    var notDefinedIsLegal = "legal!";
    let mustBeDefinedBeforeUse = "Goodbye World!!";
    console.log('[blockscoped] mustBeDefined = '+ mustBeDefinedBeforeUse);
    console.log('[blockscoped] notDefinedIsLegal = ' + notDefinedIsLegal);

console.log('mustBeDefined = '+ mustBeDefinedBeforeUse);
console.log('notDefinedIsLegal = '+ notDefinedIsLegal);

results in the following output:



Easy steps to installing Node, NPM, TypeScript, and Bower in Windows

1. to install The package manager for Windows – click “Install Chocolatey Now” button.

2. Copy the cmd.exe or PowerShell.exe command line [following instructions] as applicable.

3. After installation is complete, exit and launch command prompt (CMD.EXE) – npm will now be available.

    a. npm install – g typescript

    b. npm install –g bower

More on