PlatformNotSupportedException in DbContext through COM - c#

I wrote a simple .net framework library that exposed itself to COM in the appropriate way and I could then create and use the library from VB6. This library did some calculation and read databases rows through Entity Framework.
I have written a .net 7 (Core) version of this library, which still uses EF (not EF Core) and when the library is used through a .net console app, does the same job as the original (i.e. the code is fundamentally working).
I have exposed this to COM by:
a. Adding the following to the .csproj
<EnableComHosting>true</EnableComHosting>
<PlatformTarget>x86</PlatformTarget>
b. Registering the resulting comhost dll
regsvr32 my-library.comhost.dll
c. Creating an instance of the object in VB6
Set obj = CreateObject("new:{C3BBD3C1-FED4-43B0-A9CE-38C356D2BA8E}")
The object is successfully created and when I call one of the exposed functions in the object the code starts executing within the .net library (I can attach and step through using Visual Studio) but when it gets to the point where the DbContext is being created, I get the following:
System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.'
ConfigurationErrorsException: Configuration system failed to initialize
PlatformNotSupportedException: Operation is not supported on this platform.
I understand it is trying to say that in the current environment it is executing, it doesn't know what the target platform is supposed to be? I'm not sure what I should/could do about this to allow this issue to be resolved.
Edit 1:
Full exception details:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.AppConfig.get_DefaultInstance()
at System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext context, String nameOrConnectionString)
at System.Data.Entity.DbContext..ctor()
at LibTest.Domain.Infrastructure.ConfigurationDbContext..ctor() in X:\SOURCE\WEBSERVE\1\LibTestCore\LibTestCore.Domain\Infrastructure\ConfigurationDbContext.cs:line 27
at LibTest.Domain.Configuration.Concrete.DbConfigurationRepository..ctor() in X:\SOURCE\WEBSERVE\1\LibTestCore\LibTestCore.Domain\Concrete\Configuration\DbConfigurationRepository.cs:line 12
at LibTest.Domain.Concrete.Configuration.LegacyConfigurationRepository..ctor()
at LibTest.Domain.Concrete.Legacy.ComInterface..ctor() in X:\SOURCE\WEBSERVE\1\LibTestCore\LibTestCore.Domain\Concrete\Legacy\ComInterface.cs:line 16
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
This exception was originally thrown at this call stack:
System.Configuration.ClientConfigPaths.ClientConfigPaths(string, bool)
System.Configuration.ClientConfigPaths.GetPaths(string, bool)
System.Configuration.ClientConfigurationHost.ConfigPaths.get()
System.Configuration.ClientConfigurationHost.GetStreamName(string)
System.Configuration.ClientConfigurationHost.IsAppConfigHttp.get()
System.Configuration.Internal.DelegatingConfigHost.IsAppConfigHttp.get()
System.Configuration.ClientConfigurationSystem.ClientConfigurationSystem()
System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
Inner Exception 1:
ConfigurationErrorsException: Configuration system failed to initialize
Inner Exception 2:
PlatformNotSupportedException: Operation is not supported on this platform.

Related

Getting ModuleLoadException (C++ Modules) and TypeLoadException from WindowsBase.DLL ONLY when attempting to use the System.Windows.Clipboard class?

I am running a C# WinForms app. The goal of this small section of code is to copy the contents of a .JMP file into a .csv file for data manipulation. Easy enough right? I do not want to do this all in JMP or using a .jsl. The code for parsing and reformatting the data is already there for .csv files and has been working flawlessly.
The app that runs fine unless I have this one specific line that accesses the System.Windows.Clipboard class in my code:
Clipboard.SetDataObject(my data);
When I run the code with the line commented out all is well, but when I run them with the line included the app immediately goes into breakmode with these exceptions:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for '<Module>' threw an exception.
Source=<Cannot evaluate the exception source>
StackTrace: Cannot evaluate the exception stack trace
Inner Exception 1:
ModuleLoadException: The C++ module failed to load during appdomain initialization.
Inner Exception 2:
TypeLoadException: Could not load type 'System.Windows.Media.DisableDpiAwarenessAttribute' from assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
For Inner Exception 1, I am confused because this is a C# app in WinForms with no C++, don't know what the deal is here.
For Inner Exception 2, I am confused because the Assembly WindowsBase is for WPF and not WinForms. I am unable to even add a project reference to this WindowsBase assembly. Again, these errors only happen when I try to do anything with the System.Windows.Clipboard class.
I have downloaded, installed, and ensured the proper location of the following DLLs:
1. PresentationCore.dll
2. System.Windows.Forms.dll
Am I still missing some DLLs? Any help is appreciated.
You should use System.Windows.Forms.Clipboard instead of System.Windows.Clipboard, for the latter one is from WPF. You should remove the reference to PresentationCore.dll, because it's from WPF and not needed in WinForm.

matlab dll throws exception when used in C# .NET

I'm working on a project that requires curve-fitting. To get best results I opted to use MATLAB toolkit. I created a dll of this code and tried to use it in C# .NET framework. The dll works successfully on one system but not on any other system.
I get the following exception:
Unhandled Exception: System.TypeInitializationException: The type initializer fo
r 'curve.cfit' threw an exception. ---> System.TypeInitializationException: The
type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception. --
-> System.TypeInitializationException: The type initializer for 'MathWorks.MATLA
B.NET.Arrays.MWArray' threw an exception. ---> System.DllNotFoundException: Unab
le to load DLL 'mclmcrrt7_17.dll': The specified module could not be found. (Exc
eption from HRESULT: 0x8007007E)
at MathWorks.MATLAB.NET.Arrays.MWArray.mclmcrInitialize2(Int32 primaryMode)
at MathWorks.MATLAB.NET.Arrays.MWArray..cctor()
--- End of inner exception stack trace ---
at MathWorks.MATLAB.NET.Utility.MWMCR..cctor()
--- End of inner exception stack trace ---
at curve.cfit..cctor()
--- End of inner exception stack trace ---
at curve.cfit..ctor()
I wanted to know why this is happening and how I can solve this issue? I need to deploy my project on multiple machines. What should I do?
thank you in advance :)
The error tells you that the program could not find mclmcrrt7_17.dll. This is part of the MATLAB compiler runtime that you need to deploy on each target machine. It looks like you have not deployed the MATLAB compiler runtime.
The runtime redistributable file is available here: http://www.mathworks.co.uk/products/compiler/mcr/index.html

C# - Using external CLR DLL

I'm trying to use an external library with my code. I have added it as a reference from within Visual Studio and things like auto-complete works fine, I can see all the available methods while typing code. It also compiles without any warning or errors.
The DLL is within the same directory as the executable.
However, I get this error:
"Unhandled Exception: System.TypeInitializationException: The type initializer fo
r '<Module>' threw an exception. ---> System.DllNotFoundException: Unable to loa
d DLL 'yeppp': The specified module could not be found. (Exception from HRESULT:
0x8007007E)
at Yeppp.Library.yepLibrary_Init()
at Yeppp.Library.Init()
at .cctor()
--- End of inner exception stack trace ---"
What am I doing wrong?
http://docs.yeppp.info/cs/index.html#GettingStarted
You also need to deploy yeppp.dll as the article says.

Unload And load a reference at runTime

I have a problem with the dll Oracle.DataAccess : on my workstation I have a client installed and on some server I have an other one. At the runTime I catch a
System.TypeInitializationException: The type initializer for
'Oracle.DataAccess.Client.OracleConnection' threw an exception. --->
Oracle.DataAccess.Client.OracleException: The provider is not compatible with the version of Oracle client
at Oracle.DataAccess.Client.OracleInit.Initialize()
at Oracle.DataAccess.Client.OracleConnection..cctor()
--- End of inner exception stack trace ---
at Oracle.DataAccess.Client.OracleConnection..ctor(String connectionString)
In the catch clause, I would like to unload the current dll and load an other location (set in the config file).
Is it possible? And more than that, is it the good way of solving this problem?
One of the way to deal with it - abstract your oracle data access. Create two assemblies, each has reference to specific ODP. Determine which client installed or attempt to loading assemblies one by one until success and work trough your interface. But the key is, your client software must know only the interface, but the implementation of your ODP communication will be abstracted.
And there are patterns for this - proxy, decorator... you choose

Error: Compilation Lock when running unit tests

We have over 1000 unit tests. A while ago 18 of them started to fail when they were run together with the other tests. These tests run a windows workflow.
If they are run alone they pass.
The error appears to be that it cannot find the connection string.
It cannot find the connection string because it does not know which config file to look in.
It does not know which config file to look in because it cannot find the application context
And it cannot find the application context due to a compilation lock.
Anybody have any idea what could be wrong, and how to fix this? The error I am getting is:
Message: Exception message: The
supplied connection string is not
valid, because it contains
insufficient mapping or metadata
information. Parameter name:
connectionString Inner exception :
System.InvalidOperationException:
Unable to determine application
context. The ASP.NET application path
could not be resolved. --->
System.Reflection.TargetInvocationException:
Exception has been thrown by the
target of an invocation. --->
System.Web.HttpException: The type
initializer for
'System.Web.Compilation.CompilationLock'
threw an exception. --->
System.TypeInitializationException:
The type initializer for
'System.Web.Compilation.CompilationLock'
threw an exception. --->
System.NullReferenceException: Object
reference not set to an instance of an
object. at
System.Web.Compilation.CompilationLock..cctor()
--- End of inner exception stack trace --- at System.Web.Compilation.CompilationLock.GetLock(Boolean&
gotLock) at
System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
Thanks
Shiraz
Since you mention that your tests run a WF workflow, I would guess that a previous test run has one or more workflow instances that still run on a thread somewhere, thus locking the compiler because the assemblies are still loaded in a process somewhere.
Can you open Task Manager and investigate whether there's a running process that still uses the workflow somewhere? If so, kill it and see if you can't compile.

Categories

Resources