locked
How to use multi async method in Windows Runtime Component (C#)

    Question

  • I want to migrate an async method into Windows Runtime Component.

    CookieContainer cc = await utility.GetCookieContainer();

    But the content of async method still contains async methods.

    public async Task<CookieContainer> GetCookieContainer()
            {
                if (stsAuthToken != null)
                {
                    if (DateTime.Now > stsAuthToken.Expires)
                    {
                        this.stsAuthToken = await GetMsoStsSAMLToken();
                        AuthCookies cookies = await GetAuthCookies(this.stsAuthToken);
                        CookieContainer cc = new CookieContainer();
    
                        Cookie samlAuthCookie = new Cookie("FedAuth", cookies.FedAuth)
                        {
                            Path = "/",
                            Expires = this.stsAuthToken.Expires,
                            Secure = cookies.Host.Scheme.Equals("https"),
                            HttpOnly = true,
                            Domain = cookies.Host.Host
                        };
    
                        cc.Add(this.spSiteUrl, samlAuthCookie);
    
                        Cookie rtFACookie = new Cookie("rtFA", cookies.RtFA)
                        {
                            Path = "/",
                            Expires = this.stsAuthToken.Expires,
                            Secure = cookies.Host.Scheme.Equals("https"),
                            HttpOnly = true,
                            Domain = cookies.Host.Host
                        };
    
                        cc.Add(this.spSiteUrl, rtFACookie);
    
                        this.cookieContainer = cc;
                    }
                }

    And even GetMsoStsSAMLToken and GetAuthCookies contain async methods...

    How to migrate it?

    Thursday, February 05, 2015 9:23 AM

All replies

  • You can use async/await in WinRT.  

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 05, 2015 8:10 PM
    Moderator
  • Hi Matt,

    Task<T> is not a WinRT type, It should be converted to IAsyncOperation<T>.

    But I don't know how to convert it.

    Friday, February 06, 2015 1:04 AM
  • 1) I can't convert your code because it contains reference to objects that you're not showing.

    2) If you have a question, please ask it specifically and don't make me guess as to what you want.  

    3) I have no idea why you're having problems.  This code compiles fine in a WinRT component:

     public async void GetCookieContainerMethod()
            {
                CookieContainer cc = await GetCookieContainer();
    
            }
    
            public async Task<CookieContainer> GetCookieContainer()
            {
                return new CookieContainer();
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 06, 2015 3:50 PM
    Moderator
  • Hi Matt,

    I'm sorry for my unclear description.

    My WinRT Component is for Javascript. If I use folloiong codes, error occurs.

    public static async Task<bool> GetInternal(string url, string username, string password)
            {
                bool r = await AuthUtility.Create(new Uri(url), username, password);
                return r;
            }
    


    The error is

    SharePointWindowsRuntimeComponent.Common.GetInternal(System.String, System.String, System.String)' has a parameter of type 'System.Threading.Tasks.Task<System.Boolean>' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types.  Consider changing the type 'Task' in the method signature to one of the following types instead: Windows.Foundation.IAsyncAction, Windows.Foundation.IAsyncOperation, or one of the other Windows Runtime async interfaces. The standard .NET awaiter pattern also applies when consuming Windows Runtime async interfaces. Please see System.Runtime.InteropServices.WindowsRuntime.AsyncInfo for more information about converting managed task objects to Windows Runtime async interfaces.

    Some articles said, Task<T> is not WinRT type and should be converted to IAsyncOperation<T>. Only stirng, int, bool, object, array of above types and some 'simple' type can be used. (The related article)

    Then I try to convert Task<bool>, but the codes don't work.

    Above is why I ask this question.

    If async method can be used in WinRT component directly, please let me know why I got above error.

    Thanks.

    Monday, February 09, 2015 1:35 AM