Partager via


Win10 apps in .NET- common library issues

This is Part 2 of my "VB Win10 Apps" series.

 

General VS2015 RC known issues

In general, the "known issues" are all detailed in this forum: https://social.msdn.microsoft.com/Forums/en-US/home?forum=Win10SDKToolsIssues

But that forum didn't cover the library cases that I've run into. So here they are:

 

Json.NET

In VS2015 RC, at runtime, when you invoke JsonConvert.SerializeObject or JsonConvert.DeserializeObject, then you might get a FileNotFoundException saying that it can't load the file "System.Runtime.Serialization".

Fix1: use the "latest stable 6.0.8" version of Newtonsoft.Json, not the preview 7.* version.

Explanation: the 7.* versions of Newtonsoft.Json are using a new way of structuring their NuGet package. It's a fine way, and will work in VS2015 RTM, but just doesn't work yet in RC. The exact text of the error message is

An exception of type 'System.IO.FileNotFoundException' occurred in Newtonsoft.Json.dll but was not handled in user code. Additional information: Could not load file or assembly 'System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its components

 

Fix2: if you're trying to serialize/deserialize a type that's defined in a PCL, then change the PCL targets to exclude Silverlight5, and to only use .NET >= 4.5.1

Explanation: If your PCL targets .NET4.0.3 or older, or it targets Silverlight5, then it is a so-called "old style PCL". These will work fine in UWP apps when we get to VS2015 RTM, but they're not quite fully working in RC. You'll find this for instance if you try to reflect upon or serialize a class that's defined in such an old style PCL and has <DataContract> on it. The exact text of the error message is similar to the above, but comes from a different assembly:

An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code. Additional information: Could not load file or assembly 'System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its components

 

 

SignalR

SignalR Client is currently an "old-style PCL" and suffers from the same problem as above. The fix [2] above is to rebuild SIgnalR and remove the Silverlight target. This is a difficult task :( but MVP Joost van Schaik has helpfully provided detailed instructions:

https://dotnetbyexample.blogspot.it/2015/05/getting-signalr-clients-to-work-on.html

 

 

SharpDX

SharpDX is a set of managed wrappers to use DirectX from within VB and C# apps. It's what underpins other high-level games libraries like MonoGame.

SharpDX works fine in UWP apps in VS2015 RC. It's a NuGet package: you add a reference to it via Project References > Manage NuGetReferences.

  • SharpDX.Toolkit doesn't install on VS2015 RC for UWP apps. I'm not sure if it's going to be able to install in VS2015 RTM. Note that the SharpDX toolkit is deprecated and no longer exists in SharpDX preview-3.0 going forwards.

 

 

SQLite

SQLite is the best way to have a local database in your UWP app. It works fine in VS2015 RC.

  1. Install the "pre-release Sqlite for UAP apps" from the Sqlite download page https://www.sqlite.org/download.html. This is only needed once per developer machine.
  2. Within your project, right click on References > AddReference > WindowsUniversal > Extensions > SQLite for Universal App Platform

That installs the low-level Sqlite native library. (Don't worry about the current build-time warning "No SDKs found"). On top of that you need a VB-accessible wrapper from it. There are a few common wrappers:

 

Sqlite-net.  This NuGet wrapper is currently delivered as C#-source-code only. So you'll need to create a trivial C# PCL to call it from VB...

  1. Right click on Solution > Add > NewProject > C# > Windows > Windows8 > Class Library (Portable for 8.1 Universal).
  2. Within that C# PCL, right click on References > Manage Nuget References > sqlite-net
  3. Within that C# PCL, go to Project > Properties > Build. Change the dropdown at the top to "Configuration: all configurations. And inside "Conditional compilation symbols type NETFX_CORE
  4. Within your app, right click on References > AddReference > Projects > Solution, and add a reference to the PCL you just created.

Here's some example sqlite-net code. I've shown two techniques to query the database, using "db.QueryAsync" and "db.Table.Where". The second db.Table.Where form currently has a limitation in sqlite-net, which doesn't understand string equality tests from VB (until they accept a pull-request I sent them!)

Async Function TestSqlAsync() As Task
    Dim db As New SQLiteAsyncConnection("db_name_1")
    Await db.CreateTableAsync(Of Customer)
    Await db.InsertAsync(New Customer With {.Name = "Fred", .Age = 41})
    Dim results1 = Await db.QueryAsync(Of Customer)("select * from Customer")
    Dim results2 = Await db.Table(Of Customer).Where(Function(c) c.Age > 20).ToListAsync()
End Function

Public Class Customer
    <PrimaryKey, AutoIncrement> Public Property Id As Integer
    Public Property Name As String
    Public Property Age As Integer
End Class

 

Sqlite.WinRT.UAP - I've not personally used this NuGet package and don't know what it's like

 

LiveSDK

The LiveSDK hasn't yet been updated for UWP. Also in VS2015 RC you can't yet right-click on your project > Store > Associate App With Store. So there's not much you could do anyway.

 

 

IAsyncAction

There's an error message that a few people have encountered:

The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows,Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.

This error should never occur! It's usually a sign that you tried to upgrade from an earlier version of VS! If you get this error on a clean install of VS2015 RC, please let me know urgently. Thanks.

Comments

  • Anonymous
    June 26, 2015
    I am getting the IAsyncAction error message on a clean install of VS2015 RC. It is complaining about types IAsyncAction, IAsyncOperation<>, IAsyncActionWithProgress<>,  and IAsyncOperationWithProgress<>, showing the exact same error message that you wrote. Is there a fix for this?

  • Anonymous
    July 24, 2015
    I'm also getting the IAsyncAction/IAsyncOperation<> error.  I'm using the VS2015 RC released just a few days ago.  Here is the compilation error message:


The type 'IAsyncAction' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.

I have a .NET 4.5.2 class library, and I manually edited the csproj file to point my TargetPlatformVersion to "10.0" so that I could take advantage of the DirectInk libraries.  In addition to editing the csproj, I added references for "Windows.Foundation" and "Windows.UI" from the Windows Core reference libraries (which all have version 255.255.255.255 by design).  I also browsed to the "C:WindowsMicrosoft.NETFramework64" directory to include references to System.Runtime.dll, System.Runtime.InteropServices.WindowsRuntime.dll, and System.Runtime.WindowsRuntime.dll. I was following the directions outlined by this blog post from MSFT: blogs.msdn.com/.../winrt-for-desktop-apps.aspx Any help to resolve this would be greatly appreciated!

* **Anonymous**  
July 27, 2016  
I found a possible solution, I was working on a desktop c# project and also changes the target platform version to 10.0 to use the WinRT Windows.Media speech recognition API's. First I had added the Windows.Media Reference through the Universal Windows tab that appears in the References menu after editing the .csproj file, and while the Windows.Media assembly was recognized I also had a problem with the IAsyncOperation.It worked by removing the Windows.Media reference and manually adding a reference to the assembly in:C:/Program Files (x86)/Windows Kits/10/UnionMetadata/Windows.winmd  
  • Anonymous
    July 24, 2015
    @Tam you're following instructions for how to call WinRT from your .NET desktop apps. I too was fully able to do this with Win8.1-era WinRT, and also wrote an article on the subject at www.codeproject.com/.../How-to-call-WinRT-APIs-from-NET-desktop-apps I've been trying this and other variations so far with UWP apps but it hasn't work. (although I got different failure messages from you). I haven't yet learned what the new trick is to call UWP-era WinRT from desktop apps. Once I figure out how, I'll definitely post about it. Sorry I can't help more.

  • Anonymous
    July 28, 2015
    I've just installed Windows 10 (vanilla install) & VS 2015 and I also have the error. The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows,Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'. My project is a simple WPF application that needs access to Storage & LockScreen.

  • Anonymous
    July 30, 2015
    Also getting "error CS0012: The type 'IAsyncAction' is defined in an assembly...". I uninstalled previous versions of VS2013 and VS2015 community editions. I reinstalled the official VS2015. To reproduce the error I created a blank universal app and added the following function to MainPage.xaml.cs: async void TestFunc() {    var socket = new Windows.Networking.Sockets.DatagramSocket();    await socket.BindServiceNameAsync(""); } Sure would be nice to have a fix for this. I've already wasted too much time on it.

  • Anonymous
    July 31, 2015
    Modify your VS installation and make sure that Windows and Web Development/Windows 8.1 and Windows Phone 8.0/8.1 Tools/Tools and Windows SDKs is installed. After that, add a reference to the Windows assembly as described in the tutorials.

  • Anonymous
    July 31, 2015
    The comment has been removed

  • Anonymous
    August 09, 2015
    I gave in and did a clean install of Win10 and VS2015. Uninstalling and reinstalling VS2015 was not enough for me. The problem is now fixed. As expected, the output assembly doesn't have a reference to the assembly that the original error reported as necessary.

  • Anonymous
    October 17, 2015
    Has anyone had any success solving the problems with the 'IAsync...' files, or with the '... exists in both...' duplication errors?

  • Anonymous
    October 17, 2015
    @Joel please email me your project, lwischik@microsoft.com, and I'll diagnose the issue

  • Anonymous
    December 09, 2015
    I'm trying to build this example DeviceEnumerationAndPairing from Windows-universal-samples-master  in Visual Studio Community 2015 version 14.0.24720.00 update 1, and i have the same problem with  'IAsyncAction'. Anybody help me, I would appreciate.

  • Anonymous
    December 22, 2015
    I am having the error "The type 'IAsyncAction' is defined in an assembly...". After I let the 10511 update install on W10 enterprise. After the windows update I launched VS and all my UWP apps now have this error. I then installed update 1 and included everything(including the new windows SDK for 10586) no luck. I guess the next step is to uninstall VS and reinstall it...

  • Anonymous
    May 27, 2016
    I received the above message on a (hopefully) clean new install of Vis Studio 2015 using the Win 10 Universal template. This did not occur before the recent update of Vis Studio. This caused me to try and repair Vis Studio w/o success, then uninstall it 2015 directories and other programs that install with it, I deleted all the directories associated with these programs. The one thing I did not do was clean the registry. The same error message occurs.

  • Anonymous
    June 18, 2016
    Hello,I had the same problem with the IAsyncAction and I have just actually managed to solve it, so I am going to share the (maybe not the best) solution.First I tried repair VS, no success. Then reinstalling VS, again no success. So I went to control panel -> programs -> uninstall programs, and from there I uninstalled everything that had something to do with Visual Studio or .NET. It displayed me a couple of errors, like that some applications require that program to run, but I anyway uninstalled it (if I will need it I will install it again). After that I installed Visual Studio again, wrote the code which caused the error and, it worked, with no error.So the problem was in one of the programs I had to uninstall, but I do not know exactly which one, I uninstalled them all at once.