-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automapper fails when using .NET Native #901
Comments
Is there an easy way to reproduce this on my side? |
I've succeeded in creating a very little project in which the error triggers. It is uploaded in this repo. Run the app in Release | x86 and you should get an error like this when stepping through the mapping defined in the Profile:
Not exactly the same error as the stated in my original comment, but sure both are highly related. Note that there are two properties in the mapping's destination class which are key in making the error happen. Environment info:
Hope this points you into the right direction! |
I'm also having this issue related to AutoMapper and the .NET Native tool chain.
I've tried following the instructions mentioned in this article to handle the MissingMetadataException, but I'm not sure if I'm writing the correct directives, or if its something my app can't handle and must be added on the Automapper side - http://blogs.msdn.com/b/dotnet/archive/2014/05/21/net-native-deep-dive-help-i-hit-a-missingmetadataexception.aspx |
Also getting this error. I had to do this:
I don't know why all the public no arg extension methods need MakeGenericMethod called on them but making the calls lazy instead of doing it all up front would be a good first step. It would probably help performance and memory usage as well. |
@JamesNK Are you kidding me?!? |
I'm having a very similar issue:
I tried downloading the latest (~4.1.0-ci1033, I guess?) and debugging directly by adding Automapper.WinRT to my solution and referencing it. I turned on native tool chain debugging, and mixed native/managed debugging. When I did that I couldn't run at all; I got a "dependent dll not found" error. I'm guessing that might be because I was AutoMapper WinRT is a Windows 8.1 project rather than UWP, but I'm not sure. Regardless, that didn't help me debug the situation. I tried putting in directives similar to what JamesNK did above, but for System.Int32 instead of System.Char. This is my first time trying to use a runtime directive file, so I'm not entirely sure where it should go. The code that throws this exception is in a satellite assembly of mine, SightAndSay.DAL.dll. It's the code that sets up the mappings (Mapper.CreateMap). I'm not sure if that means I need to put the runtime directive file in that assembly or if it should go in a section in the main app's *.rd.xml file. So far I've tried the following: SightAndSay.rd.xml (in the main application's folder):
SightAndSayDAL.rd.xml (in the SightAndSay.DAL.dll project's folder):
So far I'm still getting the exception. At the moment that means I'm dead in the water for submitting my app to the store, unfortunately. Here are some links I found helpful: https://msdn.microsoft.com/en-us/library/dn600634%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/en-us/library/dn600165(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/dn600639(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/dn798709(v=vs.110).aspx Please let me know if there's anything I can do to help debug this. |
I just realized that I have a Default.rd.xml file in my Properties folders. I tried moving the block above into both the application's Default.rd.xml file and the assembly's Default.rd.xml file. That didn't help. I also tried putting the following directive in both places instead of the directive:
The exception is still being thrown. |
Here's another article that provides some useful information for library writers: http://blogs.msdn.com/b/dotnet/archive/2014/05/21/net-native-deep-dive-help-i-hit-a-missingmetadataexception.aspx . It makes it sound like changing the AutoMapper rd.xml file may be the "right" solution. |
Here's a bit of information about my latest failed attempt to resolve this. I created a Universal class library in my solution and added all of the automapper files to my project (Xiard.Automapper). Those are the L&G files from github. I uninstalled the AutoMapper package from all of my projects that were using it and referenced Xiard.AutoMapper instead. I modified the default Xiard.AutoMapper.rd.xml file in the Xiard.AutoMapper\Properties folder to include all of the following directives. This should be extreme overkill, but based on the error I would have thought this would resolve the issue:
I then realized I was getting warnings about System.Linq.Enumerable being ambiguous between System.Xml and System.Linq, so I modified my rd.xml files to simply have:
No luck. Same error in release mode. However, I did fix another issue (referencing the wrong VCLibs to use with SQLite UAP), and that got me running in debug mode. Same error. But that now allows me to at least get a stack trace when the exception occurs:
And that is happening on a call to Mapper.CreateMap on a particular class of mine, not on every class. I will see if I can dig into that a little deeper, now that I can debug directly. |
I think I can safely say that the issue has something to do with enum values. I've created these bogus classes in my app:
The exception happens when I call:
I think that's as far as I'm going to go on my side. I don't know why the runtime directives aren't helping, but clearly they're not. So I'm going to rewrite my code to remove the enum helper properties. I'll keep an eye on this post in case you come up with something that allows me to put them back. |
Scratch that. I just figured out that the issue appears to be ignored properties, not enum properties. So this code also causes the problem to appear:
When I switched to strings instead of ints for the test, the exception changed slightly:
|
Also, it appears that the setter is required. This does not trigger the error:
|
Are you using the pre-release version by chance? |
Sorry, yes, I thought I made that clear. Should be around 4.1.0-ci1033. I just pulled the L&G code from the "download as zip" link on the GitHub home page. |
Great, there was a bug around that version and WinRT/UWP. If you get this to work, I'd love to incorporate this back. Also, I know that in Silverlight, private anything doesn't work. Maybe this is similar? |
So far I'm just punting and getting rid of code that makes AutoMapper choke in release. At the moment I don't think I understand UWP native code or AutoMapper well enough to make even an educated guess about how to go about fixing it. I thought a runtime directive would resolve it, and clearly I was wrong about that. |
How are you testing this? I can try some things on my side as well. |
I have a fairly large app I'm working on publishing. Main exe plus 19 assemblies. I'm in debug mode with native tool chain turned on, and mixed (managed/native) debugging turned on. I could try to make a standalone simple app to repro it if necessary. |
There was a comment earlier on a repro, but was curious, you're not deploying to a device, right? Just debugging out of VS? |
That's correct. No deployment, just a straight F5 out of VS. And I assume you saw that I built a UWP class lib version of AutoMapper and added it to my solution rather than using the package on MyGet? |
Yep, though what's the reason why there, just for that XML file? AutoMapper supports UWP in the MyGet version (allegedly) |
Yes, exactly. I wanted to see if I could workaround the issue by modifying AutoMapper's rd.xml file. I thought that perhaps that's where the runtime directives needed to go. Since that didn't work, either I'm doing the runtime directives incorrectly or this particular problem can't actually be worked around that way. I suspect the former, but I have no idea at the moment. I will say that I had a similar issue with missing metadata in my own code, and I was able to successfully add a runtime directive in that case. So they are working for me in general; I just couldn't get them to work for this AutoMapper exception. At this point I have refactored my code to remove all setters from properties with the IgnoreMap attribute. Once I did that, I went back to using the AutoMapper 4.1.0-ci1033 pre-release version. That is working for me in both release and debug now. And, btw, my code passed the Windows App Cert Kit tests as well. So yes, the MyGet version appears to support UWP as far as I can tell. |
Hi @Xiard. I've tested the solution proposed by @JamesNK , and it has worked on the sample project I've submitted to repro the issue. However, it didn't work in my main solution, which is quite similar to yours (a main App project with a number of surrounding assemblies, and the mappings declared on one of them). I've debugged with Automappers' sources referenced directly in the solution, but it didn't gave me any more clues. Who knows, maybe this is actually an error in the .NET Native compiler. I also don't know if Automapper can provide an .rd.xml file (as I thought initially) to solve this, because it cannot anticipate on which type(s) the generic extension methods are going to be closed, as it depends on the classes being mapped. For me is a dead end right now. Even tried a hard workaround, turning the problematic method (BuildPublicNoArgExtensionMethods) into a no-op, but then got another (different) .NET Native error further down in the mappings. I do not know if I'll be able to handle all these errors in the short term, so I've started to consider removing Automapper from the project. |
Fortunately I was able to isolate the particular problem that was coming up for me (the setters on ignored properties) and remove my dependency on that without removing AutoMapper entirely. I'm not doing anything complex with AutoMapper yet; just using it for basic DTO - DomainObject mapping at this point. I suspect the fact that I haven't gotten too deep with it yet helped me in this case. |
BTW, I did have a couple of cases where I could make the setters private rather than removing them entirely. That also worked to avoid the error. |
Closing until we can get a better repro :( |
Now that Automapper 4.1.0 is out, I decided to give it a try, to see if by chance the error doesn't trigger. Unfortunately, the error was still there. But I noticed a warning in the output of the .NET Native compiler, that I didn't saw in my previous tests (and I'm not sure if it was shown earlier):
So, that was the cause for which the runtime directives provided by @JamesNK were ignored in my project. The solution was to wrap the directives into an element, as the warnings suggest:
And that's it, the directives were picked and the mapping declaration didn't throw any exception. Then I had to add the same directives for other types like Int32, Int64, Byte, and even KeyValuePair<string, object> for the same reason. And just for the record, the way you specify that last one in the .rd.xml file is escaping the angle brackets like you do in any XML file. For example:
Sooo, with the code that sets up the mappings not complaining anymore, I've started to test the rest of the code, hitting exceptions like the one reported by @pamtaro:
Which gets fixed with another directive:
Right now I'm hitting other type of exception:
And I'll try to solve it with another directive. It'll be a long path to make this to work, but at least I'm not stuck anymore. @jbogard: can I use the Automapper capabilities to self-test the mappings (AssertConfigurationIsValid) to trigger every declared mapping? If so, maybe that would make the search for the missing directives a little easier. |
Yeah that actually runs every mapping. |
Nice job, @nelsonghezzi . Well done. I had seen that warning, but I didn't try wrapping in an Assembly directive like that. I just included all of System.Linq in an Assembly directive, assuming that would be sufficient. Perhaps that doesn't work the way I think it should? Regardless, kudos. |
Has anyone tried this again with version 4.2.0? I'm getting similar but different errors when trying to compile my project in release mode.... I've attached a completely vanilla project where I've written no code and just added the AutoMapper library into Errors
I tried various permutations of things in the default.rd.xml file But no matter what way I specify those Emit types (here's one version)
It causes this exception
And I don't know where to go with that one..... That exception makes me think something about the thing that's walking the class is doing something wrong and throwing an exception .... That or I'm just doing something silly.. I tried a few different versions of the MyGet automapper things but I always get the same set of errors... I also tried to just add some dummy properties in my project of the missing Types to see if that'd help but it didn't.. |
I get this with 4.2.0:
:\ |
Hi, Got this problem with 4.2.0 as well, but only when building through TFS's build. When I use VS2015, it works. I'm seeing the following warnings and errors:
(sorry, format is a bit messy, first-time commenter on github ;)) |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Running a UWP project compiled with .NET Native tool chain causes automapper fail with the following exception:
As far I saw in the call stack, the exception was produced in the method BuildPublicNoArgExtensionMethods.
Reading some of the documentation regarding .NET Native and reflection (APIs That Rely on Reflection), it states that you must supply a specific runtime directive when using MakeGenericMethod, but it hasn't working on my side.
I wonder if this is an issue that automapper can solve by itself by defining its own .rd.xml file, or if exists another workaround that I can use to avoid this error.
Cheers.
The text was updated successfully, but these errors were encountered: