locked
Can I call async methods of WinMD from JavaScript?

    Question

  • Hi folks,

     

    I'm developing metro style app with html/javascript and writing WinMD with C#.

    This WinMD has async methods and i want to call this method from JavaScript.

     

    When i tried compiling this projects, error occured (async methods is not a valid WinRT type).

    Can't I call async methods of WinMD from JavaScript?

     

    thanks.

    Sunday, October 16, 2011 7:13 AM

Answers

  • Hi Takecian,

    You should be able to do this.  You would want to expose your method using the IAsyncOperation intrface. Something like: IAsyncOperation<String> DoSomethingAsync(). Inside of your function you would I do not think we have a C# example covering this but there are C++ samples.

    Here is a quick sample I tried out:

    C# definition:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Foundation;

    namespace AsyncCSharpLib
    {
        public sealed class AsyncMethods
        {

                public IAsyncOperation<string> Get(string url)
                {

                    // this is Key.  It converts an async task into IAsyncOperation:
                    return AsyncInfoFactory.Create(() => GetInternal(url));
                }

                private static async Task<string> GetInternal(string url)
                {
                    var client = new HttpClient();
                    var response = await client.GetAsync(url);
                    return response.Content.ReadAsString();
                }
            }
     
    }

     

    Consume in JS:


        WinJS.Application.onmainwindowactivated = function (e) {
            if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                document.querySelector('.btnGetClass').addEventListener('click', function () {
                   
                    var aMeth = new AsyncCSharpLib.AsyncMethods();
                   
                    aMeth.get("http://www.microsoft.com").then(function(resultingHtml){
                        var ndstring = resultingHtml;
                    });
               
                }, false);
               
            }
        }

     

     

     

    The C# Forums would be a great place to ask any questions if you run into any issues!

    -Jeff


    Jeff Sanders (MSFT)
    • Marked as answer by takecian Wednesday, October 19, 2011 9:39 AM
    Monday, October 17, 2011 2:15 PM
    Moderator

All replies

  • Hi Takecian,

    You should be able to do this.  You would want to expose your method using the IAsyncOperation intrface. Something like: IAsyncOperation<String> DoSomethingAsync(). Inside of your function you would I do not think we have a C# example covering this but there are C++ samples.

    Here is a quick sample I tried out:

    C# definition:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Foundation;

    namespace AsyncCSharpLib
    {
        public sealed class AsyncMethods
        {

                public IAsyncOperation<string> Get(string url)
                {

                    // this is Key.  It converts an async task into IAsyncOperation:
                    return AsyncInfoFactory.Create(() => GetInternal(url));
                }

                private static async Task<string> GetInternal(string url)
                {
                    var client = new HttpClient();
                    var response = await client.GetAsync(url);
                    return response.Content.ReadAsString();
                }
            }
     
    }

     

    Consume in JS:


        WinJS.Application.onmainwindowactivated = function (e) {
            if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                document.querySelector('.btnGetClass').addEventListener('click', function () {
                   
                    var aMeth = new AsyncCSharpLib.AsyncMethods();
                   
                    aMeth.get("http://www.microsoft.com").then(function(resultingHtml){
                        var ndstring = resultingHtml;
                    });
               
                }, false);
               
            }
        }

     

     

     

    The C# Forums would be a great place to ask any questions if you run into any issues!

    -Jeff


    Jeff Sanders (MSFT)
    • Marked as answer by takecian Wednesday, October 19, 2011 9:39 AM
    Monday, October 17, 2011 2:15 PM
    Moderator
  • Hi jpsanders,

     

    Thank you for your answer!

    I created a test project which includes your code, and built and ran it.

    It works fine!

     

    Thanks.

     

    Wednesday, October 19, 2011 9:42 AM
  • You are welcome!  I am glad I was able to help.  Enjoy Metro Style app development!

    -Jeff


    Jeff Sanders (MSFT)
    Wednesday, October 19, 2011 11:38 AM
    Moderator