Skip to content

Commit baf898d

Browse files
Add a mechanism to register native controllers. (#977)
* Add a mechanism to register native controllers. * Add a unit test for native controller.
1 parent 45054aa commit baf898d

File tree

7 files changed

+82
-3
lines changed

7 files changed

+82
-3
lines changed

dotnet/DotNetStandardClasses.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCoreCmdTest", "test\D
259259
EndProject
260260
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{46DAAFD1-FAF5-4904-8EC5-406BE04E5538}"
261261
EndProject
262-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogTest", "test\benchmarks\LogTest\LogTest.csproj", "{A1DBDCE0-4F09-445F-A202-9B260CDD46CF}"
262+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogTest", "test\benchmarks\LogTest\LogTest.csproj", "{A1DBDCE0-4F09-445F-A202-9B260CDD46CF}"
263+
EndProject
264+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessTokenController_Test", "test\NativeAccessControllerTest\AccessTokenController_Test.csproj", "{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}"
263265
EndProject
264266
Global
265267
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -635,6 +637,10 @@ Global
635637
{A1DBDCE0-4F09-445F-A202-9B260CDD46CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
636638
{A1DBDCE0-4F09-445F-A202-9B260CDD46CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
637639
{A1DBDCE0-4F09-445F-A202-9B260CDD46CF}.Release|Any CPU.Build.0 = Release|Any CPU
640+
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
641+
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
642+
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
643+
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Release|Any CPU.Build.0 = Release|Any CPU
638644
EndGlobalSection
639645
GlobalSection(SolutionProperties) = preSolution
640646
HideSolutionNode = FALSE
@@ -760,6 +766,7 @@ Global
760766
{956402BD-AC8C-426E-961B-B77B3F3EDAEB} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
761767
{46DAAFD1-FAF5-4904-8EC5-406BE04E5538} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
762768
{A1DBDCE0-4F09-445F-A202-9B260CDD46CF} = {46DAAFD1-FAF5-4904-8EC5-406BE04E5538}
769+
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
763770
EndGlobalSection
764771
GlobalSection(ExtensibilityGlobals) = postSolution
765772
SolutionGuid = {E18684C9-7D76-45CD-BF24-E3944B7F174C}

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Net;
5+
using System.Reflection;
56
using System.Threading.Tasks;
67
using GeneXus.Configuration;
78
using GeneXus.Http;
@@ -143,6 +144,7 @@ public class Startup
143144
const string CORS_POLICY_NAME = "AllowSpecificOriginsPolicy";
144145
const string CORS_ANY_ORIGIN = "*";
145146
const double CORS_MAX_AGE_SECONDS = 86400;
147+
internal const string GX_CONTROLLERS = "gxcontrollers";
146148

147149
public List<string> servicesBase = new List<string>();
148150

@@ -161,7 +163,24 @@ public void ConfigureServices(IServiceCollection services)
161163
{
162164
OpenTelemetryService.Setup(services);
163165

164-
services.AddMvc(option => option.EnableEndpointRouting = false);
166+
services.AddControllers();
167+
string controllers = Path.Combine(Startup.LocalPath, "bin", GX_CONTROLLERS);
168+
IMvcBuilder mvcBuilder = services.AddMvc(option => option.EnableEndpointRouting = false);
169+
try
170+
{
171+
if (Directory.Exists(controllers))
172+
{
173+
foreach (string controller in Directory.GetFiles(controllers))
174+
{
175+
Console.WriteLine($"Loading controller {controller}");
176+
mvcBuilder.AddApplicationPart(Assembly.LoadFrom(controller)).AddControllersAsServices();
177+
}
178+
}
179+
}
180+
catch (Exception ex)
181+
{
182+
Console.Error.WriteLine("Error loading gxcontrollers " + ex.Message);
183+
}
165184
services.Configure<KestrelServerOptions>(options =>
166185
{
167186
options.AllowSynchronousIO = true;
@@ -248,7 +267,6 @@ public void ConfigureServices(IServiceCollection services)
248267
});
249268
}
250269
DefineCorsPolicy(services);
251-
services.AddMvc();
252270
}
253271

254272
private void DefineCorsPolicy(IServiceCollection services)
@@ -333,6 +351,7 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos
333351
{
334352
app.UseResponseCompression();
335353
}
354+
app.UseRouting();
336355
app.UseCookiePolicy();
337356
app.UseSession();
338357
app.UseStaticFiles();
@@ -355,6 +374,10 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos
355374
app.UseHttpsRedirection();
356375
app.UseHsts();
357376
}
377+
app.UseEndpoints(endpoints =>
378+
{
379+
endpoints.MapControllers();
380+
});
358381
if (log.IsDebugEnabled)
359382
{
360383
try

dotnet/test/DotNetCoreWebUnitTest/DotNetCoreWebUnitTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ProjectReference Include="..\..\src\dotnetcore\GxNetCoreStartup\GxNetCoreStartup.csproj" />
4040
<ProjectReference Include="..\..\src\dotnetcore\GxPdfReportsCS\GxPdfReportsCS.csproj" />
4141
<ProjectReference Include="..\..\src\dotnetcore\Providers\Storage\GXAmazonS3\GXAmazonS3.csproj" />
42+
<ProjectReference Include="..\NativeAccessControllerTest\AccessTokenController_Test.csproj" />
4243
</ItemGroup>
4344

4445

dotnet/test/DotNetCoreWebUnitTest/Middleware/MiddlewareTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class MiddlewareTest
1818
public MiddlewareTest()
1919
{
2020
SetEnvironmentVars();
21+
BeforeStartup();
2122
server = new TestServer(WebHost.CreateDefaultBuilder().UseStartup<Startup>().UseEnvironment(DOTNET_ENVIRONMENT));
2223
GXRouting.ContentRootPath = Directory.GetCurrentDirectory();
2324
server.PreserveExecutionContext= true;
@@ -26,6 +27,10 @@ public MiddlewareTest()
2627
protected virtual void SetEnvironmentVars()
2728
{
2829

30+
}
31+
protected virtual void BeforeStartup()
32+
{
33+
2934
}
3035
protected string GetHeader(HttpResponseMessage response, string headerName)
3136
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace GeneXus.Programs
6+
{
7+
public class RequestParameters
8+
{
9+
public string message { get; set; }
10+
}
11+
12+
[ApiController]
13+
[Route("dummy.aspx")]
14+
15+
public class AccessTokenControllerDummy : ControllerBase
16+
{
17+
[HttpPost]
18+
public async Task<ActionResult<string>> Post([FromForm] string message)
19+
{
20+
return await Task.FromResult(message);
21+
}
22+
}
23+
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
5+
<OutputType>Library</OutputType>
6+
</PropertyGroup>
7+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"AccessTokenController_Test": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"environmentVariables": {
7+
"ASPNETCORE_ENVIRONMENT": "Development"
8+
},
9+
"applicationUrl": "https://localhost:63881;http://localhost:63882"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)