unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.45k stars 175 forks source link

EmbedIO.Samples console app does not work when PublishAot is set to true #584

Closed anantashrit closed 1 year ago

anantashrit commented 1 year ago

Requirement : Deploy a natively compiled tiny server on a windows PC. Natively compiled because it is better in terms of security. MSIL can be reverse engineered easily even after sufficient obfuscation.

Steps tried: Change the target framework to net7.0 Add true to the EmbedIO.Samples.csproj file Ensure that it works in debug mode. Console launches, starts the server and starts the browser, shows the embedio GUI from html folder, api/people works etc. Now create a publish profile targeting win-x64, Release x64 mode. Publish to a folder. Ensure that publish is successful. Run the EmbedIO.Samples.exe generated.

Expected : Console launches, starts the server and starts the browser, shows the embedio GUI from html folder, api/people works etc.

Observed: Console launches, starts the browser but the browser shows "unable to connect"

IDE used : Visual Studio 2022 professional

Is it not possible to natively compile for Windows, a console application just like the EmbedIO.Samples that acts as a server using EmbedIO package?

anantashrit commented 1 year ago

Update......................

On executing the line - ".WithWebApi("/api", m => m.WithController())" Getting below exception when running the natively built, EmbedIO.Samples.exe

Message: Controller type must have a public parameterless constructor. (Parameter 'controllerType')

at EmbedIO.WebApi.WebApiModuleBase.RegisterControllerType(Type) + 0xf3 at EmbedIO.Samples.Program.<>c.b__5_2(WebApiModule m) + 0x22 at EmbedIO.WebModuleContainerExtensions.WithModule[TContainer, TWebModule](TContainer, String, TWebModule, Action`1) + 0x24 at EmbedIO.Samples.Program.CreateWebServer(String) + 0x1a4

Ofcourse I added a parameterless constructor to PeopleController and tried again. Same error continues.

Then I checked the EmbedIO implementation.

WebApiModuleExtensions class has -> public static WebApiModule WithController(this WebApiModule @this) where TController : WebApiController, new() But WebApiController has no public parameterless constructor.

anantashrit commented 1 year ago

No issue with embedIO. Had to add runtime directives. Works..!