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

Runtime error when trying to creating WebServer - #552

Closed anantashrit closed 2 years ago

anantashrit commented 2 years ago

Hi,

I am using EmbedIO in my Xamarin application. As of now I have only one target i.e., UWP. No andriod or ios. I use following code to create a WebServer -

var server = new WebServer(o => o
    .WithUrlPrefix(url)
    .WithMode(HttpListenerMode.EmbedIO))
    .WithLocalSessionManager()
    .WithWebApi("/api", m => m
        .WithController<XYZController>())
    .WithStaticFolder("/", HtmlRootPath, true, m => m
        .WithContentCaching(true));

Works fine in debug mode. But in Release mode this piece of code throws the error -

System.Reflection.MissingRuntimeArtifactException: Cannot retrieve a MethodInfo for this delegate because the method it targeted (System.ComponentModel.TypeConverter.ConvertFromInvariantString(String)) was not enabled for metadata using the Dynamic attribute. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=616868

I know this is not exactly EmbedIO error. But I have tried to declare many types, namespaces from the library, EmbedIO in Default.rd.xml but no success. I would really appreciate if someone can configure Default.rd.xml.

Thanks, Anant

rdeago commented 2 years ago

Hello @anantashrit, thanks for using EmbedIO... or, should I say, for trying.

The cause of the exception seems to be the .NET Native compiler stripping metadata (i.e. attributes) from all your controller methods by default.

You can tell the compiler which attributes to retain by adding a runtime directives file to your application.

Having never written a Xamarin app, much less for UWP, I am unable to give you better directions, but I hope I've put you on the right track.

rdeago commented 2 years ago

Closing this issue as this is not a problem with EmbedIO: as a matter of fact, it affects any code that uses reflection.