diff --git a/Unosquare.Labs.EmbedIO.Samples/Unosquare.Labs.EmbedIO.Samples.csproj b/Unosquare.Labs.EmbedIO.Samples/Unosquare.Labs.EmbedIO.Samples.csproj
index 4b02da994..76af54d27 100644
--- a/Unosquare.Labs.EmbedIO.Samples/Unosquare.Labs.EmbedIO.Samples.csproj
+++ b/Unosquare.Labs.EmbedIO.Samples/Unosquare.Labs.EmbedIO.Samples.csproj
@@ -47,7 +47,7 @@
True
- ..\packages\Tubular.ServerSide.0.9.7\lib\net45\Unosquare.Tubular.dll
+ ..\packages\Tubular.ServerSide.0.9.14\lib\net45\Unosquare.Tubular.dll
True
diff --git a/Unosquare.Labs.EmbedIO.Samples/packages.config b/Unosquare.Labs.EmbedIO.Samples/packages.config
index debd2d4a5..3df7feace 100644
--- a/Unosquare.Labs.EmbedIO.Samples/packages.config
+++ b/Unosquare.Labs.EmbedIO.Samples/packages.config
@@ -2,6 +2,6 @@
-
-
+
+
\ No newline at end of file
diff --git a/Unosquare.Labs.EmbedIO.Tests/FluentTest.cs b/Unosquare.Labs.EmbedIO.Tests/FluentTest.cs
index c0da544f8..9e1f30b03 100644
--- a/Unosquare.Labs.EmbedIO.Tests/FluentTest.cs
+++ b/Unosquare.Labs.EmbedIO.Tests/FluentTest.cs
@@ -48,7 +48,7 @@ public void FluentWithWebApi()
Assert.AreEqual(webServer.Modules.Count, 1, "It has 1 modules loaded");
Assert.IsNotNull(webServer.Module(), "It has WebApiModule");
- Assert.AreEqual(webServer.Module().ControllersCount, 1, "It has one controller");
+ Assert.AreEqual(webServer.Module().ControllersCount, 2, "It has two controllers");
webServer.Dispose();
}
@@ -74,7 +74,7 @@ public void FluentLoadWebApiControllers()
Assert.AreEqual(webServer.Modules.Count, 1, "It has 1 modules loaded");
Assert.IsNotNull(webServer.Module(), "It has WebApiModule");
- Assert.AreEqual(webServer.Module().ControllersCount, 1, "It has one controller");
+ Assert.AreEqual(webServer.Module().ControllersCount, 2, "It has two controllers");
webServer.Dispose();
}
diff --git a/Unosquare.Labs.EmbedIO.Tests/TestObjects/TestController.cs b/Unosquare.Labs.EmbedIO.Tests/TestObjects/TestController.cs
index 5ef792422..7c42b1e57 100644
--- a/Unosquare.Labs.EmbedIO.Tests/TestObjects/TestController.cs
+++ b/Unosquare.Labs.EmbedIO.Tests/TestObjects/TestController.cs
@@ -74,4 +74,20 @@ public bool PostPeople(WebServer server, HttpListenerContext context)
}
}
}
+
+ public class TestControllerWithConstructor : WebApiController
+ {
+ public string WebName { get; set; }
+
+ public TestControllerWithConstructor(string name)
+ {
+ WebName = name;
+ }
+
+ [WebApiHandler(HttpVerbs.Get, "/name")]
+ public bool GetPeople(WebServer server, HttpListenerContext context)
+ {
+ return context.JsonResponse(WebName);
+ }
+ }
}
diff --git a/Unosquare.Labs.EmbedIO.Tests/Unosquare.Labs.EmbedIO.Tests.csproj b/Unosquare.Labs.EmbedIO.Tests/Unosquare.Labs.EmbedIO.Tests.csproj
index 5b88c9b3b..2183a1831 100644
--- a/Unosquare.Labs.EmbedIO.Tests/Unosquare.Labs.EmbedIO.Tests.csproj
+++ b/Unosquare.Labs.EmbedIO.Tests/Unosquare.Labs.EmbedIO.Tests.csproj
@@ -30,8 +30,8 @@
4
-
- ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll
+
+ ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll
True
diff --git a/Unosquare.Labs.EmbedIO.Tests/WebApiModuleTest.cs b/Unosquare.Labs.EmbedIO.Tests/WebApiModuleTest.cs
index 8bc5d114c..19c8a64e9 100644
--- a/Unosquare.Labs.EmbedIO.Tests/WebApiModuleTest.cs
+++ b/Unosquare.Labs.EmbedIO.Tests/WebApiModuleTest.cs
@@ -102,6 +102,25 @@ public void PostJsonData()
}
}
+ [Test]
+ public void TestWebApiWithConstructor()
+ {
+ const string name = "Test";
+
+ WebServer.Module().RegisterController(() => new TestControllerWithConstructor(name));
+
+ var request = (HttpWebRequest) WebRequest.Create(Resources.ServerAddress + "name");
+
+ using (var response = (HttpWebResponse) request.GetResponse())
+ {
+ Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");
+
+ var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
+
+ Assert.AreEqual(body, name);
+ }
+ }
+
[TearDown]
public void Kill()
{
diff --git a/Unosquare.Labs.EmbedIO.Tests/packages.config b/Unosquare.Labs.EmbedIO.Tests/packages.config
index 24073f522..3b829b9ff 100644
--- a/Unosquare.Labs.EmbedIO.Tests/packages.config
+++ b/Unosquare.Labs.EmbedIO.Tests/packages.config
@@ -1,5 +1,5 @@
-
+
\ No newline at end of file
diff --git a/Unosquare.Labs.EmbedIO/Modules/WebApiModule.cs b/Unosquare.Labs.EmbedIO/Modules/WebApiModule.cs
index 6f5c41ae8..8634a0bfb 100644
--- a/Unosquare.Labs.EmbedIO/Modules/WebApiModule.cs
+++ b/Unosquare.Labs.EmbedIO/Modules/WebApiModule.cs
@@ -1,263 +1,263 @@
-namespace Unosquare.Labs.EmbedIO.Modules
-{
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using Unosquare.Labs.EmbedIO;
-
- ///
- /// A very simple module to register class methods as handlers.
- /// Public instance methods that match the WebServerModule.ResponseHandler signature, and have the WebApi handler attribute
- /// will be used to respond to web server requests
- ///
- public class WebApiModule : WebModuleBase
- {
- private readonly List ControllerTypes = new List();
-
- private readonly Dictionary, MethodInfo>>> DelegateMap
- =
- new Dictionary, MethodInfo>>>(
- StringComparer.InvariantCultureIgnoreCase);
-
- ///
- /// Initializes a new instance of the class.
- ///
- public WebApiModule()
- : base()
- {
- this.AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, (server, context) =>
- {
- var path = context.RequestPath();
- var verb = context.RequestVerb();
- var wildcardPaths = DelegateMap.Keys
- .Where(k => k.EndsWith("/" + ModuleMap.AnyPath))
- .Select(s => s.ToLowerInvariant())
- .ToArray();
-
- var wildcardMatch = wildcardPaths.FirstOrDefault(p => path.StartsWith(p.Substring(0, p.Length - 1)));
-
- if (string.IsNullOrWhiteSpace(wildcardMatch) == false)
- path = wildcardMatch;
-
- if (DelegateMap.ContainsKey(path) == false)
- return false;
-
- if (DelegateMap[path].ContainsKey(verb) == false) // TODO: Fix Any Verb
- {
- var originalPath = context.RequestPath();
- if (DelegateMap.ContainsKey(originalPath) &&
- DelegateMap[originalPath].ContainsKey(verb))
- {
- path = originalPath;
- }
- else
- return false;
- }
-
- var methodPair = DelegateMap[path][verb];
- var controller = methodPair.Item1();
-
- if (methodPair.Item2.ReturnType == typeof(Task))
- {
- var method = Delegate.CreateDelegate(typeof(AsyncResponseHandler), controller, methodPair.Item2);
-
- server.Log.DebugFormat("Handler: {0}.{1}", method.Method.DeclaringType.FullName, method.Method.Name);
- context.NoCache();
- var returnValue = Task.Run(async () =>
- {
- var task = await (Task)method.DynamicInvoke(server, context);
- return task;
- });
-
- return returnValue.Result;
- }
- else
- {
- var method = Delegate.CreateDelegate(typeof(ResponseHandler), controller, methodPair.Item2);
-
- server.Log.DebugFormat("Handler: {0}.{1}", method.Method.DeclaringType.FullName, method.Method.Name);
- context.NoCache();
- var returnValue = (bool)method.DynamicInvoke(server, context);
- return returnValue;
- }
- });
- }
-
- ///
- /// Gets the name of this module.
- ///
- ///
- /// The name.
- ///
- public override string Name
- {
- get { return "Web API Module"; }
- }
-
- ///
- /// Gets the controllers count
- ///
- public int ControllersCount
- {
- get { return ControllerTypes.Count; }
- }
-
- ///
- /// Registers the controller.
- ///
- ///
- /// Controller types must be unique within the module
- public void RegisterController()
- where T : WebApiController, new()
- {
- if (ControllerTypes.Contains(typeof(T)))
- throw new ArgumentException("Controller types must be unique within the module");
-
- RegisterController(typeof(T));
- }
-
- ///
- /// Registers the controller.
- ///
- ///
- ///
- /// Controller types must be unique within the module
- public void RegisterController(Func controllerFactory)
- where T : WebApiController
- {
- if (ControllerTypes.Contains(typeof(T)))
- throw new ArgumentException("Controller types must be unique within the module");
-
- RegisterController(typeof(T), controllerFactory);
- }
-
- ///
- /// Registers the controller.
- ///
- /// Type of the controller.
- public void RegisterController(Type controllerType)
- {
- Func