2
votes

I'm building a .NETStandard library (version 1.3), using async await inside the library works fine until i add a reference to Windows.Foundation.UniversalApiContract. After adding this reference i get errors on every await keyword inside my code

this is the error

'IAsyncAction' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncAction' could be found

this is the code i need to run

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { OnUserChanged?.Invoke(user); });

removing the await keyword result in a working code, but i get this warning

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

How can i solve this?

1
You shouldn't add such a reference if it is UWP specific. - Lex Li
Your library isn't going to be "standard" at all when you hard-code a WinRT type like CoreApplication. And async/await does work very differently under the hood in a UWP app, nothing particularly obvious from the keywords. Since it is only ever going to work in a UWP app, do consider using the "Class Library (Universal Windows)" project template to get ahead. - Hans Passant
the problem is that I need to rais this event and this event is going to modify an object on the UI thread. If I'm not going to use "CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync" i got a cross thread access - frenk91
How do you "add a reference to Windows.Foundation.UniversalApiContract"? - I don't think this is appropriate for a netstandard library. - Stephen Cleary

1 Answers

0
votes

Make sure you have using System; at the top of your code file. That has the extension required to turn IAsyncAction to a Task.

If that doesn't work, try this statement (just to see if the code compiles and runs):

var a = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { OnUserChanged?.Invoke(user); });

await System.WindowsRuntimeSystemExtensions.AsTask(a);

If the above doesn't work for you, then try temporarily upgrading to .Net Standard 1.4 (again, just to see if it compiles and runs)

And if that doesn't work, then there might be something messed up with the library references.