Forum General
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Having two WCF clients running in different threads - thread safe?

JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

We have a Xamarin.Forms app running on Android, which talks to two WCF services.

The Select service client runs in a thread, waiting on an endpoint to return with a set of changed data. This thread runs continuously, allow the app to respond to new data being available:

        ChangeOut ISelectServiceManager.WaitForChange(ChangeIn ChangeIn)
        {
            ChangeOut result = null;

            try
            {

                result = Task.Factory.FromAsync<ChangeIn, ChangeOut>(_selectService.BeginWaitForChange, _selectService.EndWaitForChange, ChangeIn, null).Result;


            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            return result;

        }

The Insert service client is run when the app has data to upload - calling the endpoint with the following code:

public Task<StatusOut> SendEntitiesToService(DataIn DataIn)
        {
            try
            {

                var result = new TaskFactory().FromAsync<DataIn, StatusOut>(_insertService.BeginEntityInsert, _insertService.EndEntityInsert, DataIn, null);
                return result;

            }
            catch (Exception ex)
            {
                HandleException(ex);
                return null;
            }
        }

These each work very well in general. However, we occasionally get timeout errors when we wouldn't expect them, and the app is sometimes unstable.

The underlying service clients for these methods were generated using the Silverlight Service Util as documented in the Xamarin 'Working with WCF' documentation.

My question is - Is it known whether running two WCF service clients at the same time is thread safe? Could there be some non thread-safe code in the generated clients or the libraries they use?

I've done some searching for thread safety and WCF clients, but have found nothing concrete.

Sign In or Register to comment.