{"id":4423,"date":"2016-11-06T11:32:18","date_gmt":"2016-11-06T11:32:18","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4423"},"modified":"2016-11-06T11:32:18","modified_gmt":"2016-11-06T11:32:18","slug":"net-core","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/net-core\/","title":{"rendered":".NET Core"},"content":{"rendered":"<p>This should be a pretty short post, just to outline using .NET core on Linux\/Ubuntu server via Docker.<\/p>\n<p>We could look to install .NET core via apt-get, but I&#8217;ve found it so much simpler running up a Docker container with .NET core already implemented in, so let&#8217;s do that.<\/p>\n<p>First up, we run<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndocker run -it microsoft\/dotnet:latest\r\n<\/pre>\n<p>This is an official Microsoft owned container. The use of :latest means we should get the latest version each time we run this command. The -it switch switches us straight into the Docker instance when it&#8217;s started, I\/e\/ into bash.<\/p>\n<p>Now this is great if you&#8217;re happy to lose any code within the Docker image when it&#8217;s removed, but if you want to link into your hosts file system it&#8217;s better to run<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndocker run -it -v \/home\/putridparrot\/dev:\/development microsoft\/dotnet:latest\r\n<\/pre>\n<p><em>Where \/home\/putridparrot\/dev on the left of the colon, is a folder on your host\/server filesystem and maps to a folder inside the Docker instance which will be named development (i.e. it acts like a link\/shortcut).<\/em><\/p>\n<p>Now when you are within the Docker instance you can save files to the host (and vice\/versa) and they&#8217;ll persist beyond the life of the Docker instance and also allow us a simply means of copying files from, say a Windows machine into the instance of dotnet on the Linux server.<\/p>\n<p>And that literally is that. <\/p>\n<p>But let&#8217;s write some code and run it to prove everything is working.<\/p>\n<p><em>To be honest, you should go and look at <a href=\"https:\/\/www.microsoft.com\/net\/core#windows\" target=\"_blank\">Install for Windows<\/a> (or one of the installs for Linux or Mac) as I&#8217;m pretty much going to recreate the documentation on running dotnet from these pages<\/em><\/p>\n<p>To run the .NET core framework, this includes compiling code, we use the command<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndotnet\r\n<\/pre>\n<p>Before we get going, .NET core is very much a preview\/RC release, so this is the version I&#8217;m currently using (there&#8217;s no guarantee this will work the same way in a production release), running <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndotnet --version\r\n<\/pre>\n<p>we get version <strong>1.0.0-preview2-003131<\/strong>.<\/p>\n<p><strong>Let&#8217;s create the standard first application<\/strong><\/p>\n<p>Now, navigate to home and then make a directory for some source code<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ncd \/home\r\nmkdir HelloWorld\r\ncd \/HelloWorld\r\n<\/pre>\n<p>yes, we&#8217;re going to write the usual, Hello World application, but that&#8217;s simply because the dotnet command has a built in &#8220;new project&#8221; which generates this for us. So run<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndotnet new\r\n<\/pre>\n<p>This creates two files, Program.cs looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\n\r\nnamespace ConsoleApplication\r\n{\r\n    public class Program\r\n    {\r\n        public static void Main(string&#x5B;] args)\r\n        {\r\n            Console.WriteLine(&quot;Hello World!&quot;);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>nothing particularly interesting here, i.e. its a standard Hello World implementation. <\/p>\n<p>However a second file is created (which is a little more interesting), the project.json, which looks like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;version&quot;: &quot;1.0.0-*&quot;,\r\n  &quot;buildOptions&quot;: {\r\n    &quot;debugType&quot;: &quot;portable&quot;,\r\n    &quot;emitEntryPoint&quot;: true\r\n  },\r\n  &quot;dependencies&quot;: {},\r\n  &quot;frameworks&quot;: {\r\n    &quot;netcoreapp1.0&quot;: {\r\n      &quot;dependencies&quot;: {\r\n        &quot;Microsoft.NETCore.App&quot;: {\r\n          &quot;type&quot;: &quot;platform&quot;,\r\n          &quot;version&quot;: &quot;1.0.1&quot;\r\n        }\r\n      },\r\n      &quot;imports&quot;: &quot;dnxcore50&quot;\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>Now, we need to run the following from the same folder as the project (as it will use the project.json file) <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndotnet restore\r\n<\/pre>\n<p>this will restore any packages required for the project to build. To build and run the program we use<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndotnet run\r\n<\/pre>\n<p><strong>What are the dotnet cmd options<\/strong><\/p>\n<p>Obviously you can run dotnet &#8211;help and find these out yourself, but just to give a quick overview, this is what you&#8217;ll see as a list of commands<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nCommon Commands:\r\n  new           Initialize a basic .NET project\r\n  restore       Restore dependencies specified in the .NET project\r\n  build         Builds a .NET project\r\n  publish       Publishes a .NET project for deployment (including the runtime)\r\n  run           Compiles and immediately executes a .NET project\r\n  test          Runs unit tests using the test runner specified in the project\r\n  pack          Creates a NuGet package\r\n<\/pre>\n<p><strong>.NET Core in Visual Studio 2015<\/strong><\/p>\n<p>Visual Studio (2015) offers three .NET core specific project templates, Class Library, Console application and ASP.NET Core.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/articles\/core\/tutorials\/using-with-xplat-cli\" target=\"_blank\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/articles\/core\/tutorials\/using-with-xplat-cli<\/a><br \/>\n<a href=\"https:\/\/docs.asp.net\/en\/latest\/getting-started.html\" target=\"_blank\">https:\/\/docs.asp.net\/en\/latest\/getting-started.html<\/a><br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/articles\/core\/tools\/project-json\" target=\"_blank\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/articles\/core\/tools\/project-json<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This should be a pretty short post, just to outline using .NET core on Linux\/Ubuntu server via Docker. We could look to install .NET core via apt-get, but I&#8217;ve found it so much simpler running up a Docker container with .NET core already implemented in, so let&#8217;s do that. First up, we run docker run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,102],"tags":[],"class_list":["post-4423","post","type-post","status-publish","format-standard","hentry","category-net","category-docker"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4423"}],"version-history":[{"count":12,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4423\/revisions"}],"predecessor-version":[{"id":4447,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4423\/revisions\/4447"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}