TypeLoadException_ExceptionWindow

This issue stroke me suddenly and without any reason (at least I couldn’t find one). One day code that rotates picture, which was captured from camera, started to throw exceptions. I was really surprised, because I didn’t change much in the project since the time it was working just fine..Till this day, I have no idea what was the cause of it

TypeLoadException_RotatingPictureCode

What I figured out after some research on the Internet and looking in the generated *.appx package files (appx ~ zip – by changing extension of your *.appx to *.zip you can check what’s in it)  I discovered that for some reason the AppxManifest.xml was lacking entry with the title library mentioned in the title of this blog post (highlighted entry is the one that was missing and had to be added manually)

<?xml version="1.0" encoding="UTF-8"?>
<Extension>
   <InProcessServer>
      ...
      <ActivatableClass ActivatableClassId="Lumia.Imaging.Transforms.RotationFilter" ThreadingModel="both" />
      <ActivatableClass ActivatableClassId="Lumia.Imaging.Transforms.FlipFilter" ThreadingModel="both" />
      <ActivatableClass ActivatableClassId="Lumia.Imaging.Transforms.ScaleFilter" ThreadingModel="both" />
      <ActivatableClass ActivatableClassId="Lumia.Imaging.Transforms.CropFilter" ThreadingModel="both" />
      <ActivatableClass ActivatableClassId="Lumia.Imaging.Transforms.ImageAligner" ThreadingModel="both" />
      <ActivatableClass ActivatableClassId="Lumia.Imaging.ImageProviderInfo" ThreadingModel="both" />
      <ActivatableClass ActivatableClassId="Lumia.Imaging.StorageFileImageSource" ThreadingModel="both" /> // This was missing
   </InProcessServer>
</Extension>

I couldn’t work it out how I can force mechanism standing behind creating packages (*.appx) to include this in the AppxManifest.xml, so the only solution was to insert it after package was made. To automate this process, of unpacking files from *.appx package, making amendments to the AppxManifest.xml and then repacking again all the files into *.appx pckage, I wrote a pretty ‘nasty’ PowerShell script which uses makeappx.exe (to unpack and repack) and some other PowerShell ‘hacks’ ;]

#    FUNCTIONALITY:
#     - Extracts createded package (*.appx) with makeappx.exe
#         NOTE: Takes first *.appx file that it finds in the folder that script is located in
#     - Opens and adds, by replacement of known entry, missing entry in the AppxManifest.xml file
#     - Saves changed file
#     - Repackages the *.appx with makeappx.exe

$makeappxPath = ''
$makeappxExecPath = '';
$64BitPath = 'C:\Program Files (x86)\Windows Kits\8.1\bin\x64'
$64BitPathExists = Test-Path $64BitPath
if($64BitPathExists -eq $True)
{
    $makeappxPath = $64BitPath
}

$makeappxExecPath = $makeappxPath + '\makeappx.exe'

# Script execution location
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

# Listing the appx files in the script location
$Dir = get-childitem $PSScriptRoot -recurse
$List = $Dir | where {$_.extension -eq '.appx'}
# Taking first appx
$AppxFileFullPath = $PSScriptRoot + '\' + $List[0].Name

$TempUnpackPath = $PSScriptRoot + '\TempUnpack';
$NewAppxFilePath =  $PSScriptRoot + '\NewPacked_WithCameraClassRegistered.appx';
$AppxManifestFilePath = $TempUnpackPath + '\AppxManifest.xml'

# Unpack
Invoke-Expression "& `$makeappxExecPath` unpack /p $AppxFileFullPath /d $TempUnpackPath"

# Changing content of AppxManifest
(Get-Content $AppxManifestFilePath |
Foreach-Object {$_ -replace "<ActivatableClass ActivatableClassId=`"Lumia.Imaging.ImageProviderInfo`" ThreadingModel=`"both`" />", "<ActivatableClass ActivatableClassId=`"Lumia.Imaging.ImageProviderInfo`" ThreadingModel=`"both`" />`n<ActivatableClass ActivatableClassId=`"Lumia.Imaging.StorageFileImageSource`" ThreadingModel=`"both`" />"}) |
Set-Content $AppxManifestFilePath

# Pack
Invoke-Expression "& `$makeappxExecPath` pack /d $TempUnpackPath /p $NewAppxFilePath"

# Remove unpacked folder
Remove-Item $TempUnpackPath -recurse

In the end, all this script does is inserting one line of code into your AppxManifest.xml file, which is

<ActivatableClass ActivatableClassId="Lumia.Imaging.StorageFileImageSource" ThreadingModel="both" />

After executing this script, your package should be ready to go, and your Lumia’s (old Nokia’s) libraries shouldn’t fail again