TypeInitializationException accessing MonoGame GraphicsAdapter.Adapters, could not load type 'SharpDX.Rectangle' - monogame

Converting VB code from XNA 3.1 to MonoGame.
Any attempt to access class GraphicsAdapter throws the exception below. For example, trying to enumerate GraphicsAdapters. On this line:
For Each adapter As GraphicsAdapter In GraphicsAdapter.Adapters
Result is an exception:
System.TypeInitializationException occurred
HResult=-2146233036
Message=The type initializer for 'Microsoft.Xna.Framework.Graphics.GraphicsAdapter' threw an exception.
Source=MonoGame.Framework
TypeName=Microsoft.Xna.Framework.Graphics.GraphicsAdapter
StackTrace:
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter.get_Adapters()
at ...my code...
InnerException:
HResult=-2146233054
Message=Could not load type 'SharpDX.Rectangle' from assembly 'SharpDX, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1'.
Source=MonoGame.Framework
TypeName=SharpDX.Rectangle
StackTrace:
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter.CreateAdapter(Adapter1 device, Output monitor)
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter.PlatformInitializeAdapters(ReadOnlyCollection`1& adapters)
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter..cctor()
Note the inner exception:
Could not load type 'SharpDX.Rectangle' from assembly 'SharpDX, ...Version=4.0.1.0, ...
The project references these assemblies (among others):
MonoGame.Framework version=3.6.0.1625
from NuGet "MonoGame.Framework.WindowsDX", file
packages\MonoGame.Framework.WindowsDX.3.6.0.1625\lib\net40\MonoGame.Framework.dll
SharpDX version=4.0.1.0
SharpDX.Direct2D1 version=4.0.1.0
SharpDX.Direct3D9 version=4.0.1.0
SharpDX.DXGI version=4.0.1.0
SharpDX.Mathematics version=4.0.1.0
So I don't see why it would fail this way.
What could be wrong? Not compatible with this SharpDX version?
NOTE: These are all up-to-date packages from NuGet, in Visual Studio 2015. Targeting .Net 4.5.2.
UPDATE
To prove it isn't a problem with SharpDX, I put this line before that GraphicsAdapter line:
Dim testRect As SharpDX.Rectangle = New Rectangle()
This successfully created testRect. No Exception until attempt to access GraphicsAdapter. Also tried all the other Shared (static) properties of GraphicsAdapter, such as GraphicsAdapter.DefaultDevice. Any one fails with same exception.

I posted the same question to MonoGame forums:
http://community.monogame.net/t/solved-typeinitializationexception-accessing-graphicsadapter-adapters-could-not-load-type-sharpdx-rectangle/9803
Cause of problem:
Current release of MonoGame.Framework for WindowsDX (Windows OS + DirectX drivers), 3.6.0.1625 is built against SharpDX 2.6.3, and is not compatible with more recent SharpDX versions.
(Interim) Solution:
The developer branch of MonoGame does target the current SharpDX. Builds can be obtained from MonoGame's teamcity (free registration required).
Tested MonoGame.Framework build 3.7.0.994; it worked with SharpDX 4.0.1:
http://teamcity.monogame.net/viewLog.html?buildId=56483&tab=artifacts&buildTypeId=MonoGame_PackagingWindows
That build installs most SharpDX dlls to
C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows
However, SharpDX.Mathematics.dll is currently missing,
so I still install SharpDX from NuGet, rather than using those.
Standard location for MonoGame releases:
(look for at least 3.7, which is not there yet, as I write this):
http://www.monogame.net/downloads/
(If instead of NuGet packages for MonoGame and SharpDX, I had downloaded MonoGame 3.6 installer and used its version of SharpDX dlls at C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows, then I would not have had this incompatibility. However, that was not recent enough SharpDX for my purposes. This should be resolved by release of MonoGame 3.7)

Related

Using VS2019, .Net 5.0, Winforms.Wasm 1.0.19137.60 Nuget package - produces unhandled Mono.Cecil.AssemblyResolutionExceptions

I am trying to port 6 class libraries developed under .Net 4.5.2 to .Net 5.00 using VS 2019. I chose .Net 5.00 (and VS 2019) as my final goal is to deliver the solution as a web assembly. I have set the target framework for each of the libaries to .NET 5.0.
One of my class libraries uses Winforms so I have downloaded the Nuget package Winforms.Wasm (1.0.191137.60) by Roozbeh Gh into VS 2019.
On building I initially got the error:
1>C:\Users\howar_000\.nuget\packages\winforms.wasm\1.0.19137.60\build\netstandard2.0\Winforms.Wasm.targets(13,5): error :
Unhandled Exception: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Windows.Forms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
... at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Run(String[] args)
... at Driver.Main(String[] args)
I was able to get past this problem by adding:
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
as per instructions I found on the Winforms.Wasm GIT repository recommended by Roozbeh Gh.
1>C:\Users\howar_000\.nuget\packages\winforms.wasm\1.0.19137.60\build\netstandard2.0\Winforms.Wasm.targets(13,5): error :
Unhandled Exception: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Formats.Asn1, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 1>C:\Users\howar_000\.nuget\packages\winforms.wasm\1.0.19137.60\build\netstandard2.0\Winforms.Wasm.targets(13,5): error : at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Run(String[] args)
i.e. it just moved the problem along to another library distributed along with Winforms.Wasm. I have also tried to explicitly exclude the Nuget version of the System.Formats.Asn.dll but this hasnt worked either.
I realise the problem is that the Nuget Winforms.Wasm package includes netstardard2.0 libraries so that the package download is as a complete standalone distribution. Further that VS 2019 MSBuild is identifying a clash with the dotnet net5.0 distribution.
I have done a lot of investigation trying to resolve this problem - even overwriting
C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.Formats.Asn1.dll
with
C:\Users\howar_000.nuget\packages\system.formats.asn1\5.0.0\lib\netstandard2.0\System.Formats.Asn1.dll
and visa versa but to no avail.
But reading through swathes of literature on the web, I came to the realisation that this is a recurring problem that has plagued developers trying to use VS with .Net and Nuget packages for some time. Surely either Microsoft need to change the implementation of VS MSBuild to accommodate Nuget packages or Nuget package writers need to come up with a VS compliant distribution - rather than leaving developers spending many hours trying to establish work arounds and understand the inner works of VS MSBuild.
I am thinking of moving away from VS 2019 to use the Mono Developer toolkit - I will let you know how I get on.
Any help/suggestions on how best to migrate .Net 4.5.2 assemblies that use Nuget packages to equivalent web assemblies would be much appreciated.

Using ZXing with Hololens

I've been trying to use Zxing on the Hololens but as soon as the app starts on the device, I get a fileloadException when this line is called:
BarcodeReader barcodeReader = new BarcodeReader();
FileLoadException: Could not load file or assembly 'System.Core,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
The located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040) at
ZXing.BarcodeReader..ctor() at Decoder.Start() at
Decoder.$Invoke1(Int64 instance, Int64 args) at
UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64
args, IntPtr method) (Filename: Line: 0).
I have the Unity 2018.1.0b5 beta version with Visual Studio 2017 on Windows 10.
The Building works perfectly so the library is found.
Here's how I import ZXing library :
I've read all the other topics I found on it but the solutions provided do not work. I also tried to use the Zxing.winmd but the dll can't be found then.
Did I miss a step ? A file to download maybe ?
Thanks for your help !
After many configurations tried, I finally managed to use Zxing on Hololens by switching the scripting Backend in Player Settings to Il2Cpp. I was using .net (for the scripting backend) which caused me most of the errors. Il2CPP
If you don't have the IlCPP proposition, you might need to relaunch the Unity installer, making sure that "Windows Store Il2CPP " is checked.installation
I also downgraded my version of unity to 2017 in order to use the Holotoolkit which wasn't working on my 2018 beta version.
This way I don't need any specific settings to import Zxing in the Assets. Also, I didn't use Zxing.winmd but Zxing.unity.
settings Zxing
Tips : don't forget to unable the webcam and to deploy it in Visual Studio with "Release", otherwise it's going to slow down your app.
If Visual Studio doesn't find your Zxing.unity file, right click on your project in the Solution's Explorer (of Visual Studio) : Add->Reference->Find your Zxing.unity.

Matlab .Net Assembly compatible with .Net Core1.1

I am trying to use Matab module compiled as .Net assembly in a .Net Core Project, but I receive the following exception:
Unhandled Exception: System.IO.FileNotFoundException: Could not load
file or ass embly 'MWArray, Version=2.17.1.0, Culture=neutral,
PublicKeyToken=e1d84a0da19db8 6f'. The system cannot find the file
specified. at ConsoleAppCore1._1.Program.Main(String[] args)
MCR_R2017b_win64_installer.exe is installed on the system and I am able to load same Matlab module in .Net4.5 project.
Any clue to make it work with .Net Core1.1?
Simply NO - mathworks supports only "classic"/desktop .net framework on Windows platforms
Please refer to this: mathworks doc

Type or Namespace "Dapper" could not be found: in Hangfire Source code in Visual Studios?

I've imported the Hangfire Source code from Github into my existing VS 2013 solution. When I attempted to build all projects within my solution, I get several errors:
Error 6 The type or namespace name 'Dapper' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Jerry_Dev\Documents\GitHub\Hangfire\src\Hangfire.SqlServer\SqlServerDistributedLock.cs 20 7 Hangfire.SqlServer
In Package Manager, when I do: "Install-Package Dapper", it says
'Dapper 1.38' already installed.
Hangfire.SqlServer already has a reference to 'Dapper 1.38'.
I see Dapper dot net installed in my Manage Nuget Packages Window in my Hangfire.SqlServer project
We don't store the packages folder under source control to reduce the source code size. Hangfire uses Automatic Package Restore in Visual Studio instead of obsolete MSBuild-integrated package restore. The Automatic Restore downloads missing packages automatically on each build, but it works only with NuGet ≥ 2.7, so you have the following options:
Update NuGet to the latest version
Please see the official docs.
Run the build script
Go to the root Hangfire directory and run the build.bat file. It will automatically restore all the missing packages.

NUnit components for version 4.0.30319 of the CLR are not installed

I am trying to implement an automated build process. After the build, the unit tests on nunit-console.exe are run. The following error is displayed:
> c:\nunit_2.5.10\nunit-console.exe c:\builds\Output\bin\TDD.nunit /framework=4.0.30319 /nologo /trace=Off
ProcessModel: Default DomainUsage: Default
Execution Runtime: v4.0.30319
Unhandled Exception:
System.ArgumentException: NUnit components for version 4.0.30319 of the CLR are not installed
Parameter name: targetRuntime
at NUnit.Util.TestAgency.LaunchAgentProcess(RuntimeFramework targetRuntime, Boolean enableDebug)
at NUnit.Util.TestAgency.CreateRemoteAgent(RuntimeFramework framework, Int32 waitTime, Boolean enableDebug)
at NUnit.Util.TestAgency.GetAgent(RuntimeFramework framework, Int32 waitTime, Boolean enableDebug)
at NUnit.Util.ProcessRunner.Load(TestPackage package)
at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options)
at NUnit.ConsoleRunner.Runner.Main(String[] args)
There is no nunit-agent.exe on the build machine. However, on my machine it is not even called, so I suppose it is not necessary.
Why is nunit-agent.exe required in some cases but not always required? What conditions should be satisfied so nunit-agent would not need to launch?
Edit: I have found one resource, which kind of describes how it works, but not quite well: http://www.nunit.org/index.php?p=nunit-agent&r=2.5.10. It says that it is launched when program needs to run under a different framework than the one being used by NUnit (which is the case, since NUnit is compiled for 2.0). However, on my machine the nunit-agent.exe does not run although conditions seem to be the same.
I ran into this same error and it was definitely solved by including nunit-agent.exe in the folder where nunit-console.exe was launched. The complete list of .exes and .dlls necessary to run a test successfully was:
nunit.core.dll
nunit.core.interfaces.dll
nunit.framework.dll
nunit.util.dll
nunit-agent.exe
nunit-console.exe
nunit-console-runner.dll
All files are packaged in the download available from nunit.org. As of this post, 2.6.3 is the current version. Documentation for the console runner can be found here. And the direct download for the zip file is here.
For a test assembly targeting .NET 4.5.1, the following statement will execute tests:
nunit-console.exe your-assembly.dll /framework=v4.5.1
Adding a "startup/supportedRuntime" configuration tag to nunit-console.exe.config solved it for me.
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319" />
</startup>
Try using Fusion to see what assembly might be missing and where the .exe is looking.
http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx