Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Xamarin cannot access disposed object error

$
0
0

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??


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>