Skip to content

Commit ae3541b

Browse files
committed
Merge pull request #2 from panesofglass/who-needs-assemblies
Allow direct specification of controller types
2 parents 63395f4 + a73ba7a commit ae3541b

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ server.OpenAsync().Wait();
3636

3737
Console.WriteLine("Listening...");
3838
Console.ReadKey();
39+
server.CloseAsync().Wait();
3940
```
4041
* Running as admin type ```scriptcs start.csx``` to launch the app.
4142
* Open a browser to "http://localhost:8080/test";
4243
* That's it, your API is up!
4344

4445
## Customizing
45-
You can customize the host by modifying the configuration object. Or if you would like to pass your own you can use the CreateServer overload.
46-
If you pass your own, the ControllerHttpResolver will be replaced with a script friendly one.
46+
You can customize the host by modifying the configuration object.
47+
Or if you would like to pass your own you can use the `CreateServer` overload.
48+
Additional `CreateServer` overloads allow you to explicitly specify assemblies or `IHttpController` types you want to expose in your api:
49+
50+
```csharp
51+
// Use a custom configuration and specify controller types.
52+
var config = new HttpSelfHostConfiguration("http://localhost:8080");
53+
var controllers = new List<Type> { typeof(TestController) };
54+
var server = webApi.CreateServer(config, controllers)
55+
```
4756

4857
## What's next
4958
* Create a nuget package.
+24-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.Contracts;
34
using System.Linq;
4-
using System.Reflection;
5-
using System.Text;
6-
using System.Threading.Tasks;
75
using System.Web.Http.Dispatcher;
86

97
namespace ScriptCs.WebApi
108
{
11-
public class ControllerResolver : DefaultHttpControllerTypeResolver
9+
internal class ControllerResolver : DefaultHttpControllerTypeResolver
1210
{
13-
private Assembly _scriptAssembly;
11+
private ICollection<Type> _controllerTypes;
1412

15-
public ControllerResolver(Assembly scriptAssembly)
13+
internal ControllerResolver(ICollection<Type> controllerTypes)
1614
{
17-
_scriptAssembly = scriptAssembly;
15+
Contract.Requires(AllAssignableToIHttpController(controllerTypes));
16+
17+
_controllerTypes = controllerTypes;
1818
}
1919

2020
public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver)
2121
{
22-
var types = _scriptAssembly.GetTypes().ToList();
23-
var controllers = types.Where(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x)).ToList();
24-
return controllers;
22+
Contract.Invariant(_controllerTypes != null);
23+
24+
return _controllerTypes;
25+
}
26+
27+
internal static IEnumerable<Type> WhereControllerType(IEnumerable<Type> types)
28+
{
29+
Contract.Requires(types != null);
30+
31+
return types.Where(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x));
32+
}
33+
34+
internal static bool AllAssignableToIHttpController(IEnumerable<Type> types)
35+
{
36+
Contract.Requires(types != null);
37+
38+
return types.All(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x));
2539
}
2640
}
2741
}

src/ScriptCs.WebApi/WebApi.cs

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.Contracts;
4+
using System.Linq;
25
using System.Reflection;
36
using System.Web.Http;
47
using System.Web.Http.Dispatcher;
@@ -9,27 +12,36 @@ namespace ScriptCs.WebApi
912
{
1013
public class WebApi : IScriptPackContext
1114
{
12-
13-
public HttpSelfHostServer CreateServer(HttpSelfHostConfiguration config, Assembly caller = null)
15+
public HttpSelfHostServer CreateServer(HttpSelfHostConfiguration config, ICollection<Type> controllerTypes)
1416
{
15-
if (caller == null)
16-
{
17-
caller = Assembly.GetCallingAssembly();
18-
}
17+
Contract.Requires(controllerTypes != null);
18+
Contract.Requires(ControllerResolver.AllAssignableToIHttpController(controllerTypes));
1919

20-
config.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver(caller));
20+
config.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver(controllerTypes));
2121

2222
config.Routes.MapHttpRoute(name: "DefaultApi",
2323
routeTemplate: "{controller}/{id}",
2424
defaults: new { id = RouteParameter.Optional }
2525
);
26+
2627
return new HttpSelfHostServer(config);
2728
}
2829

30+
public HttpSelfHostServer CreateServer(HttpSelfHostConfiguration config, params Assembly[] assemblies)
31+
{
32+
var types = assemblies.Length == 0 ?
33+
Assembly.GetCallingAssembly().GetTypes() :
34+
assemblies.SelectMany(a => a.GetTypes()).ToArray();
35+
36+
var controllerTypes = ControllerResolver.WhereControllerType(types).ToList();
37+
return CreateServer(config, controllerTypes);
38+
}
39+
2940
public HttpSelfHostServer CreateServer(string baseAddress)
3041
{
31-
var caller = Assembly.GetCallingAssembly();
32-
return CreateServer(new HttpSelfHostConfiguration(baseAddress), caller);
42+
var types = Assembly.GetCallingAssembly().GetTypes();
43+
var controllerTypes = ControllerResolver.WhereControllerType(types).ToList();
44+
return CreateServer(new HttpSelfHostConfiguration(baseAddress), controllerTypes);
3345
}
3446
}
3547
}

0 commit comments

Comments
 (0)