locked
Can't await StreamSocket.ConnectAsync RRS feed

  • Question

  • I am trying to use the await operator with this method, but can't:

    await serverConnection.ConnectAsync(
                    new HostName("localhost"),
                    triviaPort);

    Keep getting this error:

    Error 2 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'. 

    However, the ConnectAsync() method is awaitable.  I also can't resolve the StreamSocketConnectOperation class like I see suggested in several code samples.

     
    Thursday, May 17, 2012 1:05 AM

Answers

  • Hi,

    Yes, this method is awaitable. The error message is pointing out your own method is not marked as async. Check if you have marked your method as async. Alternatively, you can let your method return Task. If you’re invoking async methods inside a lambda expression, please also mark the lambda expression as async:

                var a = new Action(async () =>
                {
                    StreamSocket socket = new StreamSocket();
                    await socket.ConnectAsync(new EndpointPair());
                });

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, May 17, 2012 8:57 AM
    Moderator

All replies

  • Hi,

    Yes, this method is awaitable. The error message is pointing out your own method is not marked as async. Check if you have marked your method as async. Alternatively, you can let your method return Task. If you’re invoking async methods inside a lambda expression, please also mark the lambda expression as async:

                var a = new Action(async () =>
                {
                    StreamSocket socket = new StreamSocket();
                    await socket.ConnectAsync(new EndpointPair());
                });

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, May 17, 2012 8:57 AM
    Moderator
  • Hi,

    I will mark the reply as an answer. If you find it no help, please feel free to unmark it and follow up.

    Thanks.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Wednesday, May 23, 2012 5:01 AM
    Moderator