Xamarin iOS Linker Issue - c#

I have an application that has an android and ios project with shared code. The android application works perfectly and uses the SDK Only linker. I also have a Linker file to enforce linker not removing used functions - Which is a fairly large file and I've also tried adding a bunch of preserve attributes. The same situation in the iOS project, however, doing the same thing doesn't work. It removes something from EF and throws this exception:
"The type initializer for 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' threw an exception. ---> System.InvalidOperationException"
This whole codebase works perfectly fine without the linking. I've added a bunch of mtouch arguments to try and skip it but that ain't working either.
--linkskip=Microsoft.EntityFrameworkCore
--linkskip=Microsoft.EntityFrameworkCore.Design
--linkskip=Microsoft.EntityFrameworkCore.Sqlite
--linkskip=Microsoft.EntityFrameworkCore.Tools
--linkskip=Interactive.Async
--linkskip=Remotion.Linq
--linkskip=System.Data
--linkskip=System.Collections.Immutable
--linkskip=System.Diagnostics.DiagnosticSource
--linkskip=Microsoft.Extensions.Logging
--linkskip=System.Interactive.Async
--linkskip=Microsoft.EntityFrameworkCore.Abstractions
--linkskip=Microsoft.Extensions.DependencyInjection
--linkskip=Microsoft.Extensions.Caching.Memory
--linkskip=System.ComponentModel.Annotations
I'm about to add every DLL in the NuGet repository which will make the Linker useless. Is this a bug for the iOS Linker or what am I missing here? I've read the documentation https://learn.microsoft.com/en-us/xamarin/ios/deploy-test/linker?tabs=windows and searched everywhere on the internet and nothing comes up. Any help is appreciated.
Thanks,

If you are using "Link SDK assemblies only" option on iOS project, then all of those --linkskip arguments won't do anything as only the SDK assemblies are being linked. So the EntityFrameworkCore might be using something that is being linked away that is in the Xamarin.iOS SDK assemblies. System.Linq is part of the SDK and adds extension methods for many database types.
So I think you may be hitting this issue: https://github.com/xamarin/xamarin-macios/issues/3394
If so, try putting:
[assembly: Preserve (typeof (System.Linq.Queryable), AllMembers = true)]
in the Main.cs file of the iOS project outside of the namespace, e.g.:
using UIKit;
[assembly: Preserve (typeof (System.Linq.Queryable), AllMembers = true)]
namespace
{
...
}

Related

Build asp net core with grpc, errors: wellknowntype not found

I build an asp net core API project with a docker. This project contains some proto files and imports some google well-known types. But I got output errors:
- google/protobuf/Timestamp.proto : error : File not found. [/src/mymy/mymy.csproj]
- Protos/notification.proto(4,1): error : Import "google/protobuf/Timestamp.proto" was not found or had errors. [/src/mymy/mymy.csproj]
- Protos/notification.proto(112,5): error : "google.protobuf.Timestamp" is not defined. [/src/mymy/mymy.csproj]
I put my source code in github: https://github.com/pearl2201/docker-aspnetcore-grpc
It is correct... they're not defined; with Grpc.Tools, imports still need to exist. You can use the ProtoRoot option on the <Protobuf ...> line to tell it where the root is, for this purpose. In BUILD-INTEGRATION there is also some mention of $(Protobuf_StandardImportsPath), but that seems to refer only to <ProtoCCommand>.
(as an aside: the protobuf-net tools include copies of all the common imports inside the package; if they aren't resolved from the file system, it looks to see what it has inside the assembly instead, but: this is not a direct change, as the protobuf-net tools output very different C#, intended for a different library implementation).

Portable class library - Reference to type 'MarshalByRefObject' claims it is defined in 'mscorlib', but it could not be found

I have a normal Class Library with a function that converts a Byte Array to an Image.
Now I've deleted that Class Library and created a Portable Class Library with the same name and now the code does not seem to work anymore and gives me an error on the "FromStream"-function:
Reference to type 'MarshalByRefObject' claims it is defined in 'mscorlib', but it could not be found
using System;
using System.Drawing;
using System.IO;
namespace App.Converters
{
public static class Converter
{
public static Image ToImage(this byte[] byteArray)
{
try
{
return Image.FromStream(new MemoryStream(byteArray));
}
catch
{
throw new FormatException("Data is not an image");
}
}
}
}
My project is targeting:
.NET Framework 4.5
ASP.NET Core 1.0
Windows 8
Windows Phone 8.1
Xamarin.Android
Xamarin.iOS
Xamarin.iOS (Classic)
Is this because "something" is not supported in one of the frameworks I'm targeting? Then why does VS let me use and show it in auto-complete?
using System.Drawing;
That's probably a bit more relevant to what you did to get this error message. You used a sledgehammer to get that using directive recognized. We have to guess at it, but one way you might have done that is with Project > Add Reference > Browse button > pick System.Drawing.dll from the c:\windows\microsoft.net subdirectory. Seems to work just fine.
And presumably you used a similar kind of sledgehammer on mscorlib.dll to get MarshalByRefObject recognized. Although that's much harder to do since the IDE can tell that is not valid, mscorlib.dll is already included in the reference set. Maybe you edited the project file by hand, hard to guess.
Don't use sledgehammers.
A PCL project already has a reference to all of the framework assemblies you can possibly use. They are not listed individually in the References node of your project, they are collapsed in the single ".NET" node.
This isn't done to make your life miserable, it ensures that you cannot accidentally use a class that is not available on one of the targets you selected. Saves you from finding out at the worst possible time, after you've spent weeks writing the code, testing it to perfection on your dev machine and now try to run it on a phone. Kaboom, can't work. Up a creek, no paddle, weeks lost.
System.Drawing is only available on desktop machines. And likewise, MBRO is only available on the full version of the CLR, not the .NETCore version. Can't work, MBRO requires remoting, a feature that was cut from .NETCore to make it "core". You'll have to find another way to accomplish what you want to do. No guidance, the question isn't detailed enough.

Error when building uwp app in Release mode

I have this app, that builds and runs successfully in Debug mode, on both my local computer and on my Windows Phone device, when trying with the Release mode, I get these 3 errors :
Error Internal compiler error: One or more errors occurred.
The non-generic type 'PInvoke.NTSTATUS.Code__PInvoke_Windows_Core'
cannot be used with type arguments
...obj\x64\Release\ilc\intermediate\Liberte.Windows.Interop\SafeTypes.g.cs 333
The type or namespace name 'Value' could not be found (are you missing
a using directive or an assembly
reference?) ...obj\x64\Release\ilc\intermediate\Liberte.Windows.Interop\SafeTypes.g.cs 333
The actual code where the two last errors are detected is an auto generated code under a file named SafeTypes.g.cs and the line where the errors is raised is :
namespace PInvoke
{
[global::System.Runtime.InteropServices.McgRedirectedType("PInvoke.NTSTATUS,PInvoke.Windows.Core, Version=0.1.0.0, Culture=neutral, PublicKeyToken=9e300f9f87f04a7a")]
public unsafe partial struct NTSTATUS__PInvoke_Windows_Core
{
public global::PInvoke.NTSTATUS.Code__PInvoke_Windows_Core <Value>k__BackingField;
}
}
I don't have any idea what does this mean, nor what should I do, any explanation and solution to this ?
Update :
Tried to Close VS, then Delete the Obj and the Bin content, rebuild again, but stuill have the same error.
I should mention maybe that I have reference to 2 other projects, one of them is a PCL project that uses PCLCrypto library, and the other one is a Universal App library that uses EntityFramework 7 pre-release with sqlight.
Update 2:
I tried to disable compilation with .Net native just to see what goes on, the release build succeeds, but the App crashes and quits just after startup.
Update 3:
I just discovered that the PCLCrypto library that I use in one of the two project I'm referencing, references two libs that are causing the problem, and they are mentioned in the errors above, I found this out when trying to uninstall the library to see if it is the cause of the problem :
I tried to rollback to a previous stable version, compiled in Debug mode, everything worked perfectly, when I try again Release mode with .Net native, I encounter a new problem : Out of memory.
Concerning Update 3, it seems to be a bug with .NET Native tool chain, I opened an issue on PCLCrypto repo that was moved to corert repo

SimpleITK Windows Universal App - does not build in release, strange behaviour in debugging and TypeLoadException

TL;DR version: Could not load type 'System.Runtime.InteropServices.HandleRef while trying to use SimpleITK on Universal Windows App.
So, right now I have to make simple school project using SimpleITK, and, I got interested in new Universal Windows Applications. I am using Visual Studio 2015 Enterprise.
So, I created a sample project, added references to SimpleITKManaged, copy&pasted SimpleITKNative to obj/Debug catalog and tried to do some tutorials. However, I learned that mixing two new technologies together may be a bad idea.
The ITK libraries I use seem not to work with Universal Windows App. After putting this snippet anywhere in the code, I can not get to debug the method. Code is not hitting breakpoint at the beggining of the method. What is inside is not important, as any type from SimpleITK is causing code to fail.
private void TestMethod()
{//breakpoint here
ImageFileReader reader = new ImageFileReader();
reader.SetFileName(FileName);
var image = reader.Execute();
}
The exception seems to be thrown before code gets to method, probably at level of XAML. I tried first implementing a call as ICommand, then as event handler in the code behind, with no differences. The exception is also not too helpful:
Could not load type 'System.Runtime.InteropServices.HandleRef' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
Neither is StackTrace:
at Pomwjo.Universal.MainPage.ButtonBase_OnClick(Object sender, RoutedEventArgs e)
The same libraries work well in WPF. I tried both x64 and x86 libraries, with the same problem. The build target and library architecture are matching. I'm using SimpleITK C# x86 wrapper 0.9.0, compiled with .NET v2.0.50727, using ITK 4.7.2. I got them pre-built from Sourceforge. Just after that, I found a 0.9.1 version, which I downloaded and tried to use it, to no avail.
Next step was ok, I'm a pro programmer - I'll build it myself!. I will spare describing what it is to build a huge library, let's just say it didn't work either.
Any ideas, how to fix this problem?
EDIT 1
I just realized, that if code is unreachable, it doesn't complain. Well, at least that is working as expected. But, what is even stranger, this snippet fails only if condition is met:
if (new Random().Next(1, 10)%2 == 0)
WrapperMethod(); //inside is snippet from above.
else return;
Also, when building in release, I get this error:
1>C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\x64\ilc\IlcInternals.targets(791,7): error : MCG0024: MCG0024:UnresolvableTypeReference Unresolvable type reference 'System.Runtime.InteropServices.HandleRef' in 'Assembly(Name=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)' found. Check the references in your build system. A reference is either missing or an assembly is missing an expected type.
When this question was first written, it was impossible to use simpleITK in Universal Apps because it lacked some of classes required for it to work - including System.Runtime.InteropServices.HandleRef.
Also, because all apps are run in sandbox, my guess (educated, but still guess) is that it is downright impossible to use native C++ code which is used by SimpleITK. This link describes a way to import native code into app, however this would require quite a bit of tinkering around ITK libraries itself - which was not a point in my project.

Why am I getting the following error when compiling this assembly?

I'm attempting to use the following command line command to compile an assembly of the code from my project:
C:/"Program Files"/Unity/Editor/Data/Mono/bin/gmcs
-target:library -out:C:/Users/Austin/Desktop/PixelExpanse.dll
-recurse:C:/Users/Austin/Desktop/Projects/Repos/trunk/PixelExpanse/SpaceColonyRefactor/Assets/Source/*.cs
-d:RUNTIME -r:C:/"Program Files"/Unity/Editor/Data/Managed/UnityEngine.dll
As you can see, I am, I believe, correctly referencing the UnityEngine.dll.
The code that would be compiled contains references to UnityEngine.UI and UnityEngine.EventSystems. But when I run the above command, I get the following compile error:
error CS0234: The type or namespace name 'EventSystems' does not exist in the namespace 'UnityEngine'. Are you missing an assembly reference?
From what I have been able to find through googling, it SEEMS like an error people were getting when using a pre-4.6 assembly, because thats when EventSystems and UI were both introduced. But I don't know how I could be missing that in the dll I'm referencing as Unity 5 is the only version that has ever touched this computer.
As a side note, I have posted this question to Unity Answers and have yet to receive a response. I expect it's because assembly compilation is beyond the scope of what most users there choose to undertake. Hence my asking it here.
The namespace UnityEngine.EventSystems actually appears in UnityEngine.UI.dll and not UnityEngine.dll so it seems you need to reference the former too when compiling manually from the command-line. Unity projects have this by default (see below).
This is verified by opening up the assembly in your reflector tool of choice, here I am using JetBrains dotPeek:
This is how my test project appears with default Unity references. Note that by default a reference to UnityEngine.UI already appears in the Unity-created project:
When I built my Windnows desktop app via Unity, the above dlls appeared in:
<drive>:<projectOutFolder>\<projectName>_Data\Managed
You can try:
right click on "project panel", and after "Reimport All".
It's can be happen due to switch between platforms, e.g. IOS, or Desktop.. thus, folrders are deleted by became unecessary..
Solve for me: Unity 5.2, Win 7, 32bits;
Good luck!
I checked the UnityEngine.dll and the Eventsystems/UI namespace is not included. Despite, there is a dll in Unity5\Editor\Data\UnityExtensions\Unity\GUISystem\UnityEngine.UI.dll which includes these namespaces.
For mac, it's /Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll.

Categories