Tag: sqlfilestream

线程和SqlFileStream。 该进程无法访问指定的文件,因为它已在另一个事务中打开

我正在提取SQL文件表中的文件的内容。 如果我不使用Parallel,以下代码可以正常工作。 我同时读取sql文件流(并行)时遇到以下exception。 该进程无法访问指定的文件,因为它已在另一个事务中打开。 TL; DR: 在Parallel.ForEach中从FileTable读取文件(使用GET_FILESTREAM_TRANSACTION_CONTEXT)时,我得到上述exception。 示例代码供您试用: https://gist.github.com/NerdPad/6d9b399f2f5f5e5c6519 更长版本: 获取附件,并提取内容: var documents = new List(); using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var attachments = await dao.GetAttachmentsAsync(); // Extract the content simultaneously // documents = attachments.ToDbDocuments().ToList(); // This works Parallel.ForEach(attachments, a => documents.Add(a.ToDbDocument())); // this doesn’t ts.Complete(); } DAO读取文件表: public async Task<IEnumerable> GetAttachmentsAsync() { […]