Could not load file or assembly System.Buffers after deploy using windows installer
Description
I'm wrting a VSTO addin and using System.Text.Json to seralize a config. It works while debuging using Visual Stutio with both Debug and Release configuration, but throws exception after installing using windows installer.
Reproduction Steps
To reproduce it, you could create a default VSTO visio addin with .Net Framework 4.8 and add the follow code to ThisAddIn class
public class Config {
public string Name { get; set; }
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
var config = new Config() { Name = "abc" };
var jsonString = JsonSerializer.Serialize(config);
MessageBox.Show(jsonString);
}
catch (Exception exception) {
MessageBox.Show(exception.Message);
}
}
Then create a windows installer project as what microsoft taught
Expected behavior
The program should display the serialized string. Actually, it used to work two weeks ago.
Actual behavior
The addin failed to load. If you attach to the Visio app then load the addin manually, the immediate window for Visual Studio shows
Exception thrown: 'System.Deployment.Application.DeploymentException' in System.Deployment.dll
Exception thrown: 'System.BadImageFormatException' in System.Text.Json.dll
Exception thrown: 'System.TypeInitializationException' in System.Text.Json.dll
If you log using NLog, it throws BadImageFormatException with System.Buffers unable to load.
2024-01-23 11:04:12.1739|Error|Failed to save configuration | System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---> System.BadImageFormatException: Could not load file or assembly 'System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 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) ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
--- End of inner exception stack trace ---
at System.Text.Json.JsonEncodedText.TranscodeAndEncode(ReadOnlySpan`1 value, JavaScriptEncoder encoder)
at System.Text.Json.JsonEncodedText.Encode(String value, JavaScriptEncoder encoder)
at System.Text.Json.JsonSerializer..cctor()
--- End of inner exception stack trace ---
at System.Text.Json.JsonSerializerOptions.GetOrCreateDefaultOptionsInstance()
at System.Text.Json.JsonSerializerOptions.get_Default()
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type inputType)
at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
at VisioAddIn4.Config.Save()
Regression?
No response
Known Workarounds
I've tried to install System.Buffers through NuGet. I've tried to create a app.config to specify rebinding. I've tried to switch to PackageRefernce.
Configuration
.NET Version: 4.8 OS Version: Windows 11 Pro 22H2 22621.3007 Architecture: x64
Other information
No response
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
Issue Details
Description
I'm wrting a VSTO addin and using System.Text.Json to seralize a config. It works while debuging using Visual Stutio with both Debug and Release configuration, but throws exception after installing using windows installer.
Reproduction Steps
To reproduce it, you could create a default VSTO visio addin with .Net Framework 4.8 and add the follow code to ThisAddIn class
public class Config {
public string Name { get; set; }
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
var config = new Config() { Name = "abc" };
var jsonString = JsonSerializer.Serialize(config);
MessageBox.Show(jsonString);
}
catch (Exception exception) {
MessageBox.Show(exception.Message);
}
}
Then create a windows installer project as what microsoft taught
Expected behavior
The program should display the serialized string. Actually, it used to work two weeks ago.
Actual behavior
The addin failed to load. If you attach to the Visio app then load the addin manually, the immediate window for Visual Studio shows
Exception thrown: 'System.Deployment.Application.DeploymentException' in System.Deployment.dll
Exception thrown: 'System.BadImageFormatException' in System.Text.Json.dll
Exception thrown: 'System.TypeInitializationException' in System.Text.Json.dll
If you log using NLog, it throws BadImageFormatException with System.Buffers unable to load.
2024-01-23 11:04:12.1739|Error|Failed to save configuration | System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---> System.BadImageFormatException: Could not load file or assembly 'System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 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) ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
--- End of inner exception stack trace ---
at System.Text.Json.JsonEncodedText.TranscodeAndEncode(ReadOnlySpan`1 value, JavaScriptEncoder encoder)
at System.Text.Json.JsonEncodedText.Encode(String value, JavaScriptEncoder encoder)
at System.Text.Json.JsonSerializer..cctor()
--- End of inner exception stack trace ---
at System.Text.Json.JsonSerializerOptions.GetOrCreateDefaultOptionsInstance()
at System.Text.Json.JsonSerializerOptions.get_Default()
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type inputType)
at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
at VisioAddIn4.Config.Save()
Regression?
No response
Known Workarounds
I've tried to install System.Buffers through NuGet. I've tried to create a app.config to specify rebinding. I've tried to switch to PackageRefernce.
Configuration
.NET Version: 4.8 OS Version: Windows 11 Pro 22H2 22621.3007 Architecture: x64
Other information
No response
| Author: | Snailya |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Odd. Have you tried clearing your local NuGet caches and trying again?
Odd. Have you tried clearing your local NuGet caches and trying again?
i've tried clear nuget cache from both Visual Stuio but it not works.
Later, i tried used json.net instead, then it is hacked, but other referenced package begin to throw BadImageFormatException, so i think it might not related to system.text.json but my OS. I'll try using other's laptop to see if it works.
Possibly. If it reproduces consistently we would probably need some sort of reproduction to evaluate the issue ourselves.
This issue has been marked needs-author-action and may be missing some important information.
I've tried using another laptop while it still not works. I noticed that there are two warning in pre-build validation for installer project.
WARNING: Unable to find dependency 'SYSTEM.RUNTIME.COMPILERSERVICES.UNSAFE' (Signature='B03F5F7F11D50A3A' Version='4.0.4.1') of assembly 'System.Memory.dll'
WARNING: Unable to find dependency 'SYSTEM.RUNTIME.COMPILERSERVICES.UNSAFE' (Signature='B03F5F7F11D50A3A' Version='4.0.4.1') of assembly 'System.Threading.Tasks.Extensions.dll'
I have a app.config file which redirect bindings for SYSTEM.RUNTIME.COMPILERSERVICES.UNSAFE from 4.0.4.1 to 6.0.0.0. But in the installer project, i don't know whether this config should be included and how to. Will that matter?
i've uploaded a demo project at https://github.com/Snailya/BadImageFormatExceptionDemo.git
After changing the target framework and target platform for the demo project, i found out the only comination works is the VisioAddIn with Target Framework 4.7. Installer Project with Target Platform x86. But it still not work for my original project.
Possibly. If it reproduces consistently we would probably need some sort of reproduction to evaluate the issue ourselves.
Now I'm sure its something related to System.Text.Json. I refactored my code by replacing Serialize, Deserialize method from System.Text.Json and GetFromJsonAsync<T> from System.Net.Http.Json by Json.Net. It now works as expected after deployment.
This issue has been marked needs-author-action and may be missing some important information.
This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.
This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.