{"id":1414,"date":"2016-02-24T11:35:22","date_gmt":"2016-02-24T16:35:22","guid":{"rendered":"http:\/\/codinggorilla.domemtech.com\/?p=1414"},"modified":"2016-03-06T12:31:00","modified_gmt":"2016-03-06T17:31:00","slug":"self-hosting-a-c-nancy-server-on-linux","status":"publish","type":"post","link":"http:\/\/165.227.223.229\/index.php\/2016\/02\/24\/self-hosting-a-c-nancy-server-on-linux\/","title":{"rendered":"Self-hosting a C# Nancy Server on Linux"},"content":{"rendered":"<p><a href=\"http:\/\/www.sinatrarb.com\/\" target=\"_blank\">Sinatra<\/a> is a popular and wonderful web application framework that has spawned a number of copycat frameworks. You can write large websites or REST servers with it. While it is a framework only for Ruby, there are alternatives for other languages, <a href=\"http:\/\/sparkjava.com\/\" target=\"_blank\">Spark Java<\/a>\u00c2\u00a0for Java, <a class=\"\" href=\"https:\/\/laravel.com\/\" target=\"_blank\">Lavarel<\/a> for PHP, etc. For C#, <a href=\"http:\/\/nancyfx.org\/\" target=\"_blank\">Nancy<\/a> is the choice, and you can avoid using ASP.NET if you&#8217;d like. This post\u00c2\u00a0shows how to set up a \u00e2\u20ac\u009cHello world!\u00e2\u20ac\u009d Nancy project that will build and run on Ubuntu and Windows.<\/p>\n<p>Prerequisites:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.virtualbox.org\/\" target=\"_blank\">VirtualBox<\/a>.<\/li>\n<li><a href=\"https:\/\/www.visualstudio.com\/\" target=\"_blank\">Microsoft Visual Studio 2015 Community Edition<\/a>.<\/li>\n<li>You will need to create a virtual machine with <a href=\"http:\/\/www.ubuntu.com\/download\" target=\"_blank\">Ubuntu<\/a>. The machine will need a mapped folder that you can create a project in using Visual Studio.<\/li>\n<li>Your virtual machine must have Mono installed. See <a href=\"http:\/\/codinggorilla.domemtech.com\/?p=1423\" target=\"_blank\">this post<\/a>.<\/li>\n<\/ul>\n<p><span style=\"line-height: 1.5;\">Method:<\/span><\/p>\n<ol>\n<li>In your Windows host machine, start Visual Studio.<\/li>\n<li>In Visual Studio:\n<ol>\n<li>Create a new C# console application &#8220;hn&#8221; within the mapped directory for the virtual machine.<\/li>\n<li>Replace the contents of Program.cs with the following code:\n<pre>using System;\r\nusing System.Collections.Generic;\r\nusing Nancy;\r\nusing Nancy.Hosting.Self;\r\n\r\nnamespace hn\r\n{\r\n    public class SampleModule : Nancy.NancyModule\r\n    {\r\n        public SampleModule()\r\n        {\r\n            Get[\"\/\"] = _ =&gt; \"Hello World!\";\r\n        }\r\n    }\r\n\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            var uri = new Uri(\"http:\/\/localhost:80\");\r\n            List list_uri = new List();\r\n            list_uri.Add(uri);\r\n\r\n            HostConfiguration hostConfigs = new HostConfiguration()\r\n            {\r\n                UrlReservations = new UrlReservations() { CreateAutomatically = true }\r\n            };\r\n\r\n            using (NancyHost host = new NancyHost(new DefaultNancyBootstrapper(), hostConfigs, list_uri.ToArray()))\r\n            {\r\n                host.Start();\r\n                foreach (Uri u in list_uri)\r\n                    Console.WriteLine(u);\r\n                Console.WriteLine(\"Press any [Enter] to close the host.\");\r\n                Console.ReadLine();\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n<\/li>\n<li>In order for this to compile, you will need to add two packages using the Package Manager.\n<ol>\n<li>Start the Package Manager in Visual Studio.<\/li>\n<li>At the prompt, type <em>Install-Package Nancy<\/em> and <em>Install-Package Nancy.Hosting.Self<\/em>.<\/li>\n<\/ol>\n<\/li>\n<li>Compile and run.<\/li>\n<li>In a browser, type in &#8220;http:\/\/localhost\/&#8221;.\n<ol>\n<li>Verify that your browser displays &#8220;Hello world!&#8221;.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<li>In the virtual machine guest:\n<ol>\n<li>Open a Terminal.<\/li>\n<li><code>cd &lt;mapped-directory&gt;\/&lt;project-directory&gt;<\/code>\n<ol>\n<li>Make sure the directory is mapped and you see the new project.<\/li>\n<\/ol>\n<\/li>\n<li><code>xbuild<\/code><\/li>\n<li><code>mono bin\/Debug\/&lt;project-executable&gt;<\/code><\/li>\n<li>In a browser, type in &#8220;http:\/\/localhost\/&#8221;.\n<ol>\n<li>Verify that your browser displays &#8220;Hello world!&#8221;.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<pre>dokku@kukku:\/vagrant\/hn$ ls\r\nApp.config  bin  hn.csproj  hn.sln  obj  packages  packages.config  Program.cs  Properties\r\ndokku@kukku:\/vagrant\/hn$ xbuild \/target:Clean\r\nXBuild Engine Version 12.0\r\nMono, Version 4.2.2.0\r\nCopyright (C) 2005-2013 Various Mono authors\r\n\r\nBuild started 2\/24\/2016 10:20:58 AM.\r\n__________________________________________________\r\nProject \"\/vagrant\/hn\/hn.sln\" (Clean target(s)):\r\n\tTarget ValidateSolutionConfiguration:\r\n\t\tBuilding solution configuration \"Debug|Any CPU\".\r\n\tTarget Clean:\r\n\t\tProject \"\/vagrant\/hn\/hn.csproj\" (Clean target(s)):\r\n\/vagrant\/hn\/hn.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.\r\n\t\t\tTarget CoreClean:\r\n\t\t\t\tDeleting file '\/vagrant\/hn\/obj\/Debug\/.NETFramework,Version=v4.5.AssemblyAttribute.cs'\r\n\t\t\t\tDeleting file '\/vagrant\/hn\/obj\/Debug\/hn.exe'\r\n\t\t\t\tDeleting file '\/vagrant\/hn\/obj\/Debug\/hn.exe.mdb'\r\n\t\t\t\tDeleting file '\/vagrant\/hn\/obj\/Debug\/hn.csproj.FilesWrittenAbsolute.txt'\r\n\t\tDone building project \"\/vagrant\/hn\/hn.csproj\".\r\nDone building project \"\/vagrant\/hn\/hn.sln\".\r\n\r\nBuild succeeded.\r\n\r\nWarnings:\r\n\r\n\/vagrant\/hn\/hn.sln (Clean) -&gt;\r\n(Clean target) -&gt;\r\n\/vagrant\/hn\/hn.csproj (Clean) -&gt;\r\n\r\n\t\/vagrant\/hn\/hn.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.\r\n\r\n\t 1 Warning(s)\r\n\t 0 Error(s)\r\n\r\nTime Elapsed 00:00:00.5218600\r\ndokku@kukku:\/vagrant\/hn$ xbuild\r\nXBuild Engine Version 12.0\r\nMono, Version 4.2.2.0\r\nCopyright (C) 2005-2013 Various Mono authors\r\n\r\nBuild started 2\/24\/2016 10:21:05 AM.\r\n__________________________________________________\r\nProject \"\/vagrant\/hn\/hn.sln\" (default target(s)):\r\n\tTarget ValidateSolutionConfiguration:\r\n\t\tBuilding solution configuration \"Debug|Any CPU\".\r\n\tTarget Build:\r\n\t\tProject \"\/vagrant\/hn\/hn.csproj\" (default target(s)):\r\n\/vagrant\/hn\/hn.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.\r\n\t\t\tTarget PrepareForBuild:\r\n\t\t\t\tConfiguration: Debug Platform: AnyCPU\r\n\t\t\tTarget GenerateSatelliteAssemblies:\r\n\t\t\tNo input files were specified for target GenerateSatelliteAssemblies, skipping.\r\n\t\t\tTarget CoreCompile:\r\n\t\t\t\tTool \/usr\/lib\/mono\/4.5\/mcs.exe execution started with arguments: \/noconfig \/debug:full \/debug+ \/optimize- \/out:obj\/Debug\/hn.exe Program.cs Properties\/AssemblyInfo.cs obj\/Debug\/.NETFramework,Version=v4.5.AssemblyAttribute.cs \/target:exe \/define:\"DEBUG;TRACE\" \/nostdlib \/platform:AnyCPU \/reference:packages\/Nancy.1.4.3\/lib\/net40\/Nancy.dll \/reference:packages\/Nancy.Hosting.Self.1.4.1\/lib\/net40\/Nancy.Hosting.Self.dll \/reference:\/usr\/lib\/mono\/4.5\/System.dll \/reference:\/usr\/lib\/mono\/4.5\/System.Xml.Linq.dll \/reference:\/usr\/lib\/mono\/4.5\/System.Data.DataSetExtensions.dll \/reference:\/usr\/lib\/mono\/4.5\/Microsoft.CSharp.dll \/reference:\/usr\/lib\/mono\/4.5\/System.Data.dll \/reference:\/usr\/lib\/mono\/4.5\/System.Net.Http.dll \/reference:\/usr\/lib\/mono\/4.5\/System.Xml.dll \/reference:\/usr\/lib\/mono\/4.5\/System.Core.dll \/reference:\/usr\/lib\/mono\/4.5\/mscorlib.dll \/warn:4\r\n\t\t\tTarget _CopyAppConfigFile:\r\n\t\t\tSkipping target \"_CopyAppConfigFile\" because its outputs are up-to-date.\r\n\t\t\tTarget DeployOutputFiles:\r\n\t\t\t\tCopying file from '\/vagrant\/hn\/obj\/Debug\/hn.exe.mdb' to '\/vagrant\/hn\/bin\/Debug\/hn.exe.mdb'\r\n\t\t\t\tCopying file from '\/vagrant\/hn\/obj\/Debug\/hn.exe' to '\/vagrant\/hn\/bin\/Debug\/hn.exe'\r\n\t\tDone building project \"\/vagrant\/hn\/hn.csproj\".\r\nDone building project \"\/vagrant\/hn\/hn.sln\".\r\n\r\nBuild succeeded.\r\n\r\nWarnings:\r\n\r\n\/vagrant\/hn\/hn.sln (default targets) -&gt;\r\n(Build target) -&gt;\r\n\/vagrant\/hn\/hn.csproj (default targets) -&gt;\r\n\r\n\t\/vagrant\/hn\/hn.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.\r\n\r\n\t 1 Warning(s)\r\n\t 0 Error(s)\r\n\r\nTime Elapsed 00:00:01.0865300\r\ndokku@kukku:\/vagrant\/hn$ mono bin\/Debug\/hn.exe\r\n\r\nUnhandled Exception:\r\nSystem.Net.Sockets.SocketException: Access denied\r\n  at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end)  in :0 \r\n  at System.Net.EndPointListener..ctor (System.Net.IPAddress addr, Int32 port, Boolean secure)  in :0 \r\n  at System.Net.EndPointManager.GetEPListener (System.String host, Int32 port, System.Net.HttpListener listener, Boolean secure)  in :0 \r\n  at System.Net.EndPointManager.AddPrefixInternal (System.String p, System.Net.HttpListener listener)  in :0 \r\n  at System.Net.EndPointManager.AddListener (System.Net.HttpListener listener)  in :0 \r\n[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException: Access denied\r\n  at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end)  in :0 \r\n  at System.Net.EndPointListener..ctor (System.Net.IPAddress addr, Int32 port, Boolean secure)  in :0 \r\n  at System.Net.EndPointManager.GetEPListener (System.String host, Int32 port, System.Net.HttpListener listener, Boolean secure)  in :0 \r\n  at System.Net.EndPointManager.AddPrefixInternal (System.String p, System.Net.HttpListener listener)  in :0 \r\n  at System.Net.EndPointManager.AddListener (System.Net.HttpListener listener)  in :0 \r\ndokku@kukku:\/vagrant\/hn$ sudo mono bin\/Debug\/hn.exe\r\nhttp:\/\/localhost\/\r\nPress any [Enter] to close the host.\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"aligncenter size-large wp-image-1424\" src=\"http:\/\/codinggorilla.domemtech.com\/wordpress\/wp-content\/uploads\/2016\/02\/2016-02-24-1-1024x576.png\" alt=\"2016-02-24 (1)\" width=\"676\" height=\"380\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Code for this example:<\/p>\n<ul>\n<li><a href=\"https:\/\/bitbucket.org\/ken_domino\/hn\/src\" target=\"_blank\">https:\/\/bitbucket.org\/ken_domino\/hn\/src<\/a><\/li>\n<\/ul>\n<p>For more information:<\/p>\n<ul>\n<li>VirtualBox &#8211; <a href=\"https:\/\/www.virtualbox.org\/\" target=\"_blank\">main page<\/a><\/li>\n<li>Mono Project &#8211; <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mono_(software)\" target=\"_blank\">Wikipedia<\/a>, <a href=\"http:\/\/www.mono-project.com\/\" target=\"_blank\">main page<\/a><\/li>\n<li>Ubuntu &#8212; <a href=\"http:\/\/www.ubuntu.com\/\" target=\"_blank\">main page<\/a><\/li>\n<li>Visual Studio Community &#8211; <a href=\"https:\/\/www.visualstudio.com\/\" target=\"_blank\">main page<\/a><\/li>\n<li>Nancy &#8212; <a href=\"http:\/\/nancyfx.org\/\" target=\"_blank\">main page<\/a><\/li>\n<li>Sinatra &#8212; <a href=\"http:\/\/www.sinatrarb.com\/\" target=\"_blank\">main page<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sinatra is a popular and wonderful web application framework that has spawned a number of copycat frameworks. You can write large websites or REST servers with it. While it is a framework only for Ruby, there are alternatives for other languages, Spark Java\u00c2\u00a0for Java, Lavarel for PHP, etc. For C#, Nancy is the choice, and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/165.227.223.229\/index.php\/2016\/02\/24\/self-hosting-a-c-nancy-server-on-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Self-hosting a C# Nancy Server on Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1414"}],"collection":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/comments?post=1414"}],"version-history":[{"count":0,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1414\/revisions"}],"wp:attachment":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/media?parent=1414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/categories?post=1414"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/tags?post=1414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}