Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IIS锁定我上传的文件时出现问题_C#_Asp.net - Fatal编程技术网

C# IIS锁定我上传的文件时出现问题

C# IIS锁定我上传的文件时出现问题,c#,asp.net,C#,Asp.net,我有一个小的上传应用程序,我使用一个Web服务(asmx)上传一个文件,检查MD5并验证它。问题是,当我验证该文件时,它表示该文件已被另一个进程锁定。下面是我上传和验证的代码: private static object padlock = new object(); 将上传文件分成小块,然后上传每个小块 [WebMethod] public void LargeUpload(byte[] content, string uniqueName) {

我有一个小的上传应用程序,我使用一个Web服务(asmx)上传一个文件,检查MD5并验证它。问题是,当我验证该文件时,它表示该文件已被另一个进程锁定。下面是我上传和验证的代码:

private static object padlock = new object();
将上传文件分成小块,然后上传每个小块

[WebMethod]
        public void LargeUpload(byte[] content, string uniqueName)
        {
            lock (padlock)
            {
                string path = Server.MapPath(PartialDir + "/" + uniqueName);
                BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Append, FileAccess.Write));
                writer.Write(content);
                writer.Flush();
                writer.Close();
                writer = null;
            }
        }
在最后一个之后,将其插入我的数据库。在此之后,客户端通过请求MD5来验证文件:

[WebMethod]
        public int EndLargeUpload(string name, int folderId, long length, string uniqueName, int customerid)
        {
            lock (padlock)
            {
                string path = Server.MapPath(PartialDir + "/" + uniqueName);
                string newPath = Server.MapPath(RepositoryDir + "/" + uniqueName);
                File.Copy(path, newPath);
                //delete partial
                File.Delete(path);
                string extension = Path.GetExtension(uniqueName);
                string newFileName = uniqueName;
                GWFile newFile = new GWFile();
                newFile.DiscName = newFileName;
                newFile.FileName = name;
                newFile.FolderId = folderId;
                newFile.Description = "";
                newFile.Size = (int)length;
                newFile.DiscFolder = Server.MapPath("/Repository");
                newFile.DiscRelativePath = "/Repository/" + newFile.DiscName;
                newFile.CustomerId = customerid;
                IGWFileRepository fileRepository = ObjectFactory.GetInstance<IGWFileRepository>();


                fileRepository.SaveFile(newFile);
                return newFile.Id;
            }
        }
[WebMethod]
public int-EndLargeUpload(字符串名称、int-folderId、长、字符串uniqueName、int-customerid)
{
锁(挂锁)
{
字符串路径=Server.MapPath(PartialDir+“/”+uniqueName);
字符串newPath=Server.MapPath(RepositoryDir+“/”+uniqueName);
复制(路径,新路径);
//删除部分
删除(路径);
字符串扩展名=Path.GetExtension(uniqueName);
字符串newFileName=uniqueName;
GWFile newFile=新GWFile();
newFile.DiscName=newFileName;
newFile.FileName=名称;
newFile.FolderId=FolderId;
newFile.Description=“”;
newFile.Size=(int)长度;
newFile.DiscFolder=Server.MapPath(“/Repository”);
newFile.discreativepath=“/Repository/”+newFile.DiscName;
newFile.CustomerId=CustomerId;
IGWFileRepository fileRepository=ObjectFactory.GetInstance();
fileRepository.SaveFile(newFile);
返回newFile.Id;
}
}
在EndLargeUpload()方法之后,客户端使用文件的id调用RequestMD5方法,此调用例外,因为它无法打开文件“…xxx…”,因为另一个进程正在使用它

private string GetMD5HashFromFile(string fileName)
            {
                lock (padlock)
                {
                    using (FileStream file = new FileStream(fileName, FileMode.Open)) // <-- excepts here
                    {
                        MD5 md5 = new MD5CryptoServiceProvider();
                        byte[] retVal = md5.ComputeHash(file);
                        file.Close();

                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < retVal.Length; i++)
                        {
                            sb.Append(retVal[i].ToString("x2"));
                        }
                        return sb.ToString();
                    }
                }
            }
私有字符串GetMD5HashFromFile(字符串文件名)
{
锁(挂锁)
{

使用(FileStream file=newfilestream(fileName,FileMode.Open))//EndLargeUpload方法中的最后两行如何:

IGWFileRepository fileRepository = ObjectFactory.GetInstance<IGWFileRepository>();
fileRepository.SaveFile(newFile);
IGWFileRepository fileRepository=ObjectFactory.GetInstance();
fileRepository.SaveFile(newFile);

是否有可能IGWFileRepository.SaveFile()没有正确关闭文件?

切换到IIS,问题似乎消失了…

提示:您不需要关闭一式三份的BinaryWriter(刷新、关闭、设置为null),调用
close()
已经足够了。或者更好的是,你可以使用
来使用
块。你在这个应用程序中还有其他文件I/O会接触到这些文件吗?对于这种类型的项目,我建议使用
IIS
而不是
Cassini
文件存储库的SaveFile方法会以任何方式接触到文件吗?另外,我注意到y您正在MD5方法中对filestream调用Close和Dispose。您在哪一行遇到错误?我根本没有其他文件I/O。我的存储库中的SaveFile方法只是数据库,它不涉及文件。我感觉file.Copy方法仍然保留对该文件的引用。请注意,该文件是从O复制的ld locatio和屏幕截图显示它被锁定在新位置。@Jakob:我在MD5方法的“使用”行中遇到错误我也有同样的问题,请解释“切换到IIS”是什么意思?