0

I have followed this tutorial to create a push notification hub for an IOS app.

When i run the code

private static async void SendNotificationAsync()
{
     NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
     var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
     await hub.SendAppleNativeNotificationAsync(alert);
}

from static void Main(string[] args) of a console program , nothing happens, the console just stops.

If I use

private static  void SendNotificationAsync()
{
     NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
     var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
     hub.SendAppleNativeNotificationAsync(alert).Wait();
}

everythings works fine

Update

As Yuval Itzchakov said in the answer bellow, Console Application's main method can't be marked async, so the async method will not be waited

2
  • You need to show how you are calling these methods - include the code in your question. Commented Sep 26, 2014 at 14:23
  • I'm going to take a punt that you are calling this from a method which can't await this method as it is an async void, so there is no way of awaiting method to complete. Commented Sep 26, 2014 at 14:49

1 Answer 1

5

A Console Application's main method can't be marked async. Hence, you need to explicitly use Task.Wait or Task.Result on the async operation to make sure the consoles main method doesn't terminate, hence closing the entire process.

I am assuming the call is along the lines of:

public static void Main(string[] args)
{
    // Do some stuff
    SendNotificationAsync();
}

You need to do two things:

  1. Change SendNotificationAsync to async Task instead of async void so you can Wait on the returned Task. Note async void is ment solely for the compatibility of async event handlers:

    private static async Task SendNotificationAsync()
    {
        NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
        var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
        await hub.SendAppleNativeNotificationAsync(alert);
    }
    
  2. Use Task.Wait at the top of your call stack:

    public static void Main(string[] args)
    {
       // Do some stuff
       SendNotificationAsync().Wait();
    }
    

This will work fine with a Console Application. This approach is NOT recommended for any application having a custom SynchronizationContext other than the default ThreadPoolSynchronizationContext. Always use await all the way down in those type of applications.

Sign up to request clarification or add additional context in comments.

4 Comments

So if i would use the async method in the web api "ecosystem", I should have any problems with private static async Task SendNotificationAsync() { NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false); var alert = "{\"aps\":{\"alert\":\"Hello\"}}"; await hub.SendAppleNativeNotificationAsync(alert); } ?
Why do you assume you'd have any problem?
I wanted to say i shouldn't have a problem
You should be perfectly fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.