"No interchange was started." error on serializing EdiMessage

126 views Asked by At

Using code like:

...
EdiMessage ediMessage = (EdiMessage)instance;
using (FileStream ediStream = File.OpenWrite(file))
{
    using (EdifactWriter writer = new EdifactWriter(ediStream))
    {
       writer.Write(ediMessage);
    }
}

I receive an error message 'No interchange was started.'

Stack trace below the writer.Write call (last public version of Edifabric):

at EdiFabric.Framework.Writers.EdiWriter`2.Write(EdiMessage message)

It concerns a D96A INVOIC object with BGM, some FTX and a UNH. The object class is . And the exception is raised by EdiWriter on the call writer.Write.

The ediMessage contents seem valid. According to Edifabric documentation it should add the default EDIFACT separators automatically:

If not explicitly specified the writer will use the default separators per standard:

What did I forget to setup?

1

There are 1 answers

2
Guido Leenders On

It seems a documentation bug; the defaulting does not happen. When changing the code to:

using (EdifactWriter writer = new EdifactWriter(ediStream))
{
  var unb = new UNB(); // Begin interchange.
  writer.Write(unb, Separators.Edifact);

  writer.Write(ediMessage);
}

the unb inserts a header in the document after which in this case one message is added. The code now raised an "Object reference not set to an instance of an object." with call stack

at EdiFabric.Core.Model.Edi.EdiMessage.GetControlNumber(String tag, Int32 position)

which was fixed by adding:

ediMessage.ControlNumber = ...;