问题
I have an app I'm converting from VS2008 to VS2012. The app is mixed mode and made of multiple projects. After upgrading, the app crashes on run as soon as managed code is entered with this exception:
An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll
Additional information: The type initializer for 'System.Windows.Application' threw an exception.
The deepest inner exception is:
"String cannot be of zero length. Parameter name: frameworkName"
The stack trace is:
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()
This ONLY happens after a clean build, and will happen every time it runs, until I clean a single project and then build again. Then it works every time I run it.
I looked at the .exe files with JetBrains and compared what they look like when the app works and when it crashes. When the app crashes, this line is in the .exe:
[assembly: TargetFramework("", FrameworkDisplayName = ".NET Framework 4")]
Obviously the framework string is blank, and that is causing the exception. When the app works, that line is not present, so no exception.
EDIT FOR CLARITY: The app works when the above line is totally absent from the .exe manifest. I have confirmed that in VS2008, the above line is not in the .exe manifest. It looks like it shouldn't be in the manifest at all, and on clean builds in VS2012 it is getting added and causing problems. Also, I am not using an app.config file.
Is there a way to fix this?
回答1:
I've been working on this for 3 days and I just found the answer.
Apparently MSBuild auto-creates a file called
.NETFramework,Version=v4.0.AssemblyAttributes.cpp
in your
C:\Users\YOURNAME\AppData\Local\Temp
directory. It uses this file when creating your .exe file.
On my machine this file contained the following:
#using <mscorlib.dll>
[assembly: System::Runtime::Versioning::TargetFrameworkAttribute(L"", FrameworkDisplayName=L".NET Framework 4")];
The empty string was causing my problems. I changed the file to say this:
#using <mscorlib.dll>
[assembly: System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.0", FrameworkDisplayName=L".NET Framework 4")];
Now everything works! I don't know how the file got into that state, but I never touched it. I don't think it's used in VS2008. I'm somewhat irked that such a file is being used in my build.
回答2:
Fixed my problem by deleting everything in C:\Users\YOURNAME\AppData\Local\Temp :)
回答3:
I've been struggling with this all day. I tried the solution above but no joy. In the end I changed the Target Framework form .Net 4.5 to .Net 4 and then recompiled. I then changed back the Target Framework back to .Net 4.5. This sorted it.
Nick
回答4:
After days spent investigating, I've discovered that the "corrupted" .NETFramework,Version=v4.0.AssemblyAttributes.cpp file was caused by one CPP project configured as follows:
<ConfigurationType>StaticLibrary</ConfigurationType>
<CLRSupport>true</CLRSupport>
Changing project configuration and deleting %TEMP% directory has been a definitive solution.
Just deleting %TEMP% does not guarantee that the file will not be generated corrupted again, so cannot be considered as a definitive solution.
回答5:
- This info injected from file %TEMP%\.NETFramework,Version=%.NET_VERSION%.AssemblyAttributes.cpp
- The file is generated only if it does not exist in your temp dir
- When compiling "mixed" lib it will generate the corrupted file with empty .NET version, and all mixed DLLs which compiled after this, will get corrupted!
To solve it you need to delete %temp%, and add this line to all your mixed lib projects (or via global property sheets)
<!-- Must be located after <ConfigurationType>StaticLibrary</ConfigurationType> --> <GenerateTargetFrameworkAttribute Condition="'$(ConfigurationType)'=='StaticLibrary'">false</GenerateTargetFrameworkAttribute>- It will turn off the corrupted file generation during mixed lib compilation
- This is a bug in msbuild scripts, which exists in all latest Visual Studios (Including VS2013 Update 5 and VS2015 Update 3)
回答6:
C++ application with CLR support.
Unhandled Exception: System.TypeInitializationException
The type initializer for '' threw an exception.
Fixing the C:\Users\YOURNAME\AppData\Local\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.cpp file worked.
来源:https://stackoverflow.com/questions/13315940/apps-exe-file-missing-net-targetframework-but-only-on-clean-builds