Forum Xamarin.Forms

Xamarin cannot access disposed object error

karanthakkarkaranthakkar USMember ✭✭
edited December 2016 in Xamarin.Forms

i'm working with xamarin.forms, i'm trying to upload video content using MultipartFormDataContent with following code

 public async Task<bool> SaveNewsItemV1(EditNewsViewInputForMobile input)
        {
            try
            {

                NewsModels NewsInputModels = new NewsModels();
                NewsInputModels.NewsModel = input;

                //create new HttpClient and MultipartFormDataContent and add our file, and StudentId
                HttpClientHandler handler = new HttpClientHandler();

                using (var client = new System.Net.Http.HttpClient(handler, false))
                {
                    client.Timeout = new TimeSpan(0, 10, 0);

                    client.BaseAddress = new Uri(ServiceHelper.ServiceUrl);
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    string boundary = "---8d0f01e6b3b5dafaaadaad";
                    MultipartFormDataContent multipartContent = new MultipartFormDataContent(boundary);
                    foreach (var item in SelectedMediaList) // obj of  List<Plugin.Media.Abstractions.MediaFile>
                    {
                        if (item != null && item.GetStream() != null)
                        {
                            Stream stream = item.GetStream();

                            byte[] imageData;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                stream.CopyTo(ms);
                                imageData = ms.ToArray();
                            }

                            var baContent = new ByteArrayContent(imageData);

                            baContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
                            baContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                            {
                                Name = "file",
                                FileName = Path.GetFileName(item.Path)
                            };

                            multipartContent.Add(baContent);
                        }
                    }
                    try
                    {
                        var jData = JsonConvert.SerializeObject(NewsInputModels);
                        var content1 = new StringContent(jData, Encoding.UTF8, "application/json");
                        multipartContent.Add(content1, "Data"); // added input model data


                        var response = await client.PostAsync("SaveNewsItem/CommentNewsV1", multipartContent);

                        var result = response.Content.ReadAsStringAsync().Result;
                        var resultobject = JsonConvert.DeserializeObject<CommentResultData>(result);
                        return resultobject.Result;
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        return false;

                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;

            }
        }

in my case my HttpClient Throwing this error
Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.

Can anyone help with this??

Answers

Sign In or Register to comment.