[英]This stream does not support seek operations, using stream
使用 C# ASP.net 4.5、Nopcommerce CMS、MVC 4 和 Visual Studio 2012。
這似乎是人們熟悉的問題,我已經搜索了“谷歌”,但尚未找到適合我需要的解決方案。
請注意,這不是我的代碼,我被要求解決這個問題(我對文件 I/O 的知識也有限,只是基礎知識)。
錯誤堆棧跟蹤是:
" at System.Net.ConnectStream.set_Position(Int64 value)\r\n at Hroc.Plugin.Core.CsvProductManagement.Services.CsvReader.Initialise(Stream stream, Encoding encoding)\r\n at Hroc.Plugin.Core.CsvProductManagement.Services.CsvReader..ctor(Stream stream)\r\n at Hroc.Plugin.Core.CsvProductManagement.Services.ImportService.ImportStoreProductsFromCsv(Stream stream, Int32 storeContext, Int32 vendorId, Boolean deleteUnmatched)\r\n at Hroc.Plugin.Core.CsvProductManagement.Services.StaticImportFile.GetStoreProductImportMessages(Stream stream, Int32 storeContext, Int32 vendorId, Boolean deleteUnmatched)\r\n at Hroc.Plugin.Core.CsvProductManagement.Tasks.ImportFilesTask.Execute()\r\n at Nop.Services.Tasks.Task.Execute(Boolean throwException) in c:\\Users\\stevesmith\\Documents\\GitHub\\Store\\Libraries\\Nop.Services\\Tasks\\Task.cs:line 83"
現在對於代碼,我盡量讓它盡可能短(盡管它是 MVC,所以准備 101 種不同的方法......
public void Execute() {
if (_importTaskSettings.Enabled) {
var response = HttpWebRequest.Create(_importTaskSettings.ImportFileUrl).GetResponse();
if (response != null)
{
using (var stream = response.GetResponseStream())
{
var messages = StaticImportFile.GetStoreProductImportMessages(stream, 0, 1, true); //error occurs here
_logger.Information(string.Format("Import Store products file task complete. Messages: {0}", string.Join(", ", messages)));
}
}
else {
//_logger.Information('import was called but is null');
}
}
}
此方法用於帶有后台任務的計時器,出於調試目的,當前設置為 30 秒。
public virtual IList<string> ImportStoreProductsFromCsv(Stream stream, int storeContext = 0, int vendorId = 0, bool deleteUnmatched = false) {
// Store always uploads against all stores
storeContext = 0;
using (var s = new CsvReader(stream)) {
// read CSV file stream into List
.....etc etc
}
當然,錯誤位於使用系統 CsvReader 上。
/// <summary>
/// Initialises the reader to work from an existing stream
/// </summary>
/// <param name="stream">Stream</param>
public CsvReader(Stream stream) {
_type = Type.Stream;
Initialise(stream, Encoding.Default);
}
好的,以上是實際代碼的一小段,深深植根於 nop commerce 和自定義插件。 但是我相信問題出在流過程中的某些地方。
這里的目的是讓服務器上某處的文件每小時更新一次。 管理員無權訪問該文件,但產品也需要更新。 作為這樣一個任務運行,找到路徑創建一個流,並使用 nop commerce 的方法將文件數據輸入到商店網站。
現在閱讀很多論壇,大多數人都說這是在嘗試尋找的同時嘗試從開放流中寫作的行為?(不確定措辭是否正確)從這里開始,實際過程中嘗試尋找是沒有意義的溪流?
我希望我的錯誤很小,我不想再創建一個新的插件並擴展 Nop 了。
如果您需要更多信息,請告訴我並更新此問題。
ResponseStream
確實不支持 Seek 操作。 您可以通過查看CanSeek
屬性來驗證這一點。
一個簡單的解決方案:
using (var stream1 = response.GetResponseStream())
using (var stream2 = new MemoryStream())
{
stream1.CopyTo(stream2);
var messages = StaticImportFile.GetStoreProductImportMessages(stream2, 0, 1, true);
}
但這不適用於非常大的流(例如 100 MB 及以上)
嘿,謝謝你的帖子,它拯救了我的一天!! 我正在使用 CSOM 和 EPPlus 從 SharePoint Office 365 文檔庫中讀取 Excel 文件。 EPPlus 不支持從 SharePoint 門戶讀取 Excel 文件,使用上述技巧我將 FileInfo Stream 轉換為 EPPlus 支持的 MemoryStream 完美無瑕.. 下面是我的代碼
Stream stream1 = spFileInfo.Stream;
using (var stream2 = new MemoryStream())
{
stream1.CopyTo(stream2);
ExcelPackage xlPackage = new ExcelPackage(stream2);
ExcelWorkbook workbook = xlPackage.Workbook;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.