Joseph Michael Pesch
VP Programming

Angular 2 Compile Warning "Critical dependency: the request of a dependency is an expression"

by 28. February 2018 15:11

When compiling Angular 2 application I received compile warning "Critical dependency: the request of a dependency is an expression" when using the require() method with a variable vs. literal string value (see examples below).

 

This throws compile warning:

let path = 'path_to_item';

let item = require(path);

 

This does not throw compile warning:

let item = require('path_to_item');

 

Tags:

Angular

Angular Component StyleUrls Not Working

by 16. February 2018 15:08

Issue with component level styles not working, import "ViewEncapsulation: from @angular/core and add encapsulation: ViewEncapsoulation.None to the @Component decorator as shown below.

import { Component, OnInit, ViewEncapsulation } from '@angular/core';

@Component({

    selector: 'app-demo',

    templateUrl: './demo.component.html',

    styleUrls: ['./demo.component.css'],

    encapsulation: ViewEncapsulation.None

})

Tags:

Angular | JavaScript | TypeScript

JavaScript/TypeScript JSON Navigate by Dotted String Path

by 16. February 2018 15:00

Navigate a JSON object by a dotted string path (e.g. "somePath.subPath.anotherSubPath").

console.log('somePath.subPath.anotherSubPath'.split('.').reduce((o, i) => o[i], jsonObject));

Tags:

Angular | JavaScript | TypeScript

JavaScript/TypeScript Dotted String to Upper/Lower First Letter

by 16. February 2018 14:56

Take a dotted string (e.g. "someThing.someThingElse.another") and convert first letter of each dotted component to upper or lower case.

    dottedToUpper(str) {
        str = str.replace(/\./g, ' ');
        str = str.replace(/\w\S*/g, function (txt) {
            return txt.charAt(0).toUpperCase() + txt.substr(1);
        });
        return str.replace(/ /g, '.');
    }
    dottedToLower(str) {
        str = str.replace(/\./g, ' ');
        str = str.replace(/\w\S*/g, function (txt) {
            return txt.charAt(0).toLowerCase() + txt.substr(1);
        });
        return str.replace(/ /g, '.');
    }
 

Tags:

Angular | JavaScript | TypeScript

C# Name Tuple Items

by 1. February 2018 14:29

To name tuple items in C#, first install the NuGet package: Install-Package System.ValueTuple

Syntax: (string ItemName, DateTime ItemName, etc.) vs. Tuple<string, DateTime>

Tags:

ASP.Net | C#

Debug Windows Service Running as Interactive Console Application

by 1. February 2018 08:40

Before running in Visual Studio change the project output type from Windows Application to Console Application.  Below is sample Program.cs code to run interactively in Visual Studio.

using System;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;

namespace DemoWindowsService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] servicesToRun;
            servicesToRun = new ServiceBase[]
            {
                new DemoService()
            };
            if (Environment.UserInteractive) RunInteractive(servicesToRun);
            else ServiceBase.Run(servicesToRun);
        }

        static void RunInteractive(ServiceBase[] servicesToRun)
        {
            Console.WriteLine("Services running in interactive mode.");
            Console.WriteLine();

            MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart",
                BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (ServiceBase service in servicesToRun)
            {
                Console.Write("Starting {0}...", service.ServiceName);
                onStartMethod.Invoke(service, new object[] { new string[] { } });
                Console.Write("Started");
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(
                "Press any key to stop the services and end the process...");
            Console.ReadKey();
            Console.WriteLine();

            MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop",
                BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (ServiceBase service in servicesToRun)
            {
                Console.Write("Stopping {0}...", service.ServiceName);
                onStopMethod.Invoke(service, null);
                Console.WriteLine("Stopped");
            }

            Console.WriteLine("All services stopped.");
            // Keep the console alive for a few seconds to allow the user to see the message.
            Thread.Sleep(5000);
        }
    }
}

Tags:

ASP.Net | C# | Windows Service