Understanding Unity Api Compatibilty

I have code which works in the editor… but fails at runtime with an IL2CPP/.NET 4.x build. I’m getting NotSupportedException for code that is implemented in Mono: mono/mcs/class/referencesource/System.ServiceModel/System/ServiceModel/BasicHttpBinding.cs at main · mono/mono · GitHub and otherwise works in the editor.

System.NotSupportedException: System.ServiceModel.BasicHttpBinding::.ctor
  at System.ServiceModel.BasicHttpBinding..ctor (System.ServiceModel.BasicHttpSecurityMode securityMode) [0x00000] in <00000000000000000000000000000000>:0

My question is – how do I know ahead of time what is supported by Unity’s version of Mono/.NET and what isn’t? Or is this a bug?

Thanks,
Shaun

Further adding to the strangeness, it would seem that this works in the Unity editor because its version of Mono does indeed have the class implementation. It’s only the IL2CPP build output that does not have the class implemented. How do I understand when this will happen? i.e. how can I know ahead of time what IL2CPP will or won’t support – assuming that this class implementation should be missing at build output runtime?

Did you switch to 4.x in the player options?

I dont know if its defualt by now, im on 2017.4

Yep.

I’m using Unity 2018.3.12f1.

The short answer is that we don’t have a good way to determine what is supported or not supported in the class library code on various platforms. There are two different axes on which would could have support or not:

  • JIT vs. AOT (i.e. Mono vs. IL2CPP) - there are some parts of the class libraries that don’t work well with AOT runtimes.
  • Platforms - for example, some platforms don’t support IPv6, or threads, or networking, etc.

At the moment we don’t have documentation about what is supported on each platform, which is not great. In this case, please drop us a bug report, we might be able to support this with IL2CPP. At least, we can have a look at it.

Also, note that the Api Compatibility Level option sets the C# compile-time profile. That option does not impact the actual implementation of the class libraries used, as that implementation may vary based on either (1) or (2) I mentioned above.

If you want more errors about unsupported APIs at compile time, I would use the .NET Standard 2.0 Api Compatibility Level. That is a smaller profile than .NET 4.x Equivalent, and it will have a better chance of being supported. There is a good bit of .NET 4.x Equivalent that we cannot support on all platforms, so you will get more errors at runtime with it.

This really comes down to when you want to see the errors.

  • Compile time - use .NET Standard 2.0
  • Run time - use .NET 4.x Equivalent

@JoshPeterson Thanks, Josh. I filed a report and I’m hopeful they might consider adding this class to the player runtime. It’s likely my last blocker before migrating from the .NET scripting backend to IL2CPP. To be honest, I’m surprised it is not present. Without this class no web services will work in IL2CPP. And then if that’s the case, why support System.ServiceModel.dll at all…

This is exactly my problem and my question as well. System.ServiceModel.dll is not supported with il2cpp and how would I know this before wasting weeks trying to get il2cpp to work?

I am paying for professional license, and I am not sure what it gives me short of removing the watermark, so at least having some documentation of what is supported would be really useful, just out of courtesy.