SynchronizationContext Error in client call

Hi Guys,

Getting an error on one of my calls from client to API, not sure I’ve seen this error before in my code.

authClient = new JsonHttpClient(url) {BearerToken = bearer, HttpMessageHandler = new NativeMessageHandler() };

News_Response result= await authClient.PostAsync<News_Response>( new News_Request() { Currency = currency })
.Success(r => {
})
.Error(ex =>{
});

return result.News;

The error that I get displays “The current SynchronizationContext may not be used as a TaskScheduler.”

I also only see this error on the first call of this service, thereafter it works as expected.

Using latest version of Servicestack within UWP app and calling API from PCL library.

Any ideas ?

Thank again.

That’s not an error we’ve seen in Service Clients, but you shouldn’t use await and Task handlers at the same time, if you’re using await, use a try/catch to handle Exceptions instead:

try
{
    var result= await authClient.PostAsync(new News_Request { Currency = currency });
} catch(Exception ex) { ... }

Just for my own understanding.

I assume this is OK

public async Task<List<NEWS_DBT>> GetAsync(string currency)
	{
		    try
		    {
            News_Response assetsResponse = await authClient.PostAsync<News_Response>(
                    new News_Request() { Currency = currency });
               

		        return assetsResponse.News;

		    }
		    catch (Exception ex)
		    {
		        Debug.WriteLine("News_Service : GetAsync Error" + ex.ToString());
		        return null;
		    }
	}

This is not OK = >

public async Task<List<NEWS_DBT>> GetAsync(string currency)
	{
		    try
		    {
            News_Response assetsResponse = await authClient.PostAsync<News_Response>(
                    new News_Request() { Currency = currency })
                     .Success(r =>
		            {
		            })
		            .Error(ex =>
		            {
		            });
		        return assetsResponse.News;

		    }
		    catch (Exception ex)
		    {
		        Debug.WriteLine("News_Service : GetAsync Error" + ex.ToString());
		        return null;
		    }
	}

There’s a lot of inconsistent .NET naming in both :slight_smile: and APIs that return collections should ideally return an empty collection (i.e. nut null), but yes use the top version.

Thanks , I can never make up my mind whether to return null or empty :wink:

if I return empty collection then I assume the collection is “really” empty and then I display the empty result to user.

If i return null, i know there was an error in connection and then I rather display data from a local store.