Loading a 3.5 compiled C++/CLI application in .NET 4.0 - .net-4.0

I'm trying to load a mixed managed application compiled and targeted for Framework 3.5 in the 4.0 CLR.
We have a .config file next to the .exe where I've added this node:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0.21006" />
</startup>
Unfortunately, the app crashes out on startup with a nasty callstack. Can someone in the know confirm that a mixed managed app (.exe is C++/CLI) will not load in 4.0 if it was compiled for 3.5?
I'm watching a Channel9 video about side-by-side CLR hosting, and one of the devs seems to imply that this is the case:
http://channel9.msdn.com/shows/Going+Deep/CLR-4-Side-by-Side-In-Process-What-How-Why/
Thanks!

You need to set useLegacyV2RuntimeActivationPolicy if you want to load a CLR 2 (.NET 3.5) mixed mode assembly in a CLR 4 process:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Without this, you'll get a nasty exception.

You should be able to run 3.5 and 4.0 in the same process. However forcing the app to use 4.0 instead of 3.5 doesn't look possible.
What are you trying to accomplish?

Related

Missing Nuget packages error in ,NET core 3.1 app

I am using VS code and not Visual Studio to run my .NET core 3.1 web app.
I have a .NET core 3.1 app. As I am using an Client VDI machine ( It is using Win 7 SP1 operating system) which is blocking the Nuget.Org URL currently.
so as a work around, I have copied over all the Nuget package into an folder in my VDI and updated the path in the Nuget.Config.
But still I am getting below
Missing Package" error. My App do not DIRECTLY depends on these packages.
following is my Nuget.Config file,
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!-- add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" / -->
<add key="nuget.org" value="C:\NugetPackageDownloadLocation" / >
</packageSources>
</configuration>
any help will be appriciated.
Have you tried adding a package source via the CLI? Given how .NET Core handles transitive dependencies getting all the packages required for a relatively complex solution might be tricky.
nuget sources Add -Name "MyServer" -Source \\myserver\packages

The key 'LocalizedPerfCounter' does not exist in the appSettings configuration section

I have a desktop application coded in VB.Net with .net 2.0 and then upgraded VS many times and latest framework is 3.5 with VS 2015.
On application start I've noticed an exception in Diagnostic Tool which is caught silently
The key 'LocalizedPerfCounter' does not exist in the appSettings configuration section.
Any ideas to fix this problem because this is an application I distribute.
Found an answer on this link and seems it is related to app setting which was missing in app.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="LocalizedPerfCounter" value="true" />
</appSettings>
</configuration>
I needed to add appSettings section after configSections section.

Import older assembly into .NET 4.0 project [duplicate]

I have a project in which I'd like to use some of the .NET 4.0 features but a core requirement is that I can use the System.Data.SQLite framework which is compiled against 2.X. I see mention of this being possible such as the accepted answer here but I don't see how to actually achieve this.
When I just try and run my 4.0 project while referencing the 2.X assembly I get:
Mixed mode assembly is built against version 'v2.0.50727' of the runtime
and cannot be loaded in the 4.0 runtime without additional
configuration information.
What "additional configuration" is necessary?
In order to use a CLR 2.0 mixed mode assembly, you need to modify your App.Config file to include:
<?xml version="1.0"?><configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup></configuration>
The key is the useLegacyV2RuntimeActivationPolicy flag. This causes the CLR to use the latest version (4.0) to load your mixed mode assembly. Without this, it will not work.
Note that this only matters for mixed mode (C++/CLI) assemblies. You can load all managed CLR 2 assemblies without specifying this in app.config.
This forum post on the .NET Framework Developer Center. It might provide some insight.
(Add to the app's config file.)
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
Depending on what version of the framework you're targeting, you may want to look here to get the correct string:
http://msdn.microsoft.com/en-us/library/ee517334.aspx
I wasted hours trying to figure out why my release targeting .Net 4.0 client required the full version.
I used this in the end:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"
sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
Once you set the app.config file, visual studio will generate a copy in the bin folder named App.exe.config. Copy this to the application directory during deployment. Sounds obvious but surprisingly a lot of people miss this step. WinForms developers are not used to config files :).
Using 2.0 and 4.0 assemblies together isn't quite straight forward.
The ORDER of the supported framework declarations in app.config actually have an effect on the exception of mixed mode being thrown. If you flip the declaration order you will get mixed mode error. This is the purpose of this answer.
So if you get the error in a Windows Forms app, , try this, mostly Windows Forms apps.
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
<supportedRuntime version="v2.0.50727"></supportedRuntime>
</startup>
Or if the project is not Windows Form. In a Web project add this to web.config file.
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0.50727"></supportedRuntime>
</startup>
Was able to solve the issue by adding "startup" element with "useLegacyV2RuntimeActivationPolicy" attribute set.
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
But had to place it as the first child element of configuration tag in App.config for it to take effect.
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
......
....
The above didnt work for me (I am working on a web app) - but this did...
Edit the sgen.exe.config file in the folder (I had to create one first);
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools
(There is also one in v7.0 folder, but I didnt need to change that one, I am using VS2012)
The conents of the XML should look like this (same in previous answers)
<?xml version ="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<requiredRuntime safemode="true" imageVersion="v4.0.30319" version="v4.0.30319"/>
</startup>
</configuration>
If your are working in a web service and the v2.0 assembly is a dependency that has been loaded by WcfSvcHost.exe then you must include
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
in ..\Microsoft Visual Studio 10.0\Common7\IDE\ WcfSvcHost.exe.config file
This way, Visual Studio will be able to send the right information through the loader at runtime.
I ran into this issue when we changed to Visual Studio 2015. None of the above answers worked for us. In the end we got it working by adding the following config file to ALL sgen.exe executables on the machine
<?xml version ="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>
Particularly in this location, even when we were targeting .NET 4.0:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools
I used this config:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v2.0"/>
<supportedRuntime version="v4.0"/>
</startup>
Worked for me
I had this problem when upgrading to Visual Studio 2015 and none of the solutions posted here made any difference, although the config is right the location for the change is not. I fixed this issue by adding this configuration:
<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>
To: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.ProcessHost.Managed.exe.config
Then restarted Visual Studio.
I found a way around this after 3-4 hours of googling. I have added the following
<startup selegacyv2runtimeactivationpolicy="true">
<supportedruntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
If this doesn't solve your problem then--> In the Project References Right Click on DLL where you getting error --> Select Properties--> Check the Run-time Version --> If it is v2.0.50727 then we know the problem.
The Problem is :- you are having 2.0 Version of respective DLL.
Solution is:- You can delete the respective DLL from the Project references and then download the latest version of DLL's from the corresponding website and add the reference of the latest version DLL reference then it will work.
I was experiencing this same error, and spent forever adding the suggested startup statements to various config files in my solution, attempting to isolate the framework mismatch. Nothing worked. I also added startup information to my XML schemas. That didn't help either. Looking at the actual file that was causing the problem (which would only say it was "moved or deleted") revealed it was actually the License Compiler (LC).
Deleting the offending licenses.licx file seems to have fixed the problem.
I was facing a similar issue while migrating some code from VS 2008 to VS 2010
Making changes to the App.config file resolved the issue for me.
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"
sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
</configuration>
Add following at this location C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64
FileName: sgen.exe.config(If you dont find this file, create and add one)
<?xml version ="1.0"?>
<configuration>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>
Doing this resolved the issue
I Use
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
It's works but just before de </configuration> tag otherwise the startup tag doesn't work properly
Also i had this issue with the class library, If any one have the issue with the class library added to your main application. Just add
<startup useLegacyV2RuntimeActivationPolicy="true">
to you main application which would then be picked by the class library.

MSI .NET Framework Prerequisite

I have an MSI file with .NET Framework 2.0 Prerequisite. In windows 8 .NEt framework 2.0 is not installed by default and when i run the .msi, it says you need to install .NET framework 2.0.
But Windows 8 already comes with .NET framework 4.0 or 4.5 which supports older versions and indeed would be able to run the msi file if i could say this msi file can also run on .NET framework 4.0. How can i do that?
More precisely, how can i configure an msi file to have a prerequisite such that min .net framework version 2.0 but can run on newer versions?
I can do this for assemblies adding the configuration below.
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v3.0" />
<supportedRuntime version="v3.5" />
<supportedRuntime version="v4.0" />
</startup>
</configuration>
Thanks
The SO question How can I detect .net 3.5 in WiX? appears to answer this. It also points to another post that discusses this when not using WiX
If you install the .Net framework 3.5, that will provide 2.0 as well. Only 4.5 is installed by default. Go to the Windows features and turn on .Net framework 3.5.

System.Data.SQLite and Fluent NHibernate - IDBCommand could not be found

I am trying out Fluent NHibernate 2.12 with System.Data.SQLite (ver 1.0.74.0 with .NET4 x64).
When trying to create my session factory I get the following error (innermost exception):
The IDbCommand and IDbConnection implementation in the assembly
System.Data.SQLite could not be found. Ensure that the assembly
System.Data.SQLite is located in the application directory or in the
Global Assembly Cache. If the assembly is in the GAC, use
element in the application configuration file to
specify the full name of the assembly.
Tried what was already suggested
my app.config looks like this:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" >
<probing privatePath="Lib\NHibernate;Lib\Castle;Lib\SQLite"></probing>
<qualifyAssembly
partialName="System.Data.SQLite"
fullName="System.Data.SQLite, Version=1.0.74.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</assemblyBinding>
</runtime>
</configuration>
and My private probing path contains the correct files (already checked that)
anyone has a clue?
I just recently set up fluent nhibernate with SQLite for unit tests so here are some tips I can give you.
If you are debugging with Visual Studio 2010 you will need to use SQLite 32 bit sadly. There is a known bug that should be included with the next release of visual studio when you try to include 64 bit assemblies.
Other than that might I suggest you make sure you have included the following entries in your web config.
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
PS found a reference to the bug
http://connect.microsoft.com/VisualStudio/feedback/details/556670/could-not-load-file-or-assembly-error-when-referencing-a-64-bit-assembly