This post was most recently updated on April 6th, 2023.2 min read.
… or “How to bang your head on a wall for hours on end when developing a MAUI application?” 😂
This article explains how to fix a surprising error you might get when trying to run a MAUI app in Windows. For me, it was thrown when I was simply reading a file from the disk, but I suspect the same or similar error (“System.TypeInitializationException: ‘The type initializer for ‘WinRT.ActivationFactory`1′ threw an exception.’ – COMException: ClassFactory cannot supply requested class”) can probably be thrown for other reasons as well.
Anyway – let’s take a closer look at the issue and when it was thrown for me, shall we?
The error gets thrown at perhaps the unlikeliest part of your code ever. When you’re reading a file with System.IO.File on Windows. Like – shouldn’t System.IO stuff work in platform-specific MAUI builds? And it works just fine on Android. But on Windows, this code throws:
using Stream fileStream = System.IO.File.OpenRead(filePath); using StreamReader reader = new StreamReader(fileStream); var c = await reader.ReadToEndAsync();
And the error you get is a pretty drastic one:
System.TypeInitializationException: 'The type initializer for 'WinRT.ActivationFactory`1' threw an exception.' COMException: ClassFactory cannot supply requested class
Perhaps you can get similar errors for different reasons, too. But I suspect the fix might quite often be the same, as far as you’re trying to build stuff with MAUI.
Okay – so what to do to fix the issue at hand?
Annoying, but I haven’t found the proper fix to this. That’s not to say I wouldn’t have got a pretty good set of workarounds documented below for trying out! 😁
Time needed: 30 minutes.
Solving “System.TypeInitializationException: ‘The type initializer for ‘WinRT.ActivationFactory`1’ threw an exception.'” in MAUI app?
- Restart Visual Studio
Boring, but it’s a good idea to start by restarting Visual Studio.
- Restart your machine
Even more boring, but restarting your machine is the next step. I had to restart mine before the next steps actually worked.
So since you’ll probably need to do this anyway, probably a good idea to try it first before trying the long-running commands in steps 3 & 4.
Come back when you’re done!
- Repair .NET workloads
The following command – run in Visual Studio Developer Shell (or in any other shell, really) – should fix your workloads if they’re broken for whatever reason. I suppose this should’ve fixed the issue for me, but it didn’t.
dotnet workload repair
- Reinstall maui-windows workload
Maybe just your maui-windows .NET workload has broken. And maybe workload repair didn’t fix it for whatever reason. Who knows – it’s easy enough to try just reinstalling maui-windows – and somehow this one finally fixed the issue for me:
dotnet workload install maui-windows
Hope this helps! If you’ve figured out other ways to fix this, let me know in the comments section below!
A couple of sources I found useful in the investigation:
- “Something happened on our end” on Microsoft Store - May 30, 2023
- How to fix OnePlus 10 Pro Bluetooth issues - May 23, 2023
- How to actually uninstall conflicting PowerShell modules? - May 16, 2023