From 2e9cd6b1d16e173e1da8cd698f6e46bb990717c4 Mon Sep 17 00:00:00 2001 From: Gustavo Jimenez Date: Sat, 19 Aug 2017 01:45:36 +0200 Subject: [PATCH 1/3] Check if port is being used by the system before start to avoid exceptions --- ToDo.Server/Program.cs | 62 ++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/ToDo.Server/Program.cs b/ToDo.Server/Program.cs index 1ecbd82..6341cc2 100755 --- a/ToDo.Server/Program.cs +++ b/ToDo.Server/Program.cs @@ -1,20 +1,54 @@ using System; using Microsoft.Owin.Hosting; +using System.Net.NetworkInformation; +using System.Net; namespace TodoApi { - public class Program - { - protected static void Main(string[] args) - { - var port = 8080; - var url = $"http://localhost:{port}"; - using (WebApp.Start(url)) - { - Console.WriteLine($"Web Server is running at {url}."); - Console.WriteLine("Press any key to quit."); - Console.ReadLine(); - } - } - } + public class Program + { + protected static void Main(string[] args) + { + var port = 8080; + var url = $"http://localhost:{port}"; + + if (IsPortAvailable(port)) + { + using (WebApp.Start(url)) + { + Console.WriteLine($"Web Server is running at {url}."); + Console.WriteLine("Press any key to quit."); + Console.ReadLine(); + } + } + else + { + Console.WriteLine($"The port {port} is being used by your system. Try with another port"); + Console.WriteLine("Press any key to quit."); + Console.ReadLine(); + } + + + } + + /// + /// Check if the port is being used by the system + /// + /// TCP port + /// + private static bool IsPortAvailable(int port) + { + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] tcpListenersEndPoints = ipGlobalProperties.GetActiveTcpListeners(); + + foreach (var tcpEp in tcpListenersEndPoints) + { + if (tcpEp.Port == port) + return false; + } + + return true; + } + } } + From fb99c4e301fe16fcfbddf441fcce845825d24142 Mon Sep 17 00:00:00 2001 From: Gustavo Jimenez Date: Sat, 19 Aug 2017 01:50:46 +0200 Subject: [PATCH 2/3] Fix: when receiving a POST request to create an item the server failed to return the newly created object. Although the object was inserted in DB an exception was thrown: Route named 'GetTodo' could not be found in the route collection. Change the route 'GetTodo' to 'DefaultApi' to point to the default route and to return the newly created object --- ToDo.Server/Controllers/TodoController.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ToDo.Server/Controllers/TodoController.cs b/ToDo.Server/Controllers/TodoController.cs index 8a8333c..1776795 100755 --- a/ToDo.Server/Controllers/TodoController.cs +++ b/ToDo.Server/Controllers/TodoController.cs @@ -44,10 +44,10 @@ public IHttpActionResult Create([FromBody] TodoItem item) return BadRequest(); } TodoItems.Add(item); - return CreatedAtRoute("GetTodo", new { id = item.Key }, item); - } + return CreatedAtRoute("DefaultApi", new { id = item.Key }, item); + } - [HttpPut] + [HttpPut] [Route("{id}")] public IHttpActionResult Update(string id, [FromBody] TodoItem item) { From 572ac7ef0b80ffd398613ccd1de5847e436312d4 Mon Sep 17 00:00:00 2001 From: Gustavo Jimenez Date: Sat, 19 Aug 2017 01:56:41 +0200 Subject: [PATCH 3/3] Fix: A call to UPDATE throwed a duplicated route exception Replace duplicated PUT signature of the Update method. Replaced by a PUT method using the default route with only the item as parameter --- ToDo.Server/Controllers/TodoController.cs | 35 ++++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/ToDo.Server/Controllers/TodoController.cs b/ToDo.Server/Controllers/TodoController.cs index 1776795..64e51c8 100755 --- a/ToDo.Server/Controllers/TodoController.cs +++ b/ToDo.Server/Controllers/TodoController.cs @@ -47,26 +47,27 @@ public IHttpActionResult Create([FromBody] TodoItem item) return CreatedAtRoute("DefaultApi", new { id = item.Key }, item); } - [HttpPut] - [Route("{id}")] - public IHttpActionResult Update(string id, [FromBody] TodoItem item) - { - if (item == null || item.Key != id) - { - return BadRequest(); - } + /// + /// Update the given item using the default PUT route + /// + /// the item to update with the modified data + /// 200 OK if success + [HttpPut] + [Route("{id}")] + public IHttpActionResult Update([FromBody] TodoItem item) + { + if (item == null) + return BadRequest(); - var todo = TodoItems.Find(id); - if (todo == null) - { - return NotFound(); - } + var todo = TodoItems.Find(item.Key); + if (todo == null) + return NotFound(); - TodoItems.Update(item); - return Ok(); - } + TodoItems.Update(item); + return Ok(); + } - [HttpPut] + [HttpPut] [Route("{id}")] public IHttpActionResult Update([FromBody] TodoItem item, string id) {