Application settings error after changing target framework of project - c#

In my application I am using user settings as explained here.
Then I realized that in VS 2010 I was using .NET 4.0 while only .NET 2.0 was sufficient.
When I changed the framework and build the project, in my code whenever I access setting now, I get the following error:
An error occurred creating the
configuration section handler for
userSettings/Vegi_Manager.Properties.Settings:
Could not load file or assembly
'System, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089' or
one of its dependencies. The system
cannot find the file specified.
(C:\Users\AKSHAY\AppData\Local\Microsoft\Vegi-Manager.vshost.exe_Url_44035dlkzpfaaauiqsd4nh3f0l0yq0tv\1.0.0.0\user.config
line 5)
It is for unknown reasons using version 4.0.
Please suggest what should I do.

Check out your app.config maybe you still have System.Configuration.UserSettingsGroup or something of that sort of version 4 still lurking around, you will have to manually edit and get the relevant for version 2

I was having the same problem having started developing my Outlook add-in in Visual Studio 2010 targeting the .NET 4.0 framework and then deciding to change it to the 3.5 framework. It seems Visual Studio was NOT smart enough to update my app.config file which still had a reference to 4.0.
As previous posters have suggested (I'll be just a little bit more specific), by manually editing app.config to change all references from "Version=4.0.0.0" to "Version=2.0.0.0" (apparently .NET 3.5 still uses 2.0.0.0) in the <sectionGroup> element and its child elements, I was able to get the settings working again in my case.

It is likely that you have a reference to a .NET 4 assembly in your solution - if you open the "Add Reference" window in your solution and make the window a bit wider, you will see that there are columns for Version and Runtime... when you created your solutions to target .NET 4, you might have added a reference to an assembly that requires the .NET 4 runtime and now you have changed to .NET 2, you need to swap it for an assembly that only needs the .NET 2 runtime.
Of course, if you have used something form the .NET 4 assembly that didn't exist in .NET 2, you will either have to re-write to avoid using it or change your mind and do it in .NET 4 instead!

if you open your app.config, make sure all the config sections are targeting .net framework 2 not 4
if you post your app.config to me i can help more

May I add here that you need to change the app.config details / references to the previous .NET framework (in this case 4.0.0.0) to the new System refence .NET version (in this case 2.0.0.0 which is the same for .NET 3.5!)

Related

Does Windows cache the .NET version used by an assembly?

I made the mistake of trying to deploy a program built with .NET 4.7.1 on a machine that didn't have that version of the .NET framework installed. It popped the following error message:
Since installing a new .NET framework requires a reboot, I don't want to go that route yet. So I tried downgrading the target framework to 4.6.1... but I still get the same error!
I verified that the assembly was compiled targeting the 4.6.1 framework:
As a test, I renamed the assembly VueLinks2.exe and it started just fine.
The only thing I can think of is that Windows "remembers" what framework is associated with an assembly. Is that correct? Is it possible to undo that and downgrade an app to a previous version without renaming the file?
There's no cache. Deciding what framework to use is a combination of the <requiredRuntime> (legacy) and <supportedRuntime> elements in the [exe].app.config and the assembly's framework version if those are not supplied. Renaming the executable will cause the [exe].app.config to no longer be matched.

'Could not load file or assembly 'netstandard, Version=2.0.0.0, ...'. Reference assemblies should not be loaded for execution

Goal:
From a .NET 4.7 console app, using reflection with Assembly.GetType(), I am trying extract the Type of a netstandard 2.0 class from Assembly X. Then I want to create an instance of this Type with Activator.CreateInstance().
What I am trying to do:
However, this assembly X has a dependency to netstandard 2.0. To be able to get the Type, netstandard dependency has to be loaded into the AppDomain. That's why when the AppDomain is requesting the netstandard assembly through the AssemblyResolve event, I simply load the dll like this :
var netStandardDllPath = #"C:\Users\xxx\.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll";
return Assembly.LoadFrom(netStandardDllPath);
Which throws:
System.BadImageFormatException: 'Could not load file or assembly
'file:///C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll'
or one of its dependencies. Reference assemblies should not be loaded
for execution. They can only be loaded in the Reflection-only loader
context. (Exception from HRESULT: 0x80131058)'
Inner Exception: BadImageFormatException: Cannot load a reference
assembly for execution.
What I know:
I know that they want us to load the DLL with Assembly.ReflectionOnlyLoadFrom. But doing that will prevent me from instanciate the type with Activator.CreateInstance(). See Microsoft official post
Also, I tried referencing the Nuget packages NETStandard.Library 2.0.0-preview1-25301-01 and NETStandard.Library.NETFramework 2.0.0-preview1-25305-02 in my console app so it would have the netstandard 2.0 libraries referenced, but it didn't change anything.
Question:
Does anyone would know if there is a proper way to load that dll without error, or maybe if this is a bug, or else? Or why this kind of dll is not able to load for execution?
The netstandard.dll you are trying to load is a reference assembly that which cannot be loaded for runtime on .NET Framework as pointed out by others. However if you need to resolve that dependency you will need to runtime version that maps to the framework you are trying to run on.
For .NET Standard support we are including them as part of the msbuild extensions that ship with VS so you will want to get the version of netstandard.dll from there. Depending on which version of VS2017 you have installed it should be somewhere like C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll or from the .NET Core 2.0 SDK you can find it C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll
Try using one of those versions in your scenario.
Wow. I just spent several hours tracking the cause of this "could not load ... netstandard" error down.
For me, the problem was that my .NET Framework project (which references both .NET Framework and .NET Standard libraries) was built with .NET Framework 4.7.2 and the system where I was deploying and running it did not have 4.7.2 installed.
Deploying a very small Console project with the same basic structure and references and executing that in a Command window finally revealed the correct error, in a pop-up, that .NET Framework 4.7.2 was missing.
If you're struggling with this particular error, make sure you have the necessary .NET Framework installed.
Set Copy Local to true in netstandard.dll properties.
Open Solution Explorer and right click on netstandard.dll.
Set Copy Local to true.
You can't load a reference assembly.
.NET Standard is a collection of APIs that must be provided by .NET Standard compatible implementations.
A reference assembly only contains contracts. This means that it contains no implementation. The assembly you are trying to load contains the .NET Standard 2.0 contracts.
A contract looks like this: https://github.com/dotnet/standard/blob/master/netstandard/ref/mscorlib.cs
EDIT: .NET Framework 4.7 implements .NET Standard 2.0, so you shouldn't need to load any assembly to use Activator.CreateInstance() to instantiate a .NET Standard type.
NETStandard 2.0.0-preview1 in not compatibility with net461 and net47.
but for realese .NET Core SDK 2.0 assemblies (as well as 2.0.0-preview2)
var netStandardDllPath = #"c:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\netstandard.dll";
Console.WriteLine(Assembly.LoadFrom(netStandardDllPath).FullName);
all is ok.
But if you steel need to load preview1 libraries, maybe you should to use netstandard2.0 instead net471.
For me solved doing the following:
1 - Installed latest .Net Framework on server.
2 - Updated windows server and my local machine.
3 - Went to Manage Nuget Package and updated all references on the update tab.
Perhaps only doing step 3 can solve in your case
In case if IBM Message Queue references are used in the project solution, this exception indicates that the DLL used for refering MQ classes are incompatible with the host(server) .NET version installed.
In this scenario, either we need to update server with latest update and make sure .NET latest version is available or use lower version of IBM Message queue DLL as reference.
Old version DLL - amqmdnet.dll (no new features will be introduced by IBM as not in support)
Latest version DLL - amqmdnetstd.dll (to run IBM MQ classes for .NET Standard, you must install Microsoft .NET Core)
Install NetStandard.Library 2.0.0.0 from NuGet , It works for me. when I downgrade .net framework 4.6.1 to 4.6.0
If you are having this issue for a project that used to work, try deleting the bin and obj folders since caching can cause this, too.

Could not load file or assembly System.DirectoryServices. This assembly is built by a runtime newer than the currently loaded runtime [duplicate]

I have downloaded published (code behind files are no there, combined with dll in bin folder) web application from Window Server 2008 where it is hosted, and open it with Visual Studio when I debug that application it shows following error:
"Could not load assembly because this assembly is built by a runtime newer than the currently loaded runtime"
I don't know how can I solve this problem and test application locally.
Please help me.
This errors happens when the DotNet framework you are using is of older version than the one used to build the assembly. You need to check which version of framework is used to build those assemblies and then use the same or higher to debug too.
I was getting this same error when running an installer for a Windows service, even when running the installer on the PC the installer was built on.
It turned out that although the Windows service project had been updated to .NET 4.5, the Setup project that was making the installer was still set to use .NET 2.0.
To check if the Setup project is using an older version of .NET than the project to be installed, in the Visual Studio Solution Explorer:
Expand the Setup project;
Under the Setup project, expand Detected Dependencies;
Under Detected Dependencies select Microsoft .NET Framework and check the Version property. Select the appropriate .NET version from the dropdown list;
Re-build the Setup project to create a new version of the installer.
This error can have a lot of other reasons, too. I had the same problem, and nothing helped until I stumbled across this:
TlbExp.exe error:This assembly is built by a runtime newer
I just ran into this issue when the assembly was built with a target framework of .NET 4, and v4.0.30319 was installed on the server, and other 4.0 apps were running successfully.
The problem arose because the app had originally been built targeting 2.0, and new 4.0 assemblies were pushed, but not the app.config file, which we generally update separately.
This means the supportedRuntime attribute was not updated in the config and caused the error. Adding the following to the app.config fixed our issue:
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
One the the assemblies reference in the project might be built using a newer version of .net, check version of every assembly reference and correct accordingly.
Also check the application pool that this web site is running as. It could be framework 2.0 default on some older windows servers. Change it to framework 4.0.

Log4net reference assembly dependency issue [duplicate]

This question already has answers here:
Log4Net in WCF not working
(6 answers)
Closed 9 years ago.
I am a Java developer and have just started learning C# to develop a couple project. I am happy to see that many Java frameworks that I am used to work with (log4j, ant, hibernate etc.) have their .net versions (log4net, nant, nhibernate).
I have just created a project and tried to put log4net assembly reference in it, but I am having the following warning (followed by 4 errors informing failure to recognize log4net namespace and classes):
The referenced assembly "log4net"
could not be resolved because it has a
dependency on "System.Web,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" which
is not in the currently targeted
framework
".NETFramework,Version=v4.0,Profile=Client".
Please remove references to assemblies
not in the targeted framework or
consider retargeting your project.
I added reference to System.Web.ApplicationServices and System.Web.Services but it's not working. Any help on solving this dependency issue will be much appreciated.
Thanks
UPDATE:
If you're still getting this error, you're using an out-of-date version of log4net and you should consider updating to the current version, which does not require the full .NET 4.0 framework. Thanks to #Philippe for pointing this out in a comment.
Your project is referencing the .NET 4.0 client profile which does not include System.Web; you actually need to reference the full framework.
See How To: Target a Specific .NET Framework Version or Profile on MSDN for directions.
In Visual Studio, open the project you want to change.
Right-click the project in Solution Explorer and then click Properties.
In the Project Designer, locate the Target Framework list, as follows.
For Visual Basic projects, click the Compile tab and then click
Advanced Compile Options. The Target
Framework list is in the Advanced
Compiler Settings dialog box.
For Visual C# projects, the Target Framework list is on the
Application tab of the Project
Designer. For more information, see
Application Page, Project Designer
(C#).
For Visual F# projects, the Target Framework list is on the
Application tab of the Project
Designer.
In the Target Framework list, select the .NET Framework version or
profile that you want. When you click
OK, the project unloads and then
reloads in the integrated development
environment (IDE). The project now
targets the .NET Framework version
that you just selected.
Possibly also of interest is Troubleshooting .NET Framework Targeting Errors.
Set your project to target the ".Net 4.0 Framework", not the ".Net 4.0 Framework Client Profile" in the project properties dialog.
Just small update.
This issue has been logged almost 3 years ago and was fixed just this month. So next version of log4net (after 1.2.10) should be fine to work with client profile frameworks.
https://issues.apache.org/jira/browse/LOG4NET-174
If you are getting this error with version 1.2.11 and you don't want to change your project's framework, simply install the version for the client profile. This is in the net-cp folder in the binary distribution.
As a reference for future users:
Don't forget to also INSTALL the framework you target!
(I, myself, thought that because all the folders (v4.0x, v2.0X, etc.) were there I had all frameworks. NOT!
It turns out I only had the .NET 4.0 client profile installed on my system and could not find the System.Web, even though the right framework was targeted.
Anyway, download your needed .net framework here:
.NET Frameworks Microsoft Downloads

Referencing a .NET 2 assembly in a project targeting .NET 4

I am currently using TeeChart Lite (the free version of teechart). I would like to use it in my WPF project however the downloadable free version is only available for .NET 2.0 therefore I get the following error:
Warning 1 The referenced assembly
"TeeChart.Lite" could not be resolved
because it has a dependency on
"System.Design, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" which
is not in the currently targeted
framework
".NETFramework,Version=v4.0,Profile=Client".
Please remove references to assemblies
not in the targeted framework or
consider retargeting your project.
Is it safe to just add the assembly System.Design, Version 4.0.0.0 even though it is not part of the targeted framework? Or is there something else I can do to get it working in .NET 4.0?
I don't have Visual Studio available to test, but it's probably one of these two things:
More likely, you just need to reference System.Design.dll, which is a standalone DLL.
Less likely: as the error message states, you are targeting the 4.0 client framework, which is the default for new projects in VS2010. Go to your project properties and reference the full 4.0 framework, which might solve the problem.
The actual solution may require combining both steps.

Categories