Including legacy library in .NET Core 2 app - c#

I'm trying to build a .net core C# app that requires a reference to an older library. Everything compiles and builds fine, but when the app attempts to execute any code from this library, it throws an exception:
System.IO.FileLoadException: 'Could not load file or assembly 'mscorlib, Version=5.3.0.620, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
The 5.3.0.620 is the version of the DLL that I'm including, but it shouldn't be the version of mscorlib required. When I include the reference in a .NET 4.6.1 app, I don't get this error.
Is there any way to force the library to be happy with the included version of mscorlib (or any other workaround)?
Edit: This is not a duplicate of this question. That person is trying to reference 4.0.0.0. I already have that reference in place. My problem is that in a .NET Core 2 app, the library I'm loading tries to reference an incorrect (non-existent) version of MSCORLIB for reasons that I don't comprehend. When I load the library in a .NET 4.6.1 app, the library behaves correctly and loads the 4.0.0.0 version.

Related

.NET Framework: How to update System.Runtime?

I have a DLL compiled from a project of type "Class Library" which has a target framework of netcoreapp3.1 (EDIT: now netstandard2.0).
I then reference this DLL in another project, a Windows Forms application, which targets .NET Framework 4.7.2 (EDIT: now 4.8).
When trying to compile the Windows Forms application, I get the following error:
Error CS1705: Assembly 'BC' with identity 'BC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
I would like to update System.Runtime on the WinForms project to use version 4.2.2.0 (which is apparently required by my DLL), however, I found no way to do so - there are no NuGet packages installed and the references section in Visual Studio doesn't seem to include System.Runtime directly:
How do I update System.Runtime and resolve the error?
Try any of the following:
Upgrade your WinForms project to .NET 4.8, because there have been a lot of compatibility fixes in that release. .NET 4.8 still has issues when interfacing with .NET Core libs, but many problems will just be gone.
Or try changing your library to .netstandard2.0. Then it should always be compatbile to Net Framework.
For best compatiblity, try doing both: Setting the library to .netstandard2.0 and change the WinForms app to .NET 4.8.

Using .NET Core DLL in a UWP Application - Runtime Version Mismatch

I have an application that is built on UWP. I need to reference a DLL from a .NET Core Project within my UWP Windows Application Targeted Universal Windows 10.0
The .NET CORE application had a target Core framework 1.0 to match with the UWP 10.0.
I was able to import the DLL, Call it in the application and the application compiled fine. But during execution, when I was using those specific functions, I got a RunTime error as below.
Any help wil be much appreciated.
System.IO.FileLoadException HResult=0x80131040 Message=Could not
load file or assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located
assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040) Source= StackTrace:

What causes error MC1000: Unknown build error, 'Could not find assembly 'System.Runtime.InteropServices.PInvoke...'

The full error information is
Code: MC1000
Description: Unknown build error, 'Could not find assembly 'System.Runtime.InteropServices.PInvoke, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.'
File: C:\Program Files\dotnet\sdk\3.0.100\Sdks\Microsoft.NET.Sdk.WindowsDesktop\targets\Microsoft.WinFX.targets 243
I'll provide a little context in case it's helpful. This is in a .NET Core 3.0 WPF project, that references a .NET Core 3.0 Console App, that references a .NET Standard 2.0 Class Library. All levels except the top level WPF project also reference a slew of Nuget packages, most of which are probably targeting .NET Standard 1.3 or 2.0. They are all targeting a compatible .NET Standard, I don't have any that are throwing the NU1701 warning.
So the question: what is causing this error?

Roslyn Microsoft.CodeAnalysis.Scripting .NET 4.7.1 Problems

I'm using the Roslyn library to execute dynamic c# scripts:
https://github.com/dotnet/roslyn/
For some reason, it only works on .NET framework 4.6. Upgrading to 4.7.1 throws the following errors:
FileNotFoundException: Could not load file or assembly
'System.Runtime, Version=4.0.20.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
system cannot find the file specified.
I have updated and reinstalled the Nuget package:
Install-Package Microsoft.CodeAnalysis.CSharp.Scripting
Any ideas what could be wrong?
UPDATE
I read that creating a .Net Standard project and implementing Roslyn there while referencing the project solves the issues. I did not find this to work at all

System.Data.Linq in netstandard20

I have a netstandard20 project that references a .Net 4.6 Project, all compiles and runs except where I call any functionality in the .Net 4.6 project, I get the following error.
FileNotFoundException: Could not load file or assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
In VS 2017, in the project explorer under Dependancies / SDK / Microsoft.NETCore.App I can see System.Data.dll and System.Data.Common.dll but no reference to System.Data.Linq.dll, I am assuming this is my problem and it has not been pulled in.
How do I get my project to include System.Data.Linq.dll?
If I include C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.Linq.dll I get the following error
BadImageFormatException: Could not load file or assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
I was able to solve my need to use System.Data.Linq in a .NET Standard 2.0 library by using the Mindbox.Data.Linq NuGet package.
After installing it you should be able to use the System.Data.Linq namespace just like you could when writing for .NET Framework!
System.Data.Linq is not part of .NET Standard 2.0 and .NET Core 2.0 and therefore cannot be used on that platform.
Even tough you have been able add a reference to a .NET 4.6 project via the compatibility shims, it fails at runtime because of a feature that is not available on the target runtime

Categories