ALAppExtensions icon indicating copy to clipboard operation
ALAppExtensions copied to clipboard

[Event Issue] Codeunit ReportManagement - OnAfterDocumentReady DocumentStream: InStream can only be read by a single extension

Open rvanbekkum opened this issue 4 years ago • 12 comments

Dear ALAppExtensions team,

I was asked to file an issue here on GitHub for tracking by @nhsejth. Problem is that the InStream parameter from the OnAfterDocumentReady event can only be read by the first subscriber. Subsequent event subscriber will be left with the parameter being set to the end of the stream (EOS = true). It would be good if multiple subscribers (multiple apps, from multiple ISVs) can read the document contents.

Originally discussed on Yammer:

I found what the problem is. Multiple apps can subscribe OnAfterDocumentReady event and read the InStream. If one of the apps has read the InStream, then none of the other apps can read the PDF contained in the InStream anymore.

This already appears to form a problem for our own apps, but more so, if you combine apps from multiple ISVs that all try to use the OnAfterDocumentReady event.

Do you know if there is a way to avoid this problem (is there an option to sort of 'reset' an InStream that I am completely missing) or can this be considered a problem that might need to be resolved in BC itself? 😅

I think changing the InStream to a "Temp Blob" would also resolve the issue, as that would allow ISVs to create an InStream themselves so there are no conflicts.

Reply:

I will have to think about the best solution here. In principle, we should have an automatic reset of the stream when the NST iterates over all subscribers, but I don't know how easy that would be, it could be better to add a Reset method on the stream object that would reset the pointer to the beginning.

Notice that TempBlob is deprecated and replaced by another app codeunit, but it could be an option to feed the data into a blob in the report management codeunit, but I would prefer not to copy data around for performance reasons.

Would it be possible to you to log an issue on GitHub that I can use for tracking?

(Also discussed in the following public thread: https://www.yammer.com/dynamicsnavdev/threads/1885281555636224)

Maybe one possible approach might be to replace the DocumentStream: InStream parameter with a DocumentTempBlob: Codeunit "Temp Blob parameter, although the other suggested approaches ((1) InStream.Reset, or (2) Automatic Reset by NST of InStream event parameter) work as well and preferred if one of them can be realized.

rvanbekkum avatar Aug 24 '22 13:08 rvanbekkum

I don't know if it works correctly. Maybe it's possible to assign the InStream to a DotNetStream - Seek the DotNetStream an reassign this Stream to the InStream.

If it isn't possible to map the seek methods for the AL InStream Objects, using a DotNet Wrapper could be a temporary solution.

pri-kise avatar Aug 24 '22 13:08 pri-kise

@AlexanderYakunin the issue describes an extensibility bug where apps of multiple partners could accidently destroy the function of another app. Therefore there should be maybe added a label that this issue requires triage.

@JesperSchulz , @nhsejth is there any update on this issue?

pri-kise avatar Sep 26 '22 09:09 pri-kise

I'm afraid I cannot provide an update on this issue. @nhsejth will give a reply, when he finds a minute to do so.

JesperSchulz avatar Sep 28 '22 12:09 JesperSchulz

Hi @JesperSchulz , do you have any updates on this?

JulianSchmidtke avatar Jan 05 '23 10:01 JulianSchmidtke

I have a potential workaround (but lack full validation) that can be tested.

In the subscriber procedure, you can create a local instream variable that gets initialized from the parameter stream. That will give two stream variables pointing to the same data, but with different pointers internally (shared stream). By using the local variable instead of the parameter, you should leave the parameter unmodified.

nhsejth avatar Jan 05 '23 11:01 nhsejth

@nhsejth thanks for your feedback. Maybe you can give then some small example code, how we can patch our solution as temporary workaround. I'm not quite sure how I can initiliaze my local InStream from the parameter stream. Are stream assignable and does this lead to a copy of the original stream?

pri-kise avatar Jan 05 '23 12:01 pri-kise

This works for us

  begin        
      TempBlob.CreateOutStream(outs);
      CopyStream(outs, DocumentStream);

      // Refill the InStream
      TempBlob.CreateInStream(DocumentStream);
  end;

JulianSchmidtke avatar Jan 05 '23 13:01 JulianSchmidtke

Some additional input: The code suggested by Julian will nicely reset the InStream for the next user. We have applied this as well (so that we don't bother any other parties due to us reading the InStream).

But, what if you have to deal with another (unknown) third-party extension that comes before yours and it reads the InStream already (so that your extension cannot read anything anymore)? To take this into account as well, we have also added something in our system library which checks if the InStream is at the end of the stream (EOS), and if so, attempts to reset the InStream (we are doing this by using the DotNet-wrapper codeunits).

However, both of these measures** are work-arounds for the actual (major) issue/bug that is reported in this GitHub issue.


** The work-around measures being:

  1. make sure other extensions that come after you can read the InStream again, and
  2. take into account other extensions that already read the InStream (and that did not take measure 1 into consideration) and reset the InStream yourself.

rvanbekkum avatar Jan 05 '23 15:01 rvanbekkum

We're looking into finding a solution for this problem for 2023 Wave 1. Fingers crossed.

JesperSchulz avatar Jan 17 '23 13:01 JesperSchulz

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. Please do not reply to this, as we do not monitor closed issues. If you have follow-up questions or requests, please create a new issue where you reference this one.

Build ID: .

JesperSchulz avatar Jan 24 '23 12:01 JesperSchulz

Referencing @nhsejth solution coming with next major: https://github.com/microsoft/ALAppExtensions/issues/11846#issuecomment-1401960427

pri-kise avatar Jan 24 '23 21:01 pri-kise

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. Please do not reply to this, as we do not monitor closed issues. If you have follow-up questions or requests, please create a new issue where you reference this one.

Build ID: 22.0.52767.0.

JesperSchulz avatar Feb 03 '23 13:02 JesperSchulz