Saturday, August 13, 2016

Microsoft MVC6 New JSON Based Configuration Tips

I have been away from the MVC world for 2 years, and just started a brand new project with it today. Things have changed quite a bit from MVC4 days. Now there is no Web.config in the base project template for Visual Studio.

After starting to learn about this change, which should be positive and there are good reasons for this change, I have found out, as usual, all the examples out there are outdated from RC times or not complete.

Here are some things I ran into that you would also run into.

You Will Need to Install Microsoft.Extensions.Options and also Microsoft.Extensions.Options.ConfigurationExtensions

If you do not do this, you will see

'Microsoft.Extensions.Configuration.IConfigurationRoot' to 'System.Action<CustomSettings>'  error.

They are available via NuGet, Just look for Microsoft.Extensions.Options and both should come up.

Where to Put My Option Plain C# Object (POCO)?

I would put that under your Models folder (create one under the root if needed), but it can be anywhere, perhaps Confgs subfolder, if you make one would just be OK too.

You Should Be Using PowerShell, Here are the Bare Minimums

I see most people avoiding PowerShell. I say if you just learn a few essential usage, it is a much easier environment to use, especially switching between MacOS and Windows.


Go to the start menu and type in powershell. If you frequent your Windows environment, I would "pin" this to Start or the Taskbar, and also start avoiding Cmd.

Some Basic Coolness

If you are going back and forth between IOS and Windows, it would be a bit more comfortable to use the PowerShell as it comes "out of the box" without the need for installing the "bash" shell and many of the Unix like commands are supported as "alias".

  • Like on the Unix shell you no longer have to type in /Users/myname/<wheatever> instead you'd be like in the familiar Unix environment like..
  • .
    • cd ~  works auend gret ysou tyoo /ura/<ccount>
    • cd ~/Downloads works!
    • cd~/Desktop works
    • ls works (this is an alias so cannot go too fancy like ls -lR)
    • From there if you do "open ." then you can open the Explorer at that directory.
  • You can now Ctrl-C and Ctrl-V if you just enable them. Right click the title bar of the PowerShell window for the Properties. Then open the Options menu and look for "Enable Ctrl key shortcuts." This was added in Windows 8

Some Peculiar Stuff

  • If you want to see environment variables, use "printenv"
  • One issue with environment variable. For example, let's say you want to use $JAVA_HOME as in MacOS, then you need to do $env:JAVA_HOME   I don't really like this, but so there

The Basic Idea

Like most anything, understanding the architecture and the basic philosophy of the design is the key to understanding whatever you are running into.

In terms of PowerShell, they tried to standardize basically on the REST type API Call style. So this is why they have commands like Get-This or Set-That.  So if you master those, then you can chain them together like you do with Unix pipes with a much more consistent "schema" they set up. 

The "help" is quite extensive and comprehensive as a result, so if you type in just about any phrase you think about, as "help something", it is a good way to move forward with more stuff.

Windows Git/GitHub Results in "Error Repository Not Found" or Denied Public Key Solution

I spent a good chunk of a day to resolve issues related to "Error Repository Not Found" or even got Permission denied (publickey) type error messages.

This issue happened suddenly on my Git environment integrated with IntelliJ Ultimate. While investigating this, I made things even worse and Git was totally denied including using SSH.

Along the way, I also tried Two Factor authentication on GitHub, and eventually got that to work both in IntelliJ and also with SourceTree.

The following sequence of events finally have fixed the issue.

If You've Tried These Already, then You Have the Same Problem as I Had.

  • Instead of trying you tried Note that this would not work if your Windows Credentials already has cached your user name and password.
  • You tried to change to the SSH URL
  • You've even tried to generate a new SSH key
  • You've even loaded the SSH key via pagent.exe
    • Note that SSH issue is a separate issue, so in this article I will stick with getting you back up and going with https (including using the Two-Factor authentication.)

1. Check the Windows Credentials to Remove Any Git Related

  • Open the Start menu and type in "Credential Manager"  This will open the Windows Credential Manager. This is basically the same thing as "Mac OS Keychain" in fact, if you are on the Mac, you would want to check there. Check and remove all Git related credentials, this is the most likely cause if you see https:// Repository not Found issue. 
  • Now try your Git access and see this will issue a new username and password prompt.

2. Last Resort: Completely Remove Your Git and Re-Install it Directly

  • Be sure you have done Step 1.
  • For this does not use chocolaty. If you did already, first try the uninstall via "choco uninstall git".
  • Go to the Program and Features and remove any installations of Git
  • Go to C:\Program Files\Git or likewise and manually delete all remnants of Git
  • Download and install from the Git Web Site. 
  • Check your~/.gitconfig file to make sure nothing strange is there. Also, I would check in your local Git repos./.git/config file for URLs pointing to the repo.  (Note, you can type in cd ~ if you are using PowerShell, if anything else, that's one big attraction of using PowerShell to be able to cd to ~/Downloads and ~/Desktop)  give it a shot I will post an article about PowerShell Essential Survival Guide next.
If You Have Activated Two-Factor Authentication...

The password field you provide should not be your interactive login password, but you should generate a Personal Access Token on your GitHub account  Note that you only see the token once after you generate it, so have a Notepad ready to temporarily copy it.

Next launch your Git UI app like SourceTree and do some operation like Fetch, this should prompt you again for the password. Use the token you have just copied in the password field and let the credential to save. This will wind up in the Windows Credentials Manager so next time you change this, you would want to do Step 1 as above.