处置RestRequest对象RestSharp? [英] Dispose Object Of RestRequest RestSharp?

查看:109
本文介绍了处置RestRequest对象RestSharp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与RestSharp一起使用,并创建RestRequest对象以将FileData发送到API.但是在获得响应后,我想从本地计算机上删除文件,但是当我尝试执行同样的操作时,却出现错误"文件正在被其他进程使用".我认为原因是我无法处理RestRequest的对象.请帮我解决.下面是代码.在此先感谢.. !!!

I am working with RestSharp and create object of RestRequest for sending FileData to API. But after getting response I want to delete the file from my local machine but when I try to do the same it gives me the error "File is being used by other process". The reason I think is that I am unable to dispose the object of RestRequest. Please help me to solve it. Below is the code. Thanks in Advance..!!!

   public string PostMultiformDataAPI(Method method, string apiUrl, string data = "", Dictionary<string, string> headers = null)
        {
            string[] files = null;
            try
            {
                RestClient client = new RestClient(apiUrl);
                var request = new RestRequest();
                request.Method = method;

                //Add header values to request
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        request.AddHeader(header.Key, header.Value);
                    }
                }

                string filename = string.Empty;

                if (Directory.Exists(HttpContext.Current.Server.MapPath("/Upload")))
                {
                    files = Directory.GetFiles(HttpContext.Current.Server.MapPath("/Upload"));

                    foreach (string file in files)
                    {
                        request.AddFile(file.Split('/').Last(), file);
                    }
                }

                // execute the request
                IRestResponse response = client.Execute(request);
                var content = response.Content; // raw content as string

                foreach (string file in files)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    var fileInfo = new FileInfo(file);
                    fileInfo.Refresh();
                    fileInfo.Delete();

                    //File.Delete(file);
                }

                return content;

            }
            finally
            {

            }
        }

推荐答案

您只需为请求对象实例分配null即可删除其对文件的引用.为我工作.请让我知道它是否适合您.

You just need to assign null to request object instance to remove the reference it has to file. Worked for me. Please let me know if it works for you.

            // execute the request
            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw content as string

            request = null;
            response = null;

            foreach (string file in files)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                var fileInfo = new FileInfo(file);
                fileInfo.Refresh();
                fileInfo.Delete();
                File.Delete(file);
            }

            return content;

这篇关于处置RestRequest对象RestSharp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆