Document.Save throws exception when used in Parallel.Foreach using .NET

Hi,
I'm having a problem when using Document.Save to a memory stream in parallel.
I get the following exception:

System.ArgumentOutOfRangeException was unhandled by user code
Message=Specified argument was out of the range of valid values.
Parameter name: index
Source=Aspose.Words
ParamName=index
StackTrace:
at x6c95d9cf46ff5f25.x09ce2c02826e31a6.x7121e9e177952651(Int32 xc0c4c459c6ccbd00)
at x6c95d9cf46ff5f25.x09ce2c02826e31a6.x52b190e626f65140(Int32 xba08ce632055a1d9)
at xa604c4d210ae0581.x3af03f5f12c5ee73.WriteCore()
at xa604c4d210ae0581.x485adbf5506556e8.x6210059f049f0d48(x7f77ea92be0d9042 x94aec03cf2ae750b)
at xa604c4d210ae0581.x3af03f5f12c5ee73.x6210059f049f0d48(xeedad81aaed42a76 x789564759d365819, xa52f2632c0ffdfaf xbd40d7ce3aca91e3, Boolean x0463a6b206bbf7e4)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x811fbd3fde91eb05(String xb41faee6912a2313, xeedad81aaed42a76 x789564759d365819, xa52f2632c0ffdfaf xe08a26cc2b49f3aa)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x544c864b2d73388d(FieldType x77ce91e5324df734, xeedad81aaed42a76 x789564759d365819)
at x9e260ffa1ac41ffa.x8b8ab0cf32b35f3c.x5907707870dfe8ca(Shape x5770cdefd8931aa9)
at x9e260ffa1ac41ffa.x8b8ab0cf32b35f3c.x20db457a8342c28f(Shape x5770cdefd8931aa9)
at x9e260ffa1ac41ffa.x8b8ab0cf32b35f3c.x84e71ff4edf2ec72(ShapeBase x5770cdefd8931aa9)
at x9e260ffa1ac41ffa.x8b8ab0cf32b35f3c.x1a2622a1866b8f97(ShapeBase x5770cdefd8931aa9)
at x9e260ffa1ac41ffa.x8b8ab0cf32b35f3c.x1f70c910ab814928(Shape x5770cdefd8931aa9)
at x9e260ffa1ac41ffa.x163859bfa28558c4.VisitShapeStart(Shape shape)
at Aspose.Words.Drawing.Shape.x2449520719b1e37e(DocumentVisitor x672ff13faf031f3d)
at Aspose.Words.CompositeNode.xf7ae36cd24e0b11c(DocumentVisitor x672ff13faf031f3d)
at Aspose.Words.Drawing.Shape.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.x464d2134480a7bf2(DocumentVisitor x672ff13faf031f3d)
at Aspose.Words.CompositeNode.xf7ae36cd24e0b11c(DocumentVisitor x672ff13faf031f3d)
at Aspose.Words.Paragraph.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.x464d2134480a7bf2(DocumentVisitor x672ff13faf031f3d)
at Aspose.Words.CompositeNode.xf7ae36cd24e0b11c(DocumentVisitor x672ff13faf031f3d)
at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x51ee56decc29a9da(Section xb32f8dd719a105db)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x160a0bf4de8f6bd0()
at x9e260ffa1ac41ffa.x163859bfa28558c4.x8cac5adfe79bc025(x8556eed81191af11 x5ac1382edb7bf2c2)
at Aspose.Words.Document.xf381a641001e6830(Stream xcf18e5243f8d5fd3, String xafe2f3653ee64ebc, SaveOptions xc27f01f21f67608c)
at Aspose.Words.Document.Save(Stream stream, SaveOptions saveOptions)
at Aspose.Words.Document.Save(Stream stream, SaveFormat saveFormat)
at r2AsposeProxy.AsposeProxy.SaveAsDoc(Stream strm) in C:\tfs\PreviousVersions\Sprint-112.2\source\r2AsposeProxy\AsposeProxy.cs:line 78
at r2MappingFunctions.MappingFunctions.GenerateCCedReports.<>c__DisplayClass4.b__0(String cc)
at System.Threading.Tasks.Parallel.<>c__DisplayClass21`2.b__17(Int32 i)
at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.b__c()
InnerException:


I googled it and found a threads with the same problem:
which leads to the most recent one
although I googled "entry279263" that appears in the link's end, and found it's version 9.6
I'm using version 10.1.1.0

the code i'm running is basically doing this:

--------------------------------

byte[] btOriginal; //contains the original file
string[] arrStrs; //string array of about 8 strings with 4-5 rows each

ConcurrentBag bag64 = new ConcurrentBag();

Parallel.Foreach(arrStrs, str =>
{
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(ms);
}

doc.Range.Bookmarks["change"].Text = str;

using (MemoryStream ms = new MemoryStream())
{
doc.Save(ms, SaveFormat.Doc);
bag64.Add(Convert.ToBase64String(ms.ToArray()))
}
}

--------------------------------

Please help me out
thanks,
Elad

Hi,

I’ve tried it with version 11.2.0.0 and got the same error.
Have you succeed producing it in your side?
please update me ASAP
thanks,
Elad

Hi,

Is there any update?
This issue really holds me back and I need a fast solution.
Thanks,
Elad.

Hello,

Can you please update?
I’m really short it time, it is urgent.
please reply ASAP.
Thanks,
Elad

Hi Elad,

Thanks for your query. Please use the latest version of Aspose.Words for .NET. If you still face problem then please share your documents for investigation purposes. Also, please share the values of (string[] arrStrs).

Hello,

I tried using the latest release and it still happens.
I’m using the following code to reproduce it in my tester.
The document used is attached to this message.
you may need to run it few times. you can increase the number of strings to raise the chance of the exception.
Please provide a solution as quickly as you can.
Thanks,
Elad.

byte[] btOriginal = File.ReadAllBytes(@“C:\exception.doc”);

string[] arrStrs = { " person1\r\nperson1\r\naddress1\r\naddress11\r\ncity1 prov1 11111\r\ncountry1\r\n ",
" person2\r\nperson2\r\naddress2\r\naddress22\r\ncity2 prov2 22222\r\ncountry2\r\n ",
" person3\r\nperson3\r\naddress3\r\naddress33\r\ncity3 prov3 33333\r\ncountry3\r\n ",
" person4\r\nperson4\r\naddress4\r\naddress44\r\ncity4 prov4 44444\r\ncountry4\r\n ",
" person5\r\nperson5\r\naddress5\r\naddress55\r\ncity5 prov5 55555\r\ncountry5\r\n ",
" person6\r\nperson6\r\naddress6\r\naddress66\r\ncity6 prov6 66666\r\ncountry6\r\n ",
" person7\r\nperson7\r\naddress7\r\naddress77\r\ncity7 prov7 77777\r\ncountry7\r\n ",
" person8\r\nperson8\r\naddress8\r\naddress88\r\ncity8 prov8 88888\r\ncountry8\r\n "};

ConcurrentBag bag64 = new ConcurrentBag();

Parallel.ForEach(arrStrs, str =>
{
Document doc;
using (MemoryStream msread = new MemoryStream(btOriginal))
{
doc = new Document(msread);
}

doc.Range.Bookmarks[“START172633868072829375000F”].Text = str;

using (MemoryStream mswrite = new MemoryStream())
{
doc.Save(mswrite, SaveFormat.Doc);
bag64.Add(Convert.ToBase64String(mswrite.ToArray()));
}

});

Hi Elad,

Thanks for sharing the document. It would be great, If you share what exactly you want to do by using Aspose.Words. We will provide you a workaround or solution. Can you please share the purpose of Parallel.ForEach?

Hi,


As you can see, I’m creating a copy of the document for each of the strings (representing an address of a person), setting it in the bookmark and saves the document.
Afterwards I’m converting all the documents to base64 strings and pass them to another component that will print all of them.

Of course I can do it in a normal foreach, but the Parallel really speeds up the process since it’s running on a multicore machine.

Is there a workaround/solution that works in parallel?

Thanks,
Elad.

Hi Elad,

Thanks for sharing the information. I have executed the same code many times and have not faced the exception with each execution. However, I have faced the same exception but not with each execution.

I have logged this issue in our issue tracking system. Our development team will look into this issue and you will be updated via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Tahir,

can you please update me?

Is there a workaround for this issue?

If not, I need to know how much time will it take untill this issue will be resolved.

I need to overcome it in version 10.1.1.0 that is used by our product.

Thanks,

Elad

Hi Elad,

I have verified our issue tracking system like to share with you that this issue is pending for analysis. Once analysis is completed then we will be in the position to share any ETA with you.

For a workaround Please use for/while loop instead of Parallel.ForEach.

We apologize for your inconvenience.

Hi Elad,


Thanks for your inquiry.

Does the same issue occur if you use the traditonal technique for multithreading? For an example of what I mean my traditional multi threading please see the code here: https://forum.aspose.com/t/61352

If the issue does not occur with this code then you can use this as a work around for the time being.

Thanks,

Hi Adam,

Yes, I tried it out and got the same exception.

Please let me know if any progress is made.

Thanks,

Elad

Hi Elad,

We are sorry for your inconvenience. We will update you via this forum thread once there is any progress on logged issue.

We appreciate your patience.

The issues you have found earlier (filed as WORDSNET-6293) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Was this Bug solved ?

@Gautham_Hegde,

Thanks for your inquiry. The issue reported in this forum thread was resolved. If you are facing any issue while using Aspose.Words, please let us know.