0

I have changed target framework from <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> to <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> for a C++ project. And now I'm getting an exception:

Exception thrown: 'System.Configuration.ConfigurationErrorsException' in System.Configuration.dll log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.

System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.TypeInitializationException: The type initializer for 'System.Uri' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.UriParser' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Runtime.Versioning.BinaryCompatibility' threw an exception. ---> System.ArgumentException: String cannot be of zero length. Parameter name: frameworkName at System.Runtime.Versioning.BinaryCompatibility.ParseFrameworkName(String frameworkName, String& identifier, Int32& version, String& profile)
at System.Runtime.Versioning.BinaryCompatibility.ParseTargetFrameworkMonikerIntoEnum(String targetFrameworkMoniker, TargetFrameworkId& targetFramework, Int32& targetFrameworkVersion) at System.Runtime.Versioning.BinaryCompatibility.ReadTargetFrameworkId() at System.Runtime.Versioning.BinaryCompatibility.get_AppWasBuiltForFramework() at System.Runtime.Versioning.BinaryCompatibility..cctor() --- End of inner exception stack trace --- at System.UriParser..cctor()
--- End of inner exception stack trace --- at System.Uri..cctor() --- End of inner exception stack trace --- at System.Configuration.ClientConfigurationSystem..ctor() at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() --- End of inner exception stack trace --- at System.Configuration.ConfigurationManager.PrepareConfigSystem() at System.Configuration.ConfigurationManager.get_AppSettings() at log4net.Util.SystemInfo.GetAppSetting(String key)

My investigation gives me the reason: while compilation VS2015 adds [assembly: TargetFramework("", FrameworkDisplayName = "")]. That's why I can't add the correct TargetFrameworkAttribute manually. And this automatically added attribute causes an exception as the target framework can't be an empty string.

Do you have any advice, on how could I fix this problem?

Update: Well, I've enabled logging as Hans Passant said. But I had no luck. As there was no word about .Net 4.0 and Visual Studio created correct file %temp%\.NETFramework,Version=v4.8.AssemblyAttributes.cpp, but the content of the file:

#using <mscorlib.dll> [assembly: System::Runtime::Versioning::TargetFrameworkAttribute(L"", FrameworkDisplayName=L"")];

Nevertheless, I found a hack - I'm adding the required attribute manually. To do it I defined a variable in the project file:

 <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>

This variable avoids the automatic creation of the TargetFrameworkAttribute. So I can add the correct attribute from my code:

[assembly:TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")];

With this workaround, I can start the application and it seems to work everything... but I do not really like it.

Vadim
  • 471
  • 6
  • 15
  • I don't think 2015 supports 4.8, e.g see https://stackoverflow.com/questions/43420178/can-i-use-visual-studio-2015-to-target-net-framework-4-7/43420229, you need to install the 4.8 targeting pack – Alan Birtles Jan 26 '22 at 07:56
  • Well, I have installed .Net 4.8 developer pack. And I'm able to select .Net 4.8 for C# projects. The attribute `TargetFramework` will be added for C# projects correctly. After I've changed the target version for the C++ project I see, that VS shows correct references for System.* assemblies. But compiled exe references assemblies form .Net 4.0... – Vadim Jan 26 '22 at 08:45
  • 1
    Use Tools > Options > Projects and Solutions > Build and Run > "MSBuild project build output verbosity" = Diagnostic. Rebuild and the Output window now gives a lot more details. Search for "GenerateTargetFrameworkMonikerAttribute", that's where things go wrong. In a nutshell, it generates a small file that should be named .NETFramework,Version=v4.8.AssemblyAttributes.cpp in order to get the project setting compiled into the output. – Hans Passant Jan 26 '22 at 10:29

0 Answers0