Console App (Non UWP) with CS/WinRT

baget 221 Reputation points
2020-11-29T14:37:06+00:00

Hi
I have created a Console App (NonUWP) that use CS/WinRT

I was able to access WinRT API like Windows.Devices.Custom

but the problem is that await is not working for me, I'm getting errors. i can worked around it using
GetResults() but it is ugly.

any idea what can cause the issue?

when I'm waiting await I'm getting the following errors:

error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
error CS0012: 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'.
error CS0012: The type 'IAsyncActionWithProgress<>' 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'.
error CS0012: 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'.
error CS0012: The type 'IAsyncOperationWithProgress<,>' 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 is my csproj:

  <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0-windows10.0.19041</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <CsWinRTIncludes>Contoso</CsWinRTIncludes>
    <CsWinRTGeneratedFilesDir>$(OutDir)</CsWinRTGeneratedFilesDir>
    <CsWinRTWindowsMetadata>10.0.19041.0</CsWinRTWindowsMetadata>
    <Nullable>annotations</Nullable>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" />
    <PackageReference Include="Microsoft.Windows.CsWinRT" Version="1.1.0" />
    <PackageReference Include="System.Runtime.InteropServices.WindowsRuntime" Version="4.3.0" />
    <PackageReference Include="System.Runtime.WindowsRuntime" Version="4.7.0" />
  </ItemGroup>

</Project>
Universal Windows Platform (UWP)
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Manodasan Wignarajah 11 Reputation points
    2020-12-11T01:21:40.713+00:00

    @baget when you reference the net5.0-windows10.0.19041.0 target framework (TFM), you get an implicit reference to the CsWinRT generated Windows SDK projection for the specified version. You would be able to use that to write a console app that makes use of the Windows WinRT APIs. Details can also be found here.

    It looks like the error you are seeing with await is due to some reference conflicts that are occurring when you reference the Microsoft.Windows.SDK.Contracts package. Can you try removing that package reference along with the other 3 package references. After that you should still be able to build due to the reference from the TFM providing you the Windows WinRT APIs and should also be able to use await.

    You typically only need a CsWinRT package reference along with a Microsoft.Windows.SDK.Contracts package reference if you are trying to build a CsWinRT projection for your own WinRT component. If you are trying to do that, I would recommend building that as a separate projection DLL and then referencing it as part of your app. That should avoid the issues you are running into.

    Let me know if you still run into any issues after doing that.

    2 people found this answer helpful.
    0 comments No comments

  2. Cheong00 3,471 Reputation points
    2020-11-30T01:09:34.997+00:00

    The first one is obvious, you need to add async keyword in front of the method that contain your call.

    If you run into problem that your main() method does not support async keyword, you may want to check this out.

    The others all complain you have not added "Windows" library to your Reference .

    1 person found this answer helpful.

  3. Timon Yang-MSFT 9,576 Reputation points
    2020-12-08T07:04:55.777+00:00

    You can find the Windows library at this place as long as you have the UWP workload installed:

    C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0

    After adding it, try to use GetAwaiter() to solve your problem.
    46038-image.png

    Visual Studio's IntelliSense cannot recognize this method, so please enter or copy it manually.

    For console applications, it may be a good alternative to async-await.

    Is .GetAwaiter().GetResult(); safe for general use?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.