unosquare / embedio

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

Problem with starting web server on iOS. #470

Closed simohr closed 4 years ago

simohr commented 4 years ago

Describe the bug When starting a web server on iOS I get an exception:

System.TypeInitializationException: The type initializer for 'EmbedIO.WebServer' threw an exception. ---> System.InvalidOperationException: Sequence contains no elements
  at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Linq/src/System/Linq/First.cs:16 
  at EmbedIO.WebServer..cctor () [0x0000f] in <995ebf832c7645bfb10152e725a1d15e>:0 

The same code was working for iOS in 3.3.3. We start the web server with "http://*:8080". And this setup runs on android without problems on 3.4.3.

webServer = new EmbedIO.WebServer("http://*:8080");

To Reproduce Steps to reproduce the behavior:

  1. On an iOS device try to start web server.
  2. See error - the above exception

Expected behavior A running web server.

Smartphone (please complete the following information):

rdeago commented 4 years ago

Welcome back @simohr!

The issue is caused by this bug in Xamarin. You can work around it by changing the "Linker behavior" setting in the "iOS Build" page of your project's Properties dialog to either "Don't Link" or "Link SDK assemblies only".

When the Xamarin linker links an assembly, it strips assembly attributes from it. This causes this code, that initializes the WebServer.Signature static property, to fail.

simohr commented 4 years ago

The linker has been my nemesis from the start. I have got our project to run it to work barely with the Link All option with having to write around 100 lines of code to preserve our classes, otherwise I get linker errors. So far I thought it didn't touch nugets but it seems it has its' hands in everything.

simohr commented 4 years ago

For anyone with the same issue, in your Xamarin.iOS project Right Click -> Options -> iOS Build -> Additional mtouch arguments type this line:

--linkskip=EmbedIO

This way you are telling the mtouch tool that builds your iOS app from .net code to exclude the EmbedIO library when linking, thus no longer stripping any assembly attributes.

rdeago commented 4 years ago

Thanks to xamarin/xamarin-macios#6049 there seems to be an alternative to disabling linking of EmbedIO: tell the linker to preserve assembly-level custom attributes.

In your Xamarin.iOS project, right click -> Options -> iOS Build -> Additional mtouch arguments:

--optimize=-custom-attributes-removal

@simohr can you try this and tell us whether it works for you, and the impact on the size of your app?

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.