MissingMethodException: Method not found: 'IntPtr System.Runtime.CompilerServices.Unsafe.ByteOffset(!!0 ByRef, !!0 ByRef)'

MissingMethodException: Method not found: 'IntPtr System.Runtime.CompilerServices.Unsafe.ByteOffset(!!0 ByRef, !!0 ByRef)'

jens.slofstra!
Enthusiast Enthusiast
2,763 Views
8 Replies
Message 1 of 9

MissingMethodException: Method not found: 'IntPtr System.Runtime.CompilerServices.Unsafe.ByteOffset(!!0 ByRef, !!0 ByRef)'

jens.slofstra!
Enthusiast
Enthusiast

Hi everyone,

Recently I installed Visual Studio on my laptop. 

But when I try to run my add-in I get a MissingMethodException.

On my work PC everything works fine but on my laptop it does not.

 

This is the error I get:

System.TypeInitializationException: 'The type initializer for 'PerTypeValues`1' threw an exception.'

Inner Exception

MissingMethodException: Method not found: 'IntPtr System.Runtime.CompilerServices.Unsafe.ByteOffset(!!0 ByRef, !!0 ByRef)'.

 

These are the settings and versions I currently have.

They are the same as on my work PC.

 

jensslofstra_0-1637584589254.pngjensslofstra_1-1637584638238.png

 

I haven't found a workaround or fix on this problem.

Only this https://forums.autodesk.com/t5/revit-api-forum/revit-api-error-on-trying-to-invoke-microsoft-ml-from...

 

But this does not explain how to fix the issue.

0 Likes
Accepted solutions (2)
2,764 Views
8 Replies
Replies (8)
Message 2 of 9

jeremy_tammik
Alumni
Alumni

With normal Revit add-in code, you should never encounter any such messages. I have no idea what might be causing this.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 9

RPTHOMAS108
Mentor
Mentor

From what I can see that class isn't part of .Net Framework, see bottom of page in link below.

 

Unsafe.ByteOffset<T>(T, T) Method (System.Runtime.CompilerServices) | Microsoft Docs

 

If so I don't know why that would even feature as you are targeting Framework.

 

A bit odd that you see it on one machine and not the other. You've not really clarified if the project solution is the same on both machines or if it is some kind of remote machine scenario. Is the laptop older than the other machine i.e. does it have the newer .NET Core/.NET etc. Also is the version of visual studio the same i.e. latest?

 

0 Likes
Message 4 of 9

jens.slofstra!
Enthusiast
Enthusiast
Accepted solution

Hi everyone, (@jeremy_tammik @RPTHOMAS108)

What I have found is that two of my add ins use the exact same references / Nuget packages.

Only the versions were different so Revit didn't know which version to pick. (because it was not specified)

Problem now is that the .dll files are somewhere on my pc and I can't find them.

Solution is to clean up my pc and that worked.

 

Hope this was useful for someone other than me 🙂

0 Likes
Message 5 of 9

Anonymous
Not applicable

What do you mean with "clean"? I have the same problem and when I search for the file it appears in a lot of places.

0 Likes
Message 6 of 9

jens.slofstra!
Enthusiast
Enthusiast

I uninstalled programs I didn't need. Removed Temp an Cache folders. Ran disk fragmentation. And basically set my whole pc to factory settings. It is not a pretty solution and not something I really recommend but after one week of struggeling I figured it was the only option.

0 Likes
Message 7 of 9

bvgarbar
Contributor
Contributor
Hi!

I Have the same problem.
I cleaned everything and everywhere, but it didn't help.
Can you explain please, how to resolve it?

Best Regards,
Bogdan.
0 Likes
Message 8 of 9

jens.slofstra!
Enthusiast
Enthusiast
Accepted solution

Hi @bvgarbar,

I think the problem we get are different from each other.

What you can do is check which NuGet package has a dependency on the System.Runtime.CompilerServices.Unsafe.dll.

When you can find that package, you uninstall that one and try to find a replacing package.

 

I have encountered similar problems with creating .json files with System.Text.Json.

Instead I only use Newtonsoft.Json now and the problem has not popped up again.

 

Hope this helps you solve your problem.

0 Likes
Message 9 of 9

bvgarbar
Contributor
Contributor

Hi!

I Have the same problem with System.Runtime.CompilerServices.Unsafe.dll. in System.Text.Json.
You're right. Newtonsoft.Json might help. But I am trying to minimize dependencies on different packages and wanted to get rid of Newtonsoft.Json.

I also noticed that the problem does not occur when I debug the application via Addin manager. Everything works well.
I also noticed that errors are different if the plugin is run on different versions. For example, in Revit version 17, the error says that System.Runtime.CompilerServices.Unsafe.dll. v 4.0.4.1 cannot be found. In Revit version 19, the error says that the method cannot be found.

I found this solution and it helped me. 

(https://ru.stackoverflow.com/questions/1149772/%D0%9D%D0%B5-%D1%83%D0%B4%D0%B0%D0%BB%D0%BE%D1%81%D1%...)

 

Suggestion

1) Please try to register System.Runtime.CompilerServices.Unsafe version 4.0.4.1 into GAC so that the system can it.

  • Run Developer Command Prompt for VS2019 as Administrator

  • type:

    cd xxxxx (the path of the the System.Runtime.CompilerServices.Unsafe 4.0.4.1)
    
    gacutil /i System.Runtime.CompilerServices.Unsafe.dll

 

But it's not very good solution.

I download nuget System.Runtime.CompilerServices.Unsafe.dll. v 4.5.3 and put it next to the app.

Then I use Assembly.LoadFrom("namePath") and everything works.

0 Likes