Thursday, September 15, 2016

For the love of Jiggling Visual Studio 2015 Update 3 .NetCore Solutions

I've had an opportunity to work with VS.Net 2015 Update 3 for a while now using .Net Core. There are two different flavors of .Net Core projects. I call them:
  1. Core Core: Which is all Core
  2. Core Framework: Which uses .Net Framework 4.61

We've been using Core Framework and it occasionally needs to be (affectionately named) 'jiggled'. Our solutions have more than one project in them and they occasionally barf when trying to communicate to other projects in the solution. These projects may also lose their ability to use dependencies defined in the project.json.

Here are the jiggling tricks that work for our group:
  1. Identify the project or projects that are having issues with their dependencies.
  2. On that project determine if it is trying to reference another project in your solution that is having issues. If so go to that project see if that is having issues. Lather, rinse, repeat.
  3. Now you are in a project with no dependencies on other projects in your solution and it has an issue with it's references.
  4. Jiggle 1: Right-click on references and choose "Restore Package...". If it is fixed, great, if not goto Jiggle 2.
  5. Jiggle 2: Right-click on your project and choose "Unload Project". Then right-click again and choose "Load Project". Repeat Jiggle 1.
  6. Jiggle 3: Right-click on your project and choose "Unload Project". Then from Windows Explorer, not IE, go to your project folder and delete "project.json.lock". Right-click on your project and choose "Load Project" which will rebuild your "project.json.lock". Repeat Jiggle 1.
  7. Jiggle of last Resort: If Jiggle 1 thru 3 don't work and you can't expand your reference tree, then reinstall your Update 3 and the .Net Core SDK then reboot and follow Jiggle 1 thru 3 again. If these don't work then reboot your machine three times and stand on your left foot then commence crying as that is beyond the scope of this little ditty.
Repeat the Jiggle steps for each project in your solution.

Enjoy!

Tuesday, July 5, 2016

Visual Studio 2015 Update 3 with DotNetCore 1.0.0 SDK install with Entity Framework 7 ( EF7 )

Had some fun today trying to get EF7 to work with dotnet core, but finally got through all the issues I was running into. Let me take you on that journey:

In order to proceed you've got to install the following three items (perhaps just the first two...):
1. Visual Studio 2015 Update 3
2. .NET Core for Visual Studio
3. or choose the installer from https://www.microsoft.com/net/download which I perhaps shouldn't have done.

Links for the above can be found in this article which I also use in my explanations below:
https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

I installed 1 above, then 2, but was still getting an error. In particular I couldn't get this line to run from this tutorial.
 Install-Package Microsoft.EntityFrameworkCore.Tools –Pre  


To troubleshoot I installed the x86 versions of .NET Core SDK Installer (Preview 2) and .NET Core Installer (v1.0) as well to see if that would help. It did until I rebooted after which no references were being displayed. I quickly reinstalled the .Net Core SDK Installer (Preview 2) x64 versions again and that seemed to do the trick.

All three of the install package commands were now installed without restore failures!:
 Install-Package Microsoft.EntityFrameworkCore.SqlServer  
 Install-Package Microsoft.EntityFrameworkCore.Tools –Pre  
 Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design  


Next I tried to get EF7 to work with a raw .Net Core project following the efproject.net link's instructions which needed a little changing.

First change the project.json to the following:
 {  
  "version": "1.0.0-*",  
  "buildOptions": {  
   "emitEntryPoint": true  
  },  
  "dependencies": {  
   "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",  
   "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",  
   "Microsoft.EntityFrameworkCore.Tools": {  
    "type": "build",  
    "version": "1.0.0-preview2-final"  
   },  
   "Microsoft.NETCore.App": {  
    "type": "platform",  
    "version": "1.0.0"  
   },  
   "Microsoft.EntityFrameworkCore.Design": {  
    "type": "build",  
    "version": "1.0.0-preview2-final"  
   }  
  },  
  "tools": {  
   "Microsoft.EntityFrameworkCore.Tools": {  
    "version": "1.0.0-preview2-final"  
   },  
   "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",  
   "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"  
  },  
  "frameworks": {  
   "netcoreapp1.0": {}  
  }  
 }   


Key point 1: Can't use EF7 against a class library so we added the buildOptions and emitEntryPoint portion per this article. EF7 won't work against a project that is not an application of some sort (i.e. it can't be a class library). Otherwise you'll see these kinds of errors when you try and run Scaffold-DbContext:
 PM> Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer  
 Could not invoke this command on the startup project 'Core.Datav2'. This preview of Entity Framework tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds.  

or if you forgot to add the [STAThread]...static void main(string[] args) in you're Program.cs:
 PM> Scaffold-DbContext "Server=localhost;Database=VbscriptIndexer;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer  
 C:\Program Files\dotnet\dotnet.exe compile-csc @C:\Projects\dotNetCore\Core.Datav2\bin\Core.Datav2\obj\Debug\netcoreapp1.0\dotnet-compile.rsp returned Exit Code 1 C:\Projects\dotNetCore\Core.Datav2\error CS5001: Program does not contain a static 'Main' method suitable for an entry point   
  Build failed on 'Core.Datav2'.   
And if you're still having issues, re-install the .NET Core SDK Install (Preview 2) and try again. I ended up doing it a few times which was a bit annoying but was really self-caused misery.

Saturday, July 2, 2016

Visual Studio VS.Net 2015 RC2 and Typescript

So my Visual Studio solution is throwing a ton of errors due to the latest rx.js component. The Angular2 solution works just fine, VS.Net 2015 RC2 doesn't think so. To resolve the VS 2015 RC2 issue, you can see the fix described at:
https://github.com/Microsoft/TypeScript/issues/8518

This link essentially upgrades your VS.Net 2015 RC2 instances from ts.version 1.8.9 to ts.version 1.8.10 which will fix all the pesky ambient declaration and other similar errors showing up in Visual Studio. 1.8.10 now handles external modules with their nested ambient modules by moving them to imported files since they need to be resolved. As an aside, I found that typings.json resolves much the same issue but in another way.

If you have VS.2015 rc2 you should seriously consider upgrading it with update 3: https://blogs.msdn.microsoft.com/visualstudio/2016/06/27/visual-studio-2015-update-3-and-net-core-1-0-available-now/

Update 3 will take you to ts.version 1.8.34 and should NOT install the ts version 1.8.10 over the top.

Anyways, after installing v 1.8.10 my VS.Net rc2 Angular2 app builds successfully. Will install update 3 tomorrow as it is a 7gb download and can take quite a while to install.

Tuesday, June 14, 2016

Exploring Microservices and Web Components

Been listening to a number of podcasts and reading on microservices over the last couple years and may now have a project to apply this pattern to.

Found some good posts on the subject:

1. Microservices vs Monolithic:
http://www.slideshare.net/chris.e.richardson/decompose-that-war-a-pattern-language-for-microservices-qcon-qconsp/20

2. API gateway pattern to support microservices:
http://microservices.io/patterns/apigateway.html

3. Frontend microservices (aka Web Components)
https://technologyconversations.com/2015/08/09/including-front-end-web-components-into-microservices/

3.a. Web Components are great except perhaps in IE 11 which has the Polyfill workaround. So it's definitely doable there. IE Edge appears to be being built with Web Components in mind so we'll see how that future plays out. Working in an enterprise that relies on IE as it's default browser, one has to consider such things.
http://microservices.io/patterns/apigateway.html on IE Edge
https://vaadin.com/blog/-/blogs/web-components-in-production-use-are-we-there-yet-/ on IE 11

Saturday, June 4, 2016

Visual Studio 2015 Update 2 NuGet.config Proxy/Firewall Issues

Overview
My "dotnet restore" does not work with NuGet's v3 package source.  This post analyzes why it was occurring and how it was resolved.

Analysis:
I've been playing around with my nuget.config file recently due to "dotnet restore" issues with a project built with VS 2015 Update 2 dotnetcore project. To start, nuget.config is located here for me: C:\Users\<username>\AppData\Roaming\NuGet\NuGet.Config

When I change this file it affects my VS 2015 builds (i.e. restore)

For the purposes of this post I will be using the command line to do "dotnet restore"s using ConEmu.
I was having issues with a couple of the package sources in nuget.config requiring proxy authentication. Here is a sample ran from the command line:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 250ms
error: Unable to load the service index for source https://dotnet.myget.org/F/dotnet-cli/api/v3/index.json.
error:   Response status code does not indicate success: 407 (Proxy Authentication Required).

So I reverted back to an earlier version of NuGet.Config which is as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </activePackageSource>
  <packageSources>
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </packageSources>
</configuration>

Now when I run "dotnet restore" from the command line I see this:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver'
info :   OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver' 807ms
info :   GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost'
info :   OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost' 136ms
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...
info : Committing restore...
log  : Writing lock file to disk. Path: C:\<projectPath>\project.lock.json
log  : C:\<projectPath>\project.json
log  : Restore completed in 9030ms.

NuGet Config files used:
    C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config
    C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config

Feeds used:
    https://www.nuget.org/api/v2/
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

I like the command line tool "dotnet restore" and the info it provides.  Using it helps see what NuGet Config files are being used and which feeds are being used. Additionally I'm currently using https://www.nuget.org/api/v2/ as a package source where I should be using v3.

For grins and giggles I added "Hangfire.Core", of which I know nothing about, to my project.json to see how it would react:
"Hangfire.Core": "1.5.6"
It looks like the v2 package source found it but unfortunately my project is not compatible with it so...
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   CACHE https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver'
info :   CACHE https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost'
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...
error: Package Hangfire.Core 1.5.6 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.5.6 supports: net45 (.NETFramework,Version=v4.5)
error: Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
error: One or more packages are incompatible with .NETCoreApp,Version=v1.0.
info : Committing restore...
log  : Lock file has not changed. Skipping lock file write. Path: C:\<projectPath>\project.lock.json
log  : C:\<projectPath>\project.json
log  : Restore failed in 1219ms.

Errors in C:\<projectPath>\project.json
    Package Hangfire.Core 1.5.6 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.5.6 supports: net45 (.NETFramework,Version=v4.5)
    Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.

NuGet Config files used:
    C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config
    C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config

Feeds used:
    https://www.nuget.org/api/v2/
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

What's weird is that v2 works at all for me. Granted I have VS 2010, 2012, 2013, and 2015 on my machine. Here is what I found from the nuget homepage:
•NuGet feed v3 (VS 2015 / NuGet v3.x): https://api.nuget.org/v3/index.json
•NuGet feed v2 (VS 2013 and earlier / NuGet 2.x): https://www.nuget.org/api/v2
So once more into the breach..... (i.e. get v3 to work for my "dotnet restore"s in VS 2015)

I changed the project.json to see if "Hangfire.Core" is supported by dotnetcore in it's current beta release:
"Hangfire.Core": "1.6.0-beta3"

I also modified my NuGet.Config as follows to include the v3 version:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </activePackageSource>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </packageSources>
</configuration>

However when I run "dotnet restore" it fails with the above NuGet.Config file:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
error: Unable to load the service index for source https://api.nuget.org/v3/.
error:   An error occurred while sending the request.
error:   A connection with the server could not be established

Resolution:
Ultimately removing the activePackageSources section fixed the error for me and allowed me to pick up v3 finally as a valid package source. Here is the new NuGet.Config that works:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </packageSources>
</configuration>

And here are the command line results, although again "Hangfire.Core" in it's current beta state is not compatible with dotnetcore. But hey, at least the feed is now using v3:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json
info :   GET https://api.nuget.org/v3-flatcontainer/hangfire.core/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 168ms
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json 182ms
info :   OK https://api.nuget.org/v3-flatcontainer/hangfire.core/index.json 578ms
info :   GET https://api.nuget.org/v3-flatcontainer/hangfire.core/1.6.0-beta3/hangfire.core.1.6.0-beta3.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/hangfire.core/1.6.0-beta3/hangfire.core.1.6.0-beta3.nupkg 215ms
log  : Installing Hangfire.Core 1.6.0-beta3.
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...
error: Package Hangfire.Core 1.6.0-beta3 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.6.0-beta3 supports: net45 (.NETFramework,Version=v4.5)
error: Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
error: One or more packages are incompatible with .NETCoreApp,Version=v1.0.
info : Committing restore...
log  : Writing lock file to disk. Path: C:\<projectPath>\project.lock.json
log  : C:\<projectPath>\project.json
log  : Restore failed in 5477ms.

Errors in C:\<projectPath>\project.json
    Package Hangfire.Core 1.6.0-beta3 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.6.0-beta3 supports: net45 (.NETFramework,Version=v4.5)
    Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.

NuGet Config files used:
    C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config
    C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config

Feeds used:
    https://api.nuget.org/v3/index.json
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

So I came at this from a couple different directions and ultimately got v3 to work.

To throw a wrench into this solution:
So I continued to have issues connecting to the nuget v2 and v3 package sources via "dotnet restore". Most of the time it doesn’t work, sometimes it works. I’m thinking that perhaps my company has multiple proxy servers out there filtering our requests out to the world. I’m thinking our requests out to the world go through one of these servers. Sometimes we’re going through one server for a while when suddenly we are on another server. My guess is that one of these servers lets us through while another doesn’t allow it. I tested it by simply running "dotnet restore" from the command line over and over again. Mostly it failed but it would sometimes work:

 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 info :  GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver'  
 info :  OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver' 733ms  
 info :  GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost'  
 info :  OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost' 186ms  
 log : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...  
 info : Committing restore...  
 log : Writing lock file to disk. Path: C:\<projectPath>\project.lock.json  
 log : C:\<projectPath>\project.json  
 log : Restore completed in 11027ms.  
 NuGet Config files used:  
   C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config  
   C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config  
 Feeds used:  
   https://www.nuget.org/api/v2/  
   C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v2/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 info :  GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json  
 info :  OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 189ms  
 info :  GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json  
 info :  OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json 282ms  
 log : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...  
 info : Committing restore...  
 log : Lock file has not changed. Skipping lock file write. Path: C:\<projectPath>\project.lock.json  
 log : C:\<projectPath>\project.json  
 log : Restore completed in 2410ms.  
 NuGet Config files used:  
   C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config  
   C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config  
 Feeds used:  
   https://api.nuget.org/v3/index.json  
   C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\  
 <user>@<computer> C:\<projectPath>  
 $  
So we have an issue with our proxy/firewall that resulted in the rigmarole of this post above. Enjoy!

Tuesday, May 24, 2016

Angular2 migration from Beta to RC1

I had a chance to explore this Angular2 space again and found that voila, RC1 is out! How fun it would be migrate over.. Using my last post as a basis for moving forward, here is what I went through.

Please note that I'm not an expert in Angular2. Just exploring it in my free time.

First off the dependencies in your packages.json file changed:
Before:
 {  
  "version": "1.0.0",  
  "name": "ASP.NET",  
  "private": true,  
  "dependencies": {  
   "angular2": "^2.0.0-beta.7",  
   "es6-promise": "^3.1.2",  
   "es6-shim": "0.35.0",  
   "reflect-metadata": "^0.1.3",  
   "rxjs": "^5.0.0-beta.2",  
   "systemjs": "^0.19.23",  
   "whatwg-fetch": "^0.11.0",  
   "zone.js": "^0.6.1"  
  },  
  "devDependencies": {}  
 }  

After:
 {  
  "version": "1.0.0",  
  "name": "ASP.NET",  
  "private": true,  
  "dependencies": {  
   "@angular/common": "2.0.0-rc.1",  
   "@angular/compiler": "2.0.0-rc.1",  
   "@angular/core": "2.0.0-rc.1",  
   "@angular/http": "2.0.0-rc.1",  
   "@angular/platform-browser": "2.0.0-rc.1",  
   "@angular/platform-browser-dynamic": "2.0.0-rc.1",  
   "@angular/router": "2.0.0-rc.1",  
   "@angular/router-deprecated": "2.0.0-rc.1",  
   "@angular/upgrade": "2.0.0-rc.1",  
   "es6-promise": "^3.1.2",  
   "es6-shim": "0.35.0",  
   "reflect-metadata": "^0.1.3",  
   "rxjs": "^5.0.0-beta.6", //Be sure to use beta.6  
   "systemjs": "^0.19.27",  
   "whatwg-fetch": "^0.11.0",  
   "zone.js": "^0.6.12"  
  },  
  "devDependencies": {  
   "typescript": "^1.8.10"  
  }  
 }  

After setting up these dependencies in packages.json I performed my npm install and found some errors:

Went ahead and deleted the "angular2" folders from my "wwwroot" folder.
Modified the gulpfile.js file to replace the "angular2" copies from node_modules to wwwroot/node_modules with new "@angular/*" calls:
 gulp.task('thirdparty', function () {  
   gulp.src('./node_modules/@angular/**/*.js')  
     .pipe(gulp.dest('./wwwroot/node_modules/@angular'));  
   // gulp.src('./node_modules/angular2/**/*.js')  
   //   .pipe(gulp.dest('./wwwroot/node_modules/angular2'));  

Then I had to go and update my index.html file to replace the angular2 references with @angular. However I decided life would be harder if I took this opportunity to see what systemjs is about. So of course lets digress and go down systemjs learning alley.

Systemjs allows us to specify our third party references in a separate javascript file. Systemjs has the added ability of loading these 3rd party references on demand vs on page load. So anyways, here is the before and after of my index.html file: Before:
 <!DOCTYPE html>  
 <html>  
 <head>  
   <base href="/">  
   <meta charset="utf-8" />  
   <title></title>  
 </head>  
 <body>  
   <app-shell><h2>Loading...</h2></app-shell>  
   <script src="node_modules/es6-shim/es6-shim.js"></script>  
   <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>  
   <script src="node_modules/systemjs/dist/system.src.js"></script>  
   <script src="node_modules/rxjs/bundles/Rx.js"></script>  
   <script src="node_modules/es6-shim/es6-shim.js"></script>  
   <script src="node_modules/angular2/bundles/angular2.dev.js"></script>  
   <script src="node_modules/angular2/bundles/router.dev.js"></script>  
   <script src="node_modules/angular2/bundles/http.dev.js"></script>  
   <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>  
   <script>  
     System.config({  
       packages: {  
         app: {  
           format: 'register',  
           defaultExtension: 'js',  
         }  
       }  
     });  
     System.import('app/boot')  
       .then(console.log('started application'), console.error.bind(console));  
   </script>  
 </body>  
 </html>  

After:
 <!DOCTYPE html>  
 <html>  
 <head>  
   <base href="/">  
   <meta charset="utf-8" />  
   <title></title>  
 </head>  
 <body>  
   <app-shell><h2>Loading...</h2></app-shell>  
   <script src="node_modules/es6-shim/es6-shim.js"></script>  
   <script src="node_modules/typescript/lib/typescript.js"></script>  
   <script src="node_modules/es6-shim/es6-shim.js"></script>  
   <script src="node_modules/reflect-metadata/Reflect.js"></script>  
   <script src="node_modules/systemjs/dist/system.js"></script> <!--//Changed from system.src.js -->  
   <script src="node_modules/zone.js/dist/zone.js"></script>   <!--//Had to add Zonejs as it is used by @angular/core -->  
   <script src="systemjs.config.js"></script>  
   <script>  
     System.import('app/boot')  
       .then(console.log('started application'), console.error.bind(console));  
   </script>  
 </body>  
 </html>  

This leads us to where are the "@angular" references? Well I've got them in a file called systemjs.config.js which is in the root of my project directory (i.e. at the same level as my packages.json, etc).
Here is what I've got in my systemjs.config.js file:
 System.config({  
   transpiler: 'typescript',  
   typescriptOptions: {emitDecoratorMetadata: true},  
   map: {  
     'app' : 'app',  
     'rxjs': 'node_modules/rxjs',  
     '@angular/core'          : 'node_modules/@angular/core',  
     '@angular/common'         : 'node_modules/@angular/common',  
     '@angular/compiler'        : 'node_modules/@angular/compiler',  
     '@angular/router'         : 'node_modules/@angular/router',  
     '@angular/platform-browser'    : 'node_modules/@angular/platform-browser',  
     '@angular/platform-browser-dynamic': 'node_modules/@angular/platform-browser-dynamic',  
     '@angular/http'          : 'node_modules/@angular/http'  
 },  
   packages: {  
     'app'               : {main: 'app/boot.ts', defaultExtension: 'ts'},  
     'rxjs'               : {main: 'index.js'},  
     '@angular/core'          : {main: 'index.js'},  
     '@angular/common'         : {main: 'index.js'},  
     '@angular/compiler'        : {main: 'index.js'},  
     '@angular/router'         : {main: 'index.js'},  
     '@angular/platform-browser'    : {main: 'index.js'},  
     '@angular/platform-browser-dynamic': {main: 'index.js'},  
     '@angular/http'          : { main: 'index.js'}  
 }  
 });  

So I've got my app, rxjs and @angular setup in systemjs and it seems to work just fine. But before it will work you need to be sure to copy it to the wwwroot folder via your gulpfile.js:
 gulp.task('copy', function () {  
   gulp.src('./app/**/*.*')  
     .pipe(gulp.dest('./wwwroot/app'));  
   gulp.src('./systemjs.config.js')  
     .pipe(gulp.dest('./wwwroot'));  
 });  

So our basic setup is done. Now onto the interesting changes that have taken place since Angular2 Beta 7: 1. It looks like @View is no more. They've moved it's properties into @Componment. Here is some before and after: Before:
 @Component({  
   selector: 'book-edit.component'  
 })  
 @View({  
   templateUrl: './app/components/book/edit/book-edit.component.html',  
   directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFormControl]  
 })  

After:
 @Component({  
   selector: 'book-edit.component',  
   templateUrl: './app/components/book/edit/book-edit.component.html',  
   directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFormControl]  
 })  

2. Of course, with angular2 gone, you have to go through and clean up all your imports. Here is a brief example: Before:
 import {Component, View, Inject} from 'angular2/core';  
 import {RouteParams} from 'angular2/router';  
 import {CORE_DIRECTIVES, FORM_DIRECTIVES, FormBuilder, Control, ControlGroup, Validators, NgFormControl, AbstractControl } from 'angular2/common';  

After:
 import {Component, Inject} from '@angular/core';  
 import {RouteSegment} from '@angular/router';  
 import {CORE_DIRECTIVES, FORM_DIRECTIVES, FormBuilder, Control, ControlGroup, Validators, NgFormControl, AbstractControl } from '@angular/common';  

3. Routers had a major overhaul. Here are some before and after changes I ran into:
 
 before: import {RouteParams} from '@angular/router';  
 after : import {RouteSegment} from '@angular/router';  

 before: constructor( @Inject(RouteParams) params: RouteParams,  
 after : constructor( @Inject(RouteSegment) params: RouteSegment,  

 before: this.getBook(params.get('id'));  
 after : this.getBook(params.getParam('id'));  

 before:  
 @RouteConfig([  
     { path: '/list-admin',      component: BookListAdminComponent,  as: 'ListAdmin' },  
     { path: '/list-readonly',     component: BookListReadonlyComponent, as: 'ListReadonly' },  
     { path: '/create',        component: BookCreateComponent,    as: 'Create' },  
     { path: '/edit/:id',       component: BookEditComponent,     as: 'Edit' },  
     { path: '/view/:id/',       component: BookViewComponent,     as: 'View' }  
 ])  
 after:   
 @Routes([  
     { path: '/list-admin', component: BookListAdminComponent }, //  as: 'ListAdmin' },  
     { path: '/list-readonly', component: BookListReadonlyComponent }, // as: 'ListReadonly' },  
     { path: '/create', component: BookCreateComponent }, //    as: 'Create' },  
     { path: '/edit/:id', component: BookEditComponent }, //     as: 'Edit' },  
     { path: '/view/:id', component: BookViewComponent }, //     as: 'View' }  
 ])  

 before: this.router.parent.navigate(['/View', { id: book.id }]);  
 after: this.router.navigate(['/view/'+ book.id ]);  

 before: <a [routerLink]="['ListReadonly']">View List of Books</a>  
 after: <a [routerLink]="['/list-readonly']">View List of Books</a>  
Now at this point I've made all my import changes, etc and think I'm ready to continue on. Unfortunately, VS.Net 2015 is throwing a lot of errors my way now, like:
1. Observable does not contain property ‘map’
2. TS2304: Cannot find name 'Promise'
3. Build: Ambient modules cannot be nested in other modules or namespaces
4. Build: Ambient declaration cannot specify relative module name

Looking at the details of these errors, for the most part all the errors I'm seeing are all associated to rxjs.

In my little world I've been brought up to respect and bow before the VS.Net 2015 gods when they throw red error lightning bolts my way. In this case its about 650 of 'em and I'm resembling a pin cushion. For I must fix them before I proceed, so I did the following: (note: I found later on that the code would run fine. It was VS.net and it's typescript version that were the problem and there is nothing I can do about it at this point...)
1. Explored 'typings'
2. Explored more 'gulp' functionality
3. In the end not sure if I needed 'typings' and didn't need the additional 'gulp' functionality.

I'll explore my journey with 'typings' in another post.

So yeah, life is good again. VS.net is throwing errors, but when I run the app all is good. All my crud functionality is working again after the upgrade. I look forward to when VS.net stops throwing errors on the rxjs stuff. So we'll see.

Wednesday, January 13, 2016

Angular2 TypeScript Hello World App Setup with VS 2015 and ASP.NET 5 RC1


  1. InstallVisual Studio 2015
  2. Install ASP.NET 5 RC1 which contains new templates you’ll need for this.
  3. Install npm which you’ll need to get our Angular2 and associated third party dependencies
  4. If you’re in a corporate environment you’ll need to configure your npm proxy settings to get past all this:
    npm config set proxy http://proxy.company.com:80/
    1. If you have a proxy to get through you’ll need to figure out what your proxy is.  If you’re company is using a .pac file then you’ll need to read it to determine the proxy.  Have fun J
    2. This link is helpful: http://jjasonclark.com/how-to-setup-node-behind-web-proxy/
    3. Emulate the VS Developer command prompt by running this:
    4. Then modify the npm config settings as follows:
    5. This should allow npm to access the outside world.
    6. However it may not.  When you type “npm config set proxy http…” it places the entries in a “.npmrc” file located on your “C:\Users\UserID\.npmrc” path.
      1. If you mistype your proxy or https-proxy command it won’t validate it for you.  It’ll simply put it in there, as is, with no config checking whatsoever.  At least that’s what I think from 10 seconds of experience with it…
      2. My .npmrc file looks like this after the three “set” commands above:
    7. However then again this didn’t work for someone I know.  So we found another entry I’d put in at some point in my travels in the “Environment Variables”:
      1. Add HTTP_PROXY of http://proxy.company.com:80 as shown in the above illustration
      2. Check your NODE_PATH is there as well as it may assist with running "gulp" when you get to that point.
    8. Now test that the config setting is correct:
      npm config get proxy
      1. And it will show this:
      2. If it still shows your http://login:password@http://fdsafdsa then you’ve still got an issue.
    9. It could be that if you have the .npmsrc file in the root of the folder where you’re running this thing then maybe that’ll work.  Feel free to play and figure it out.
  5. Note:  The following was taken from a few web articles:
    1. http://dotnetspeak.com/2015/12/angular-2-beta-asp-net-v-next-release-candidate-and-typescript-so-happy-together - The below steps are largely based on this article.  However it is missing some key things that I struggled with.  So I added those items below.
    2. https://angular.io/docs/ts/latest/quickstart.html - Official startup guide from the Angular folk.  However it’s missing the VS.net setup, etc
    3. Note: In both instances you’ll find more details on “why” we do things in the above links than you’ll find below.  This is an instruction manual. I only go into detail in areas I had trouble with.
    4. Enjoy!
  6. Create a new ASP.Net 5 project:
      1. Choose the “Empty” ASP.NET 5 Template.
      2. Uncheck “Host in the cloud” if checked.
    1. Choose OK to continue
  7. I called my new project “Web.Client”.  Here’s a screenshot of our folder structure:
  8. Setup static file support in the package and application code
    1. Package file update: Open “project.json” and add the static files package
      1. "Microsoft.AspNet.StaticFiles":"1.0.0-rc1-final"
    2. Application code update: Open “Startup.cs” and modify the Configure method as follows:
      1. Add app.UseStaticFiles();
      2. Comment out the Hello World script
  9. Add “index.html” under the wwwroot folder:

  10. Edit the “index.html” file and give it something to tell us:
  11. Build Web.Client so that the Startup.cs changes take effect.
  12. Test that our index.html page can be seen in a browser:
    1. Notes:
      1. At this point we have the bare minimum needed to serve our new “index.html” page
    2. Open a command prompt with administrative privileges:
    3. Navigate to your project directory.  In my case I’ll be navigating to my “Web.Client” directory in the console session:
      cd c:\Sample\Web.Client
    4. Run “dnx web” to start our hosting environment:
      1. You could instead use the “IIS Express” debug option from Visual Studio.
        1. My experience with this is it will work but is very slow.  I personally like Microsoft’s “dnx” command prompt option better at this point…  I mean come on, there is only one “.cs” file and we don’t need to edit it again so why invoke the overhead…
        2. Read up on “DNX” if you want to know more: http://docs.asp.net/en/latest/dnx/overview.html
      2. If “dnx” is not recoginized and throwing an error try “dnvm upgrade” which seems to resolve this issue.
    5. Open a browser and navigate to http://localhost:5000/index.html:
      1. >If you see “Hello World!” then you forgot to either make the Startup.cs changes or build your application prior to executing “dnx web”.  Make both changes, press “Ctrl+C”, then type “dnx web” to reload your app for access through the URL.
  13. Now let’s install our Angular2 components:
    1. Add a new item to the project: “NPM Configuration file” called “package.json”
    2. Open “package.json” and paste these dependencies:
      1. This is the default:
      2. This is after we’ve made our changes:
      3. Here’s the text:
        "dependencies": { "angular2":"2.0.0-beta.0", "systemjs":"0.19.9", "es6-promise":"3.0.2", "es6-shim":"0.34.0", "reflect-metadata":"0.1.2", "rxjs":"5.0.0-beta.0", "zone.js":"0.5.10" },
      4. Note: You can view what the latest versioning of each of these dependencies by replacing the first letter with itself. I’m sure theres a shortcut, but I haven’t put much effort into finding it:
      5. Note: You also hover over each of the dependencies to get a little fan fiction on each of the dependencies:
      6. Note: I ran into an issue going from 2.0.0-beta.0 to 2.0.0-beta.1 when trying to view my Angular2 site in IE11. Worked in beta.0, but not in beta.1. More on that when we get to that section. So for now, use the dependencies provided above. I’ll mention it again below when you get there…
      7. Save your changes to “package.json”
    3. If you’re watching you may see that the that the packages are “restoring…” in your solution:
      1. In your Output you’ll see that npm is installing those dependencies auto-magically:
      2. Wait for the “Restoring…” to complete
  14. Now that we have the dependencies setup we need to put some structure onto where we’ll do our development.  Create a folder called “app” under your project, mine is “Web.Client”
  15. Add a “TypeScript” file  under the new “app” folder and call it “app.ts”. 
  16. Here’s what we’ll have so far in “wwwroot”, “Dependencies\npm”, and “app”:
  17. Edit “app.ts” and put this in there:
    import {Component} from 'angular2/core'; @Component({ selector: 'app-shell', template: '<h1> Angular 2 is working if you see this for {{name}}</h1> ' }) export class app { name: string; constructor() { this .name = "Hello Kitty"; } }
  18. Now we’ll see this when we look at “app.ts” (i.e. notice the compile error):
    1. The screenshot above says “export class Main” when it should say “export class app”
  19. Now we’ll fix the compile error by configuring our typescript behavior by:
    1. Add a “TypeScript JSON Configuration file” at the root of your Project:
    2. By default it shows this:
    3. We want to add a couple things to make our app.ts file sing:
      1. Enable decorators functionality:
        “emitDecoratorMetadata”: true
      2. Enable experimental Decorators (we’re in beta which means it’s all experimental at least that’s my best guess. If you don’t do this the Visual Studio script checker will barf on random things)
        “experimentalDecorators”: true
      3. Use systemjs for module loading
        “module”: “system”
      4. Use node style module resolution:
        “moduleResolution”: “node”
      5. Turn on noImpliciteAny for the heck of it
      6. What we’ll have is this:
    4. Save your changes
  20. Now we need to start our app by using bootstrapping functionality from Angular2.
    1. Create a new “TypeScript” file under the “app” folder called “boot.ts”:
    2. Modify “boot.ts” and put this in it:
    3. import{app}from"./app"; import{bootstrap}from'angular2/platform/browser' bootstrap(app);
      1. In this script we’re pointing to our app.ts’s “app” class and starting it up.
    4. Save your changes
  21. Now we get to use our new app.ts and boot.ts in index.html.  So let’s put it all together
    1. Modify “index.html” in the “wwwroot” folder as follows: <!--DOCTYPEhtml--> <html> <head> <metacharset ="utf-8" /> <title> Angular2 Testing </title> <!-- 1. Load thy libraries --> <scriptsrc ="node_modules/angular2/bundles/angular2-polyfills.js"></script> <scriptsrc ="node_modules/systemjs/dist/system.src.js"></script> <scriptsrc ="node_modules/rxjs/bundles/Rx.js"></script> <scriptsrc ="node_modules/angular2/bundles/angular2.dev.js"></script> <!-- 2. Configure thy SystemJS --> <script> System.config({ packages: { app: { format: 'register', defaultExtension: 'js', } } }); System.import( 'app/boot') .then(console.log( 'Application started...' ), console.log( 'Error occurred while starting application!')); </script> </head> <!-- 3. Display thy application --> <body> <app-shell><h2> Loading or more appropriately, your Angular2 app isn't working yet... </h2></app-shell> </body> </html>
    2. Above script taken from the Angular2 start page at https://angular.io/docs/ts/latest/quickstart.html
    3. Now we need to figure out how to get the “app” folder files we’ve created into the “wwwroot” folder.  Well this problem has been solved by a program called “gulp”.  Here’s how we install, configure, and abuse “gulp” to perform the dependency and “app” folder copies to “wwwroot”.
      1. Install “gulp”
        1. You could try and install gulp at the root of your project but I recommend you do it more globally like so
          1. From a command prompt type:
            npm install gulp –g
            1. This installs the gulp app globally.
            2. Add a “Gulp configuration file” to your Web.Client root folder called gulpfile.js:
            3. Edit your “gulpfile.js” file as follows: /* This file in the main entry point for defining Gulp tasks and using Gulp plugins. Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007 */ var gulp = require('gulp'); // Copy our dependencies to the wwwroot folder gulp.task('thirdparty',function() { gulp.src('./node_modules/angular2/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/angular2')); gulp.src('./node_modules/es6-promise/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/es6-promise')); gulp.src('./node_modules/zone.js/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/zone.js')); gulp.src('./node_modules/systemjs/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/systemjs')); gulp.src('./node_modules/reflect-metadata/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/reflect-metadata')); gulp.src('./node_modules/es6-shim/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/es6-shim')); gulp.src('./node_modules/rxjs/**/*.js') .pipe(gulp.dest('./wwwroot/node_modules/rxjs')); }); //Copy our "app" folder changes over to the wwwroot folder's "app" folder gulp.task('copy',function() { gulp.src('./app/**/*.*') .pipe(gulp.dest('./wwwroot/app')); }); //Optional way of automatically copying over the compiled javascript files over to the wwwroot folder gulp.task('watch',function() { gulp.watch('./app/**/*.js', ['copy']); }); //By default the thirdparty task above will run gulp.task('default', ['thirdparty']);
    4. Save all changes to your solution, which includes your “gulpfile.js” file.
    5. Now we’ll test our “gulp” install
      1. Run “gulp thirdparty” from your project folder location to copy the dependencies over to “wwwroot”
        1. Before: After:
        2. Here is what your command prompt will show:
      2. At this point we would want to run “gulp copy” as well. However we first need to get the app.ts and boot.ts files to compile.
    6. Setup compilation of your TypeScript (.ts) files as follows:
      1. In your VS 2015 solution goto Tools\Options:
      2. Navigate to “Text Editor\TypeScript\Project\General” and set these flags:
      3. You might as well enable “Line Numbers” while you’re at it. I luv line numbers, but if you’re a hater ignore this step:
      4. Choose OK to continue
      5. Before:   After:
      6. Note: You may not see the app.js and boot.js at first.  You may have to jiggle the keys in the lock a little before it’ll take.  In order to get boot.ts to create the boot.js file I put my cursor in the file put a space and saved.  This seemed to trigger the on save compile nature of these TypeScript (.ts) files.
      7. If you have the app.js and boot.js files then we’re ready for the next step
    7. Now we can do the “gulp copy” command:
      1. It puts the root\app files in root\wwwroot\app:
    8. At this point everything is covered… well except for IE11 which you’ll see shortly
    9. Test our file in the browser:
      1. Run “dnx web” again from your project folder in a command prompt:
      2. In Chrome:
      3. In IE11:
        1. Say what???!!!
        2. Ok, so first things first, let’s debug this, press F12 in your IE11 browser window to bring up the developer tools window.
        3. Navigate to the “Console” tab:
        4. See nothing, then refresh your IE11 window and see your debug window refresh:
        5. Note:  There are improved ways of getting better errors returned.  Bower traceur or something like that may help, but I’ve not had a chance to look at that yet.
        6. Anyways, IE11 does not work for your Angular2 code.  See the next steps for a fix.
    10. Fix our IE11 implementation by adding “es6-shim” into index.html as follows before the “system.js” script:
                      <scriptsrc="node_modules/es6-shim/es6-shim.js"></script>
      
      1. The “es6-shim” fixes our IE11 issue and makes the Angular2 code compatible with IE11.
    11. Save your “index.html” change
    12. Reload your “index.html” page in your IE11 browser and now it’s working unless you forgot to save like I did at first…:
      1. Still not seeing it?  Then we’ll repeat what I said earlier.  The beta versions are finicky.  Beta.0 will work, Beta.1 will not work.  If you felt like typing the different dependencies into the “package.json” file then you most likely got defaulted to the most recent version which right now happens to be beta.1.  Change it back to Beta.0 and wait for the dependencies to reload after you saved.  Refresh the page in the browser to make it work.
    13. As an aside, you could open a new command prompt and type “gulp watch” which may annoy you every time you make a change to a file in the root project’s “app” folder.  But then it could have been the automatic .js file updates that occur each time you make a change to the .ts file.
      1. Go into Visual Studio and edit your \app\app.ts file located here:
      2. Edit the template text and choose save.  For grins and giggles I changed the following:
      3. View your changes in IE11 or Chrome… We are in a corporate environment so these are our two choices…
    14. Now if you are like my coworker you hate having to run “gulp” manually via a command prompt.  Never fear for he discovered that you can add gulp to run as part of your build process: http://docs.asp.net/en/latest/client-side/using-gulp.html#running-default-tasks

    15. That’s all I got, enjoy!