11using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics . Contracts ;
4+ using System . Linq ;
25using System . Reflection ;
36using System . Web . Http ;
47using 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