Question

Using C# ASP.net 4.5, Nopcommerce CMS, MVC 4 and visual studio 2012.

This seems to be a familiar problem people have, I,ve searched the "googles" and have yet to find a solution fitting my need.

please note, this is not my code, I was asked to fix this issue (I also have limited knowledge on file I/O, just the basics).

The error stack trace is:

"   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"

now for the code, ill try to keep it as short as possible (its is MVC though so prepare for 101 different methods...

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');
            }
        }
    }

this methods gets used on a timer withing a background task, for debugging purposes its currently set to 30 seconds.

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
}

and of course the error is located on the using system 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);
    }

Ok, so the above is a small snippet of the actual code rooted deep with nop commerce and custom plugins. however i believe the fault lies some where in the process of the stream.

The purpose here is that a file living somewhere on the server gets updated every hour. The Admins do not have access to the file, but there products also need updating. As such a task runs which find the path creates a stream, and uses nop commerce's methods to input the file data into the stores website.

Now reading a lot of forums most people say its the act of trying to write from the open stream whilst trying to seek?(not sure if worded that correctly) And from this the actual process doesn't make sense of trying to seek in the stream?

I'm hoping my error is something small, I do not wish to have to create a new plugin and extend of Nop anymore than i need too.

If you require any more information, let me know and ill update this question.

Was it helpful?

Solution

A ResponseStream does indeed not support Seek operations. You can verify this by looking at the CanSeek property.

A simple solution:

using (var stream1 = response.GetResponseStream())
using (var stream2 = new MemoryStream())
{
    stream1.CopyTo(stream2);
    var messages = StaticImportFile.GetStoreProductImportMessages(stream2, 0, 1, true);
}

But this won't work well for very large streams (eg 100 MB and above)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top