web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Pass System.IO.Stream to BinData

(0) ShareShare
ReportReport
Posted on by 1,215

Hi,

Greetings!

I am getting a System.IO.Stream object stored in Azure storage and in another method I have uploaded it to another location, which means the file get works fine.

Now I have to send a base64Encode of BinData object. I tried the following code but it fails at Binary binaryStream = Binary::constructFromMemoryStream(stream);

System.IO.Stream stream = //get file stream;

if(stream.CanSeek)
{
    stream.Seek(0, System.IO.SeekOrigin::Begin);
}
Binary binaryStream = Binary::constructFromMemoryStream(stream);
binData = new BinData();

container conBinData = binaryStream.getContainer();

binData.setData(conBinData);

container data = [binData.base64Encode()];

With best regards,

Abhinay

I have the same question (0)
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Please tell us more about the problem than just "it fails". I guess you get a runtime exception - what is its type and message?

    It the stream really a MemoryStream?

    Also, do you really must use BinData? For example, is using System.Convert::ToBase64String() impossible in your case?

  • Abhinay Tharwal Profile Picture
    1,215 on at

    Hi Martin,

    Thank you for your reply.

    The error message does not say much

    Exception thrown: 'System.ArgumentException' in Microsoft.Dynamics.AX.Xpp.Support.dll
    
    {"Exception has been thrown by the target of an invocation."}

    I tried this code too:

    System.IO.MemoryStream memoryStream = stream as System.IO.MemoryStream;
    Binary binaryStream = Binary::constructFromMemoryStream(memoryStream);

    //BR, Abhinay

  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    You get more from the exception, e.g. checking inner exception and stack traces.

    The "as" operator won't magically convert object from one to another. Also, you said you tried the code, but you forgot to tell us what happened. My guess is that you get an empty value in memoryStream variables. If so, it confirms my suspicion that it's not a  MemoryStream instance, and it explain why the whole thing can't work.

  • Verified answer
    Abhinay Tharwal Profile Picture
    1,215 on at

    Hi Martin,

    Thank you for your suggestion.

    The line I had to add was:

    stream.CopyTo(memoryStream);

    So, the code would be:

    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
    stream.CopyTo(memoryStream);
    Binary binaryStream = Binary::constructFromMemoryStream(memoryStream);

    //BR, Abhinay

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans