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.

Launching

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 https://gihub.com... you tried https://myname@github.com... 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 https://github.com/settings/tokens  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.

Tuesday, July 19, 2016

Fix: IntelliJ Ultimate 2016.2 Play Framework Views.html Resolution Issue (Again!)

Symptom:

You've just upgraded to IntelliJ 2016.2 and your existing Play Framework no longer resolves your Views.html.* references.

Note that this is different from the Route issue which requires you to add the Source dependency from target/scala-2.11/routes/main (which refers to the set of auto-generated Java sources).

Solution for Resolving Views:

I have solved this issue by going to File => Project Structure and then specifying the folder dependency to the target/scala-2.11/classes/views

Be sure that you pick "JARs or directories..." when you click the + sign on the right.

Saturday, July 09, 2016

TypeScript: Avoid Using Lambda in CallBack, "this" is not Captured

Symptom:

You have created a TypeScript class, and assigned a callback to an object implemented in Lambda expression (or have been suggested by Reshaper or IntelliJ to convert it to a lambda expression).

See the example below and what the problem is about to become much more clear.

The Issue

The issue is that the Lambda captures "this" in a different way than the anonymous function (). This is actually clearly documented under the Arrow Functions section in the Mozilla MDN page.

But if you are like me it is much easier to understand by actually coding the situation.

The Explanation of The Example:

I have created a simple Main class in which a callback "whatIsThis()" can be externally installed after the class has been instantiated. Then by calling whatReallyIsThis(), it will in turn calls the installed whatIsThis(). This is a fairly typical scenario, I would say.

If you are not aware of all of the implications of this, you would (and I have) thought that the class member property "s" should be accessible from within the callback in all of the scenarios.

The ones that works are;

  • If the callback function is is a function() and not an arrow function like =>(). This will not work.
  • Also, you can try this yourself, any attempt to pass "this" as main like Apply, Call or Bind to the lambda function will have no effect. 
Lessons Learned

You should treat lambda as a whole new beast of function. It appears that they tried to fix the variable notion of "this" in lambda and it only captures "this" in the line of code where it was created. What throws you off is that you've learned about "this" with anonymous functions, and assumed it works the same. Be aware of this major difference!


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Main {
    constructor(public s: string) {}
    whatIsThis:  () => void;
    whatReallyIsThis() {
        this.whatIsThis();
    }
}

let main = new Main("works!");

console.log("Anonymouns func - this points to Main instance!");

main.whatIsThis = function() { 
    console.log("This " + this.s);
}

main.whatReallyIsThis(); // This works.

console.log("Using the Labmda - this does not work!");

main.whatIsThis = () => {
    console.log("This s is " + this.s);
}

main.whatReallyIsThis(); // This s is undefined

console.log("Finally, what is this is all about");
let that = main;
exports.s = "in Global scope"; // Yup, it's in the Global!
main.whatIsThis = =&gt;()  {
    console.log"Thi(s s is " + this.s); 
    console.log("That.s " + that.s); 
}
main.whatReallyIsThis(); // This s is in Global scope, That.s works.


Sunday, September 06, 2015

Facebook + Chrome: Please try closing and reopening your browser window.

Symptom:

You get "Please try closing and reopening your browser window." when posting a message on Facebook. But closing the window does not get this message to go away.

Possible Cause

In my situation, this was due to having the Cross Origin Resource Sharing mode enabled on my Chrome using the CORS extension. By turning it off, the problem went away.

This is often considered as a non-secure mode and Facebook especially will not allow some features to be used like Posting and Messaging when the web request has been modified.

The CORS extension is used by developers like myself to access resources from different web servers and it is not normally needed for consumer use, so if you do get into this situation, you may actually have some other mechanisms/extensions which is causing the same effect, and that could mean that your Chrome may be infected with malware so be sure to check your extensions and remove anything that you do not understand what they do.

Also try with a different browser and if it only happens on Chrome (or vise versa), then I will clean up the browser.


Friday, June 19, 2015

Hibernate Envars, ValidityAuditStrategy Fails with Cannot update previous revision for entity

Symptom:

You have configured for the org.hibernate.envers.strategy.ValidityAuditStrategy with your Hibernate Envers to audit trail SQL table records with the following exception;

java.lang.RuntimeException: Cannot update previous revision for entity

Root Cause:

This happens when the table that already has records and you add the Envers auditing with ValidateAuditStrategy "after the fact". In other words, there are legacy records which were not originally audited.

Basically, the ValidityAuditStrategy was designed only with the assumption that you started using it from completely no data in the table and its related tables. It requires the initial INSERT record of the very first record of all rows with a unique record ID.

This issue has been logged in the Hibernate JIRA HHH-8456

Workarounds:

If you can afford to disable the ValidyAuditStrategy, that will log the entries.

The initial revision entry must be inserted into the newly created _AUD table for every existing legacy record.

This problem may be worked around by using the following tool that's available from the GitHub.

https://github.com/hydra1983/hibernate-envers-tools

Sunday, May 24, 2015

IntelliJ IDEA 14.x How To Resolve "routes" Activator/Play Object

Problem:

You just imported the SBT project of Activator or Play framework app in IntelliJ IDEA (14), and you found that you "routes" among other symbols do not resolve.

Root Cause:

You are lacking the dependency for the auto-generated routing class.

Solution:

You can add Module Dependency to classes_managed. This is located in the target/scala_2.xx/classes_managed

I originally posted a similar solution, but this one is much better and works with Play 2.3 or later. This video shows you exactly what you will need to do.



Thursday, April 30, 2015

Windows File History (7, 8 and 10) - One Of the Most Useful and Overlooked Feature

You are probably using some type of backup software from a commercial vendor. I have been too, but recently I stopped using it and switched to a Built in File History.

If you are used to Mac OS Time Machine, it comes as close to that, but in some ways better because you can still control what gets backed up and what does not. Like the Time Machine, you can ask it to back up hourly or even every 30 min of recent file changes and then automatically prune off old backups.

Another advantage is that it is very well integrated with the power control of your system, especially a laptop. I was using another vendor's backup software and any time I wanted to sleep or shutdown, it refused to do so because it was in a middle of a backup. Not with this, you can go to sleep, close the cover and you won't get in a situation where you have a hot battery drained laptop when you pull it out of your case.

In my situation, what I needed was automated hourly backup of some of my important documents, especially the code I am currently working on (but not quite ready to commit to Git). But if you are a teacher or an accountant just keeping the Office docs that works pretty painlessly.

Where to Backup?


Cloud backup is very popular, but the problem is that you are not always on the most reliable network especially on laptops. You could also be in flight and working on documents or code. Fortunately, most laptops have a memory card slot. So I advise that you buy as big of a memory (SD card) as you can afford, I would say get at least 64 GB. So just plug it in, format it in NTFS.

Since this is a solid-state memory, it does not consume much power and you know you have a backup available to you pretty much anywhere.

How To Enable and Configure It?





  • Go to your control panel and find File History which is under System and Security.
  • You can now enable the feature and select the destination "drive" here.
  • After the drive has been configured, click Advanced Settings

  • You can configure how often you want to back up.

Selecting Items to Back Up

Here is another cool thing about this. You can easily manage which stuff on your system is backed up. The key answer to " how to select which folder to backup" is that anything included in the Libraries part of your Explorer is backed up. By default your Documents, Music, Photos and Videos are backed up. In the picture below I have dragged in WinguMD folder which contains my work stuff. Note that dragging the items in the Libraries folder does not move the folder there, but it just like the shortcuts but backups are enabled.  I would not back-up videos if you have a small memory card.

On a side note, I have been wondering what the Libraries were for the longest time. Now we both know what this is.





Windows 10 Lost Ability to RDP Into the System

Symptom:


You upgraded to Windows 10 and now you cannot RDP to the system at your home network. (I do this a lot because I use Mac at home and RDP to my laptop which is Surface 3).

One of the Root Causes

In my situation, all of my WiFi connection went to Public which automatically turned off just about any incoming traffic to it from the Windows Firewall. This is a good feature. Do not turn that off.

To Make Your WiFi Network to Private

This, I guarantee you will take some time as there has been a big change.

  1. On your desktop, click the New Notifications icon on the task bar. That' far right and looks like a Messaging icon.
  2. Right click over WiFi and "Go to Settings."
  3. This is where you will be lost, especially if you have many spots.
  4. On the list of WiFi access points scroll all the way down.
  5. Click HomeGroup
  6. If your system is like mine, you will get a chance to enable the HomeGroup on the network you are on.