1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Diagnostics . Contracts ;
4
+ using System . Linq ;
2
5
using System . Reflection ;
3
6
using System . Web . Http ;
4
7
using System . Web . Http . Dispatcher ;
@@ -9,27 +12,36 @@ namespace ScriptCs.WebApi
9
12
{
10
13
public class WebApi : IScriptPackContext
11
14
{
12
-
13
- public HttpSelfHostServer CreateServer ( HttpSelfHostConfiguration config , Assembly caller = null )
15
+ public HttpSelfHostServer CreateServer ( HttpSelfHostConfiguration config , ICollection < Type > controllerTypes )
14
16
{
15
- if ( caller == null )
16
- {
17
- caller = Assembly . GetCallingAssembly ( ) ;
18
- }
17
+ Contract . Requires ( controllerTypes != null ) ;
18
+ Contract . Requires ( ControllerResolver . AllAssignableToIHttpController ( controllerTypes ) ) ;
19
19
20
- config . Services . Replace ( typeof ( IHttpControllerTypeResolver ) , new ControllerResolver ( caller ) ) ;
20
+ config . Services . Replace ( typeof ( IHttpControllerTypeResolver ) , new ControllerResolver ( controllerTypes ) ) ;
21
21
22
22
config . Routes . MapHttpRoute ( name : "DefaultApi" ,
23
23
routeTemplate : "{controller}/{id}" ,
24
24
defaults : new { id = RouteParameter . Optional }
25
25
) ;
26
+
26
27
return new HttpSelfHostServer ( config ) ;
27
28
}
28
29
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
+
29
40
public HttpSelfHostServer CreateServer ( string baseAddress )
30
41
{
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 ) ;
33
45
}
34
46
}
35
47
}
0 commit comments