@@ -60,6 +60,7 @@ public override void webExecute()
6060 {
6161#if NETCORE
6262 GxRestWrapper handler = null ;
63+ GXBaseObject worker = null ;
6364#else
6465 Utils . GxRestService handler = null ;
6566#endif
@@ -77,25 +78,35 @@ public override void webExecute()
7778 parmsColl . FromJSonString ( jsonStr ) ;
7879 }
7980 }
80- #if NETCORE
81-
82- handler = GetRouting ( ) . GetController ( context . HttpContext , new ControllerInfo ( ) { Name = gxobj . Replace ( '.' , Path . DirectorySeparatorChar ) } ) ;
83- if ( handler == null ) {
84- throw new GxClassLoaderException ( $ "{ gxobj } not found") ;
85- }
86- #else
81+ string servicesType = gxobj + "_services" ;
8782 string nspace ;
8883 if ( ! Config . GetValueOf ( "AppMainNamespace" , out nspace ) )
8984 nspace = "GeneXus.Programs" ;
90- handler = ( GxRestService ) ClassLoader . FindInstance ( gxobj , nspace , gxobj + "_services" , null , null ) ;
91- #endif
92- handler . RunAsMain = false ;
93-
9485#if NETCORE
95- GXBaseObject worker = handler . Worker ;
86+ if ( RestAPIHelpers . ServiceAsController ( ) )
87+ {
88+ worker = CreateWorkerInstance ( nspace , gxobj ) ;
89+ if ( worker == null )
90+ {
91+ throw new GxClassLoaderException ( $ "{ gxobj } not found") ;
92+ }
93+ }
94+ else
95+ {
96+ handler = GetRouting ( ) . GetController ( context . HttpContext , new ControllerInfo ( ) { Name = gxobj . Replace ( '.' , Path . DirectorySeparatorChar ) } ) ;
97+ if ( handler == null )
98+ {
99+ throw new GxClassLoaderException ( $ "{ gxobj } not found") ;
100+ }
101+ worker = handler . Worker ;
102+ worker . IsMain = false ;
103+ }
96104#else
105+ handler = ( GxRestService ) ClassLoader . FindInstance ( gxobj , nspace , servicesType , null , null ) ;
106+ handler . RunAsMain = false ;
97107 GxRestService worker = handler ;
98108#endif
109+
99110 ParameterInfo [ ] pars = worker . GetType ( ) . GetMethod ( EXECUTE_METHOD ) . GetParameters ( ) ;
100111
101112 int ParmsCount = pars . Length ;
@@ -126,13 +137,46 @@ public override void webExecute()
126137 }
127138 finally
128139 {
140+ #if NETCORE
141+ if ( worker != null )
142+ {
143+ worker . IsMain = true ;
144+ worker . cleanup ( ) ;
145+ }
146+ #else
129147 if ( handler != null )
130148 {
131149 handler . RunAsMain = true ;
132150 handler . Cleanup ( ) ;
133151 }
152+ #endif
153+ }
154+ }
155+ #if NETCORE
156+ const string SERVICES_SUFFIX = "_services" ;
157+
158+ internal static GXBaseObject CreateWorkerInstance ( string nspace , string gxobj )
159+ {
160+ string svcFile = new FileInfo ( Path . Combine ( GXRouting . ContentRootPath , $ "{ gxobj } .svc") ) . FullName ;
161+ if ( File . Exists ( svcFile ) )
162+ {
163+
164+ string [ ] serviceAssemblyQualifiedName = new string ( File . ReadLines ( svcFile ) . First ( ) . SkipWhile ( c => c != '"' )
165+ . Skip ( 1 )
166+ . TakeWhile ( c => c != '"' )
167+ . ToArray ( ) ) . Trim ( ) . Split ( ',' ) ;
168+ string serviceAssemblyName = serviceAssemblyQualifiedName . Last ( ) ;
169+ string serviceClassName = serviceAssemblyQualifiedName . First ( ) ;
170+ if ( ! string . IsNullOrEmpty ( nspace ) && serviceClassName . StartsWith ( nspace ) )
171+ serviceClassName = serviceClassName . Substring ( nspace . Length + 1 ) ;
172+ else
173+ nspace = string . Empty ;
174+ string workerClassName = serviceClassName . Substring ( 0 , serviceClassName . Length - SERVICES_SUFFIX . Length ) ;
175+ return ( GXBaseObject ) ClassLoader . FindInstance ( serviceAssemblyName , nspace , workerClassName , null , null ) ;
134176 }
177+ return null ;
135178 }
179+ #endif
136180
137181 }
138182
0 commit comments