Skip to content

Instantly share code, notes, and snippets.

@kentpickard
kentpickard / ConnectionGroupManager
Last active August 29, 2015 14:10
Xamarin MonoTouch: Example of manually closing a ServicePoint connection group when there are no pending requests. This is needed to allow network calls to continue working when the user switches from one wireless network to another. This is a workaround for bug https://bugzilla.xamarin.com/show_bug.cgi?id=23806
public class UserApiClient : BaseApiClient
{
// Example of making a call to API in order to get a user by ID
public Task<User> GetUser(int Id)
{
// using RunWithConnectionCloser will call ServicePoint.CloseConnectionGroup as long as there are no other pending operations.
// Its important that all service calls are wrapped with RunWithConnectionCloser
return BaseApiClient.RunWithConnectionCloser (() => {
// Make service call using RestSharp (what I'm using), HttpWebRequest, HttpClient, or whatever