WCF 图像服务正在锁定文件

WCF Image Service is Locking Files

我正在做一个 c# wcf 服务,在该服务中我收到一堆图像,该服务将它们合并到一个多图像 Tiff 文件中。在服务结束时,我想删除原始文件,但我收到一条错误消息,指出其他进程正在锁定该文件。

这是接收图像(作为 byte[] 列表)并将它们写入磁盘的代码

public static List<string> SaveByteImagesToFile(List<byte[]> bytesToCopyIntoFiles, string imageReferenceType, string imageReferenceValue)
        {
            _applicationLogger.Debug(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name);

            string imageFinalPath = string.Empty;
            string joinImagesFilePath = string.Empty;

            List<string> imagesFilePath = new List<string>();

            int count = 1;

            try
            {
                if (bytesToCopyIntoFiles.Count == 0)
                {
                    throw new ArgumentNullException("bytesToCopyIntoFiles");
                }
                else
                {
                    joinImagesFilePath = SettingsManager.GetServiceSetting(AppSettingsKeys.CopyImagesToFilePath, "NO_VALID_FILEPATH");

                    if (joinImagesFilePath.IsValidFilePath(out string errorMessage, true, true))
                    {

                        foreach (byte[] image in bytesToCopyIntoFiles)
                        {
                            var imageFileName = imageReferenceType + "_" + imageReferenceValue + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + count.ToString();
                            imageFinalPath = joinImagesFilePath + Path.DirectorySeparatorChar + imageFileName + ".tiff";

                            using (FileStream stream = new FileStream(imageFinalPath, FileMode.Create, FileAccess.ReadWrite))
                            {
                                stream.Write(image, 0, image.Length);
                                stream.Flush();
                            }

                            imagesFilePath.Add(imageFinalPath);
                            count++;
                        }
                    }
                    else
                    {
                        exceptionMessageType = MainRepository.GetExceptionMessage("E171");
                        throw new IOException(exceptionMessageType.ExceptionMessage + " " + errorMessage);
                    }
                }
                return imagesFilePath;
            }
            catch
            {
                throw;
            }
        }  

如何或使用什么来防止服务或任何进程锁定文件。如您所见,我正在使用文件流的使用范围,但没有任何运气。

有什么想法吗?谢谢

已解决!通过按特定顺序组织文件,在创建多页 tiff 时,当逻辑结束时,工作人员已经解锁了资源,我现在可以毫无问题地删除它们。