System.PlatformNotSupportedException exception when calling Assembly.ReflectionOnlyLoadFrom - c#

I am using .NET Core 2.1 to load an assembly calling Assembly.ReflectionOnlyLoadFrom(...) but am getting a System.PlatformNotSupportedException exception. Microsoft documentation seems to indicate that it should work. Has this been implemented? If not, are there any alternatives to get exported types from a .NET 4.7.2 assembly using .NET Core?
Assembly.ReflectionOnlyLoadFrom(fileName)
'Assembly.ReflectionOnlyLoadFrom(fileName)' threw an exception of type 'System.PlatformNotSupportedException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233031
HelpLink: null
InnerException: null
Message: "ReflectionOnly loading is not supported on this platform."
Source: "System.Private.CoreLib"
StackTrace: " at System.Reflection.Assembly.ReflectionOnlyLoadFrom(String assemblyFile)"
TargetSite: {System.Reflection.Assembly ReflectionOnlyLoadFrom(System.String)}

As written above, reflection is not supported yet on .net core.
You can use the experimental use TypeLoader from the LAB's repo.
If you want to use the experimental NuGets, you need to follow instructions on:
https://github.com/dotnet/corefxlab (basically adding the NuGet repo - https://dotnet.myget.org/F/dotnet-corefxlab/) and fetch the System.Reflection.TypeLoader.
Don't expect much support though :)

Related

Unity Container exception – You cannot have more than one dynamic module in each dynamic assembly in this version of the runtime

I've got a project that I need to Migrate from .NET Framework to .NET Core. After migrating it I've noticed an exception –
Unity.Exceptions.ResolutionFailedException HResult=0x80131500
Message=Resolution of the dependency failed, type =
'LanDocs.ExchangeSystem.Server.Interfaces.IExchangeDataHandler', name
= '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - You cannot have more than one dynamic
module in each dynamic assembly in this version of the runtime.
What might be the issue and how to fix it?
I've figured it out myself and decided to share the solution here since I haven't found anything helpful while struggling with it.
The issue was with Policy Injection – once I've removed the entire section everything worked well.
But the problem was the version of version of nuget package. I've had Unity.Interception with version 5.5.3 and Unity with version 5.8.6. Once I've changed both versions to 5.11.1 everything worked well on .NET Core.

Method not found: 'System.Linq.IQueryable`1<!!0> NHibernate.Linq.LinqExtensionMethods.Query(NHibernate.IStatelessSession)'

I have a problem when upgrading NHibernate from 4.1.2 to 5.2.3 and Target Framework from 4.5 to 4.6.2 (to compatible with NH 5.x). The problem occurs when I execute the query using LINQ (it worked well before):
service.Query.Where(p => p.Approved).FirstOrDefault();
This is detail message exception:
Method not found: 'System.Linq.IQueryable`1 NHibernate.Linq.LinqExtensionMethods.Query(NHibernate.IStatelessSession)'.
I notice when I call service.Query, the system will throw an exception:
'service.Query' threw an exception of type 'System.MissingMethodException'
What did I miss?
Your app is still trying to call old extension Query method which was removed in NHibernate 5. Query is IStatelessSession/ISession interface method since NHibernate 5.
Exception you see means that your app is not properly recompiled with new NHibernate dlls. Make sure that all your projects reference new dlls (or nuget package) and you have no compilation errors (possible case - you run last successful build before upgrade but with newer dlls).

Unable to call "load-string" from C# project with Clojure >= 1.8

I have a C# project that loads and calls a custom Clojure function via load-string. This works fine with Clojure 1.7 (installed via NuGet). However, when if I upgrade to the latest pre-release (1.9.0-alpha13), the following exception is thrown upon the invocation of load-string:
"System.ArgumentException: No matching clause: :clojure.spec/pcat at clojure/spec."
There are no issues running the same exact code in a 1.9 REPL.
Using the latest stable (1.8), I instead get the following:
"System.TypeInitializationException: The type initializer for 'clojure.clr.api.Clojure' threw an exception. ---> System.TypeInitializationException: The type initializer for 'clojure.lang.RT' threw an exception. ---> System.IO.FileNotFoundException: Could not locate clojure.core.server.cljc.dll or clojure/core/server.cljc on load path."
My best guess with the issue with 1.8 is that there seems to be an issue with ILMerge that's preventing all the DLLs from being merged.
Any thoughts on how to fix these issues? I realize that 1.9 is still in alpha, so I'm not too concerned about that, but it's a bit annoying to be unable to use 1.8.

Custom Tool Error: after switching project from .net 4.5 to .Net 4.6.1

I have upgraded my project from .Net 4.5 to .Net 4.6.1 and am now getting the following errors when trying to compile:
The "GenerateBindingRedirects" task failed unexpectedly.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Build.Tasks.GenerateBindingRedirects.UpdateExistingBindingRedirects(XElement runtimeNode, IDictionary`2 redirects)
at Microsoft.Build.Tasks.GenerateBindingRedirects.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()
Custom tool error: Failed to generate file: An error occurred creating the configuration section handler for system.serviceModel/bindings: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) (C:\ALM[BranchName]\Common[ProjectName]\app.config line 48) Portal.Interface C:\ALM[BranchName]\Common[ProjectName]\Service References[ServiceName]\Reference.svcmap
Based on other searches, I've tried:
Removing the ServiceModel section in app config and updating service references.
This doesn't work though, because I do not have access to the services from my machine.
I've also confirmed that the service references are configured with "Reuse types in referenced assemblies" is unchecked.
One search led me to removing one of the entries in Reference.svcmap, but was non-specific on what entry should be removed.
I'm definitely out of my element with service references so any help would be appreciated.
The offending line:
private void UpdateExistingBindingRedirects(XElement runtimeNode, IDictionary<AssemblyName, string> redirects)
{
...
var publicKeyToken = assemblyIdentity.Attribute("publicKeyToken");
var publicKeyTokenValue = publicKeyToken.Value;
...
looks like you cannot redirect assemblies that are not strong-named. which means I should just be able to remove the entries. This does make me wonder how this worked before in .Net 4.5
Specifying publicKeyToken="null" fixes it for me.

Using .NET dependencies with Iron.io IronWorker

I'm brand new to Iron.io's IronWorker, and I'm attempting to interface with it via C#.NET. I have managed to get the sample on the site to work, which utilizes the System.Web.Script.Serialization library. However, I want to use Newtonsoft.Json library for serialization/deserialization. My .worker file (located in the \bin directory of a Visual Studio project) looks like this:
runtime 'mono'
exec 'Debug\hello.exe'
file 'Debug\Newtonsoft.Json.dll'
I'm fairly certain that Newtonsoft.Json is being loaded, because adding the 3rd line of file: 'Debug\Netwonsoft.Json.dll' fixed the error that said that Newtonsoft could not be found.
The error I'm receiving when attempting to run a task is the following (I removed any seemingly sensitive information):
{"code_id":"[CODE_ID]","code_name":"hello","created_at":"2014-11-
16T13:48:01Z","duration":2452,"end_time":"2014-11-16T13:48:06Z","id":"[ID]","msg":"Unhandled
Exception: System.TypeLoadException: Could not load type
'System.Runtime.CompilerServices.ExtensionAttribute' from assembly
'Newtonsoft.Json'.\n at HelloWorld.Main (System.String[] args) [0x00000] in <filename
unknown>:0 \n[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could
not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly
'Newtonsoft.Json'.\n","project_id":"[PROJECTID]","start_time":
"2014-11-16T13:48:04Z","timeout":3600,"updated_at":"2014-11-16T13:48:07Z","status"
:"error"}
In the course of tracking down the error: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'Newtonsoft.Json', I ran across a SO thread that suggested there might be a .NET framework mismatch between v4.0 and v4.5. I'm using v4.5, and I tried changing it to v4.0 with no luck (so this may not be the issue).
Can someone please point me in the right direction as to how to properly utilize a dependency, such as Newtonsoft.Json (so at least the sample on Iron.io will work)?
P.S. I've been toying with IronSharp as an API to assist with interfacing with Iron.io products (once I get past this dependency issue). But, I'm open to suggestions if there's anything better I should be checking out.
SOLUTION
#Featalion's answer below really helped, becuase he pointed me to the stack command, which I placed in like so:
runtime 'mono'
stack 'mono-3.6'
exec 'Debug\hello.exe'
file 'Debug\Netwonsoft.Json.dll'
I believe it resolved the error at hand, because the higher mono framework seems to handle the latest version of Newtwonsoft.Json.dll. I'll post here if I find otherwise.
Make note that as of 11/17/2014, it seems the highest stack version supported by IronWorker is 3.6 (even though 3.10 is available, details found here). Check the highest stable release from Iron.io at this link.
This is, possibly, related to mono version on the IronWorker service. I found related question on StackOverflow. Try to change the stack in your .worker file to mono-3.6 or mono-3.0. See all possible runtime environments on Iron.io's dev site. mono runtime is default, and it is alias for mono-2.10.

Categories