2017-10-04

Webpack has been initialised using a configuration object that does not match the API schema.

On Windows, when running yarn (custom action "start") to start the dev server, on the step that starts the webpack-dev-server, I had the following error message:

$ webpack-dev-server --debug --hot --progress --colors
 10% building modules 2/2 modules 0 active
 Invalid configuration object. 
 Webpack has been initialised using a configuration object that does not match the API schema.
 ... 

This was not an easy one to understand, but it turned out to be very simple.

When I had CD-ed into the React app folder, I had typed the foldername with only lower case characters. The foldername was originally typed with a capital first letter. Webpack did not like that.

Solution:
cd ..
cd Folder
yarn start


2017-09-11

Adding nicer icons to the PageTree in EPiServer

I did some research about adding nicer icons for pages in the EPiServer Page Tree, and I came up with some nice resources.

What seems to be common for all approaches is that you must create css-classes that encapsule each icon to use. At the moment I don't see how to get around this without actually creating the classes.

You also need to create an InitializableModule which sets up the icons when the web is started. I have omitted that code here. Please read the example from the first example below (from blog.nansen.com), for that code.

This example is complete and shows how to use your own (or a third party lib) icons:
http://blog.nansen.com/2014/10/page-tree-icons-in-episerver-cms-75.html

This example shows how to create your own icons (as an image sprite) and use for the icons: https://jonika.nu/JonasBlogg/archives/347


Here's one that shows how to use the already included icons from EPiServer:
https://ericceric.ghost.io/use-episervers-content-icons-as-site-tree-icons/

In stead of creating and maintaining the list of Episerver icons yourself, as in the example above, you could go for the Jon D Jones nuget package: https://www.nuget.org/packages/JonDJones.IconPack/. This iconpack does not really contain any icons, just convenience constants that point to the built-in episerver icons (it seems so to me anyway).


I created a generic attribute, based on reflection, that plugs in the JonDJones icon pack (download from Nuget, as mentioned) into the first example I mentioned from the "nansenblog":

[AttributeUsage(AttributeTargets.Class)]
public class ContentIconAttribute : Attribute
{
    public ContentIconAttribute(Type type, string propertyName)
    {
        IconClass = GetStaticStringValueFromLib(type, propertyName);
    }

    private string GetStaticStringValueFromLib(Type libType, string name)
    {
        var fieldInfo = libType.GetField(name);
        var propInfo = libType.GetProperty(name);
        var value = (fieldInfo?.GetValue(null) ?? propInfo?.GetValue(null)) as string;
        return value;
    }

    /// 
    /// Css class to apply to the icon
    /// 
    public string IconClass { get; set; }
}

And here's how to use it:
using JonDJones.IconPack;

[ContentIcon(typeof(ObjectIcons), nameof(ObjectIcons.Start))]
public class StartPageModel : BasePage
{
    ...
}

2017-06-13

Always run Visual Studio as administrator

I am using Visual Studio a lot, and when I pin it to the task bar in Windows 10, right-clicking it will bring up a (popup) list of my last opened projects.

I need to run VS as Administrator, to be able to debug my web applications running on the local IIS.

So when I right-click the pinned VS-icon on the task bar, I can right-click on Visual Studio, then select "Run As Administrator", which is fine. The problem is that I cannot right-click on any of my previously opened solutions and run VS as admin while opening that solution.

On a shortcut (and probably also some exe-files) you will get the option to "always run as administrator". Problem is, this option does not appear when looking at the properties of the DevEnv.exe file, so you need to "fake" it by going into the compatibility settings.

In essense: What I want to do is basically to always open Visual Studio "As Administrator".

So here's how:

From link 1 below:
  1. Locate devenv.exe, by right-clicking the VS icon and selecting properties, then see the link in the shortcut.
  2. Right-click devenv.exe and choose Troubleshoot compatibility
  3. In the new window click at Troubleshoot Program
  4. Check The program requires additional permissions
  5. Click "Next" until you get to a button that says "Test the application". 
  6. Click the button and VS will be launched "As Administrator". 
  7. Switch back to the dialog, press "Next".
  8. Click the top option to save the preferences.

Sources:
  1. https://superuser.com/questions/465065/no-compatibility-tab-for-devenv-exe-vs-2010-and-vs-2012-on-windows-8
Related:


2017-04-19

Get time for given country

This code gets the first time zone for a country (meaning it probably won't work if your country has more than one), and calculates the date/time given the UTC date/time.

The method is an extension to the DateTime type, meaning you can use it by typing .AsLocalTimeForGivenCountryFromUtc(countryCode) after a variable or function returning that type. The countryCode parameter is a string, and must be a two-letter ISO 3166-1 country code.

You need to import John Skeets NodaTime library from NuGet.

Code:
using NodaTime.TimeZones;
using System;
using System.Linq;

namespace MyNameSpace
{

    public static class DateTimeExtension
    {

        public static DateTime AsLocalTimeForGivenCountryFromUtc(this DateTime utcDateTime, string countryCode)
        {
            //no matter what tz info is on the date, assume it is UTC
            var dte = new DateTime(utcDateTime.Year, utcDateTime.Month, utcDateTime.Day, utcDateTime.Hour, utcDateTime.Minute, utcDateTime.Second, DateTimeKind.Utc);
            var tzMapping = TzdbDateTimeZoneSource.Default.WindowsMapping.MapZones.FirstOrDefault(t => t.Territory == countryCode); //NodaTime
            var tz = TimeZoneInfo.FindSystemTimeZoneById(tzMapping.WindowsId);
            return TimeZoneInfo.ConvertTimeFromUtc(dte, tz);
        }
    }
}

2017-04-06

Visual Studio 2015 Freezes when ReSharper is running

Ok, so finally got ReSharper installed. Again. Been doing fine without it for a while, but the team is using it so I got myself a license. But it was a bit of a disappointment at first. It seems that whenever I build my solution, which has both some C# and one F# project, VS freezes. Today it even crashed after being unresponsive for quite a few minutes. Seems more people have the same problem:

https://resharper-support.jetbrains.com/hc/en-us/articles/206546149-Visual-Studio-with-ReSharper-is-freezing-and-or-crashing?flash_digest=fcf1e14381f3c8171cbc9129d0fa536af24a7a10

So what happened was that I was in my F# project making some changes, then I built the solution, and then I went on to do some more changes in the same project, but things were moving very slowly, and in the end froze completely and then the crash.

So my working theory is that the problem may be related to F#. So I have added the F# project folder to the exclude list in R# options. I also added the *.fs file type to the excluded files.

So far it's looking good. I have built several times, and editing F# code is smooth.

PS. Still looking good! :) Think I found the solution!

PPS. Also disabling R# for TypeScript files may be a good idea (or so I've heard).

2017-04-03

Things that may be confusing when moving to F#

I have been doing C# for quite a few years, and I have started using F# recently. I am only occasionally coding in F# so I tend to forget some things from one time to the other. So here's what I must remember:
  • The sequence of F# code files in Visual Studio matters!
    • To have access to a module or type from another module, that module must be declared before (higher up) the other module.
    • To move a module up, use Alt + ArrowUp.
  •  Indentation matters!
    • If some code is part of some enclosing element, that code must be indented.
Here's a great article:
http://connelhooley.uk/blog/2017/04/10/f-sharp-guide

2017-01-13

Free Code Improvement Alternatives

I really liked using ReSharper (JetBrains) in Microsoft Visual Studio.

There are also some other commercial tools like it:
- CodeRush (DevExpress)
- JustCode (Telerik)

The functionality that I found most useful was:
- Go To Implementation
- Find Usages
- Suggestions on code improvements
- Extract function

I think that ReSharper has become too expensive now, currently at a $299 first year subscription, then a bit lower on subsequent years. That's about 40% of the price of Visual Studio Professional, and you don't even buy the product - you subscribe.

So I am testing out this combo as an alternative:

Roslyn provides the Light Bulb for code improvements:
https://blog.tommyparnell.com/vs-2015-getting-resharper-experiance-without-resharper/

Several very handy tools:
- Productivity Power Tools

For more code improvement suggestions (pick one, not both):
- CodeCracker for C# (testing this now, looks good)
- Refactoring Essentials for Visual Studio*

I have also tested CodeMaid, but not just right now*.
I may enable it again at a later point in time though.
It looks like a very useful addition to Visual Studio.

Here are some related Stack Overflow questions:
http://stackoverflow.com/questions/2834439/what-are-some-alternatives-to-resharper
http://stackoverflow.com/questions/24542406/alternatives-to-resharper

*Enabling too many competing plugins may cause VS to run very slowly.