-
Notifications
You must be signed in to change notification settings - Fork 192
Using Simple.OData.Client with Xamarin iOS and Android mobile apps
When using Simple.OData.Client with mobile applications build with Xamarin tool you may need to explicitly reference OData adapter library for the OData protocol used by your app, otherwise the application may raise FileNotFoundException with the error message about the V3 or V4 adapter, e.g.:
System.IO.FileNotFoundException:
Could not load file or assembly 'Simple.OData.Client.V3.Adapter' or one of its dependencies.
This behavior is caused by application size optimization performed by the linker. The linker scans applications dependencies and strips out all unused assemblies, or in this case – assemblies it suspects to be unused. To reduce memory footprint, Simple.OData.Client 4.0 loads only assemblies that are required by the selected version of OData protocol, and the root adapter assembly is loaded by reflection making the iOS linker believe that it’s unused.
The behavior is documented by Xamarin, and there are several workarounds. Some of them requires marking code to be preserved with special attributes, but Simple.OData.Client is a packaged as a portable class library, so it can’t have dependencies on platform-specific definitions. An alternative is to include in the main application assembly a single line of code referencing the OData adapter assembly. Here’s how you do it if your application uses OData services over V3 protocol:
Simple.OData.Client.V3Adapter.Reference();
And of the code for V4 protocol is similar:
Simple.OData.Client.V4Adapter.Reference();
Note that this code does nothing, so it can be placed anywhere within the iOS or Android application assembly. Windows Phone and desktop applications will run fine without an explicit adapter reference.