Wednesday, 18 May 2016

.Net Core - Publishing from Visual Studio 2015 - It's a pain

tldr; Install .Net Core SDK RC2, run dnvm upgrade to setup dnu.exe if its not visible to the console, install bower and gulp using npm, run dnu publish against your project, copy files across to server, install HttpPlatformhandler on the server, setup site manually, set app pool to not use managed code

.Net Core

So Microsoft's big announcement of the last few years is making ASP .Net better. When I say better, it means having much more pluggable functionality in the web pipeline. You no longer have to include everything that you might need, just what you do need. That has to be good for memory usage and page load times and makes cross-platform porting much more doable compared to the massive beast that is System.Web.dll

You've heard of OWIN, the pluggable web server system. That is basically the pattern for .Net Core. You can use .Net on any server and any host, each layer being abstracted from the next. With traditional .Net, everything had a dependency on IIS i.e. Windows only, which meant that to port .Net, you had to write the entire stack for another platform.

There are a whole load of features and differences between MVC5/Web Forms and .Net Core but for now, just imagine it is more modular and allows you to write MVC or Web API as well as anything else you want to write OWIN middleware for.

Your First Project

If you have Visual Studio 2015, Update 2, you should already have the templates for .Net Core. Depending on what exactly you have installed, they will either appear in the list underneath "ASP  .Net Web Application" in the Web folder as "ASP .Net Core Web Application" or if you don't have the Core SDK, they will appear in the list after you select "ASP .Net Web Application" as ASP .Net 5 templates (.Net 5 was the old name for Core). Do not confuse .Net 5 with MVC 5 which was .Net 4!

Publishing

My reason for learning about Core was to look at some basic performance tests as an indication as to whether to start developing on Core now or wait a year or so. For this reason, I didn't want to modify the default code, just publish it and fire some load tests at it.

Right-click the project and choose "Publish", I can see a publish dialog with only one option - "File system". Hmmm. Visual Studio apparently doesn't understand this project type and also doesn't know how to publish it. If you try, nothing gets published. As is often the case with new technology, there are lots of articles about doing things manually to fix it, from the previous betas and release candidates. I tried a few but no dice.

Well, you can publish it from the command line using dnu.exe apparently (no idea what all these tools are). Tried that. dnu.exe is not recognised as an internal or external command... Apparently you have to run "dnvm upgrade" first to install it and setup the paths correctly. Done.

Try again. "Bower" unknown command. I know what Bower is, I don't know why it is not installed. Maybe I need something else for Visual Studio. Oh, here is the installer for .Net Core for Visual Studio 2015. Not sure why I already had the templates but no supporting tools installed. Anyway, installed it. dnu.exe still doesn't work - same error.

One article said that if you run the tools from Visual Studio, it sorts all the paths out. The problem is, I can't - publish now doesn't work at all. If I select it, nothing happens. If I choose it from the Build menu, I get some weird dll error referencing an interface name that doesn't even exist on Google!

Maybe I need to install bower manually. npm install bower. Great. 'Gulp' unknown command. Hmm. npm install gulp -g. OK. Ignore all the millions of errors but now - O dear - the dreaded npm on Windows bug - path too long. This is due to npm's incredibly sensible but completely rubbish idea to nest dependencies to avoid package conflicts. This leads to dependencies that are 10s deep.

Groan. OK, move the project from Visual Studio Projects down into a lower level directory and try again. Now it seems to work and the project is published. What a ballache. Surely this should all happen automatically from Visual Studio. Version 1 of Core is supposed to drop in a month!

OK. Now you have to copy the files to the web server manually (of course you do!). Web Deploy is supposed to work but doesn't seem to do anything at all. Setup your IIS web site as normal, point it at the wwwroot folder of your files and, importantly, set your application pool to NOT use managed code.

Access the site. No dice, Internal Server Error 500.19. Why? Well, the documentation that I followed that made me install some exe on the server was obviously not what I needed to install. I think I read that MS changed it but, again, documentation is out of date (but not marked as such) so I needed to install the HttpPlatformHandler, which I then did. Didn't need to restart and the site now works.

Conclusion

Apart from all the promises of this great new technology stack, the tools are still woefully poor and the documentation is all over the place. They are probably scrambling to get things working but MS have succeeded quite well because most people can install, develop and publish without doing anything weird. Using Node Package Manager to install things that should work automatically in Visual Studio is just weird.

At some point, I might even load test my new site!


Post a Comment