I'm trying to allow a Desktop Bridge application to register for WNS notifications from an Azure Notification Hub, but when I actually use the UWP APIs it throws
`System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.'
with an inner exception of
FileNotFoundException: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
I set up my solution following the Visual Studio packaging instructions. My package project has a target version of Fall Creators Update, minimum version of Anniversary Update, and I associated it with a Windows Dev Center project for WNS support. All my .NET Framework projects are targeting v4.6.2. If I don't call any UWP APIs, the packaged application runs perfectly.
All the WNS code is in a single Class Library project, and that project is referenced by my desktop application (which is added to the package project's Applications). The class library has references for all six files in Microsoft's tutorial, with Copy Local = False for the three .winmd files:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.winmd
C:\Program Files (x86)\Windows Kits\10\References\10.0.16299.0\Windows.Foundation.UniversalApiContract\5.0.0.0\Windows.Foundation.UniversalApiContract.winmd
C:\Program Files (x86)\Windows Kits\10\References\10.0.16299.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd
The actual push notification function is inside an async method:
public namespace WnsClassLibrary
{
public class WnsChannelService
{
private PushNotificationChannel _channel;
public async Task CreateChannel()
{
_channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
}
}
}
When the desktop application starts it tries to call CreateChannel()
as an unawaited async method, and that's when the exception is thrown - as far as I can tell, it doesn't even actually make it inside the method.
Does anyone know why this happens or how I fix it? I tried setting the packaging project's minimum version to Fall Creators Update as suggested in UWP application: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.14.0, but I still get the same exception.
It looks like the problem might not actually be in Desktop Bridge or the UWP API references, but just a misconfigured assembly binding redirect.
While trying to get the exception to show up in a smaller solution, I found
<configuration>
<!--[unrelated configuration data...]-->
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.WindowsRuntime" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
in the app.config
files for both the desktop app and the WNS class library. Deleting that <runtime>
element makes everything run fine, and copy/pasting it into my reproduction project makes the System.IO.FileNotFoundException start showing up.
I can't figure out what added that assembly binding redirect or make it happen again, but the desktop app was originally .Net 4.5 and took plenty of experimentation to migrate to .Net 4.6.2, update all the Nuget packages, and implement Desktop Bridge. My best guess is that something may have triggered a Nuget automatic configurator along the way?