我在磁盘上有很多文件需要读取和反序列化。如果我尝试使用多个任务的流读取器访问这些文件,我会得到以下异常:
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, '../uploads');
},
filename: function (req, file, callback) {
callback(null, file.originalname + '-' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''));
}
});
var video = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, '../attachuploads');
},
filename: function (req, file, callback) {
callback(null, file.originalname + '-' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''));
}
});
var upload = multer({ storage : storage, video : video });
/* Create Course */
router.post('/create', upload.any(),function(req, res, file) {
var uploadpath =dirname+'/uploads/'+ req.files[0].originalname+ '-' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
var videopath =dirname+'/attachuploads/'+ req.files[0].originalname+ '-' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
var courses = new Course({
course_Name: req.body.coursename,
course_Code: req.body.coursecode,
course_Title: req.body.coursetitle,
course_Author: req.body.courseauthor,
course_Discription: req.body.coursediscription,
course_Attachments: uploadpath,
course_Video: videopath,
course_Urlpath: req.body.courseurlpath,
course_StartingDate: req.body.coursestartingdate,
course_EndingDate: req.body.courseendingdate
});
// save the sample course
courses.save(function(err) {
if (err) {throw err;}
res.json({ success: true });
});
});
以下是代码:
System.ObjectDisposedException: Cannot access a disposed object
System.Runtime.InteropServices.SafeHandle.DangerousReleaseInternal(bool dispose)SafeHandle.cs:193
System.Runtime.InteropServices.SafeHandle.DangerousRelease()SafeHandle.cs:142
System.IO.FileStream.Dispose(bool disposing)FileStream.cs:913
System.IO.IsolatedStorage.IsolatedStorageFileStream.Dispose(bool disposing)IsolatedStorageFileStream.cs:250
System.IO.Stream.Close()stream.cs:248
System.IO.StreamReader.Dispose(bool disposing)streamreader.cs:296
System.IO.TextReader.Dispose()textreader.cs:78
System.IO.TextReader.Dispose()(wrapper remoting-invoke-with-check)
.FileServices.StreamReaderWrapper.Dispose()
.FileServices.IsolatedSerializer.<DeserializeAsync>d__9<T>.MoveNext()
我在这里做错了什么?
public async Task<T> DeserializeAsync<T>(string path)
{
T result = default(T);
if (!_fileRepository.FileManager.FileExists(path)) { return result; }
using (var streamReader = _streamReader.GetNew(_fileRepository.FileManager.OpenFile(
path, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
var contents = await streamReader.ReadToEndAsync();
result = _jsonSerializer.DeserializeFromString<T>(contents, Encoding.UTF8);
streamReader.Dispose();
}
return result;
}
答案 0 :(得分:2)
您正在拨打Dispose
两次。退出代码块后,using语句将自动处理您的streamReader
变量。但你也明确地呼吁streamReader.Dispose();
另外,你的StreamReaderWrapper.GetNew()
应该只返回一个新的包装器实例,否则线程会互相踩到脚趾
答案 1 :(得分:1)
我认为你无法访问&#34; getNew&#34;功能,因为你已经处理了对象..
来自您的代码:
使用(var streamReader = _streamReader.GetNew(_fileRepository.FileManager.OpenFile(
)谁&#34; _streamReader&#34;?包装器或包装读取器的实例?