/// <summary> /// Generate and write EDI document to a stream async /// </summary> public static async void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the invoice var invoice = EF_X12_004010_810_Builder.BuildInvoice("1"); using (var stream = new MemoryStream()) { using (var writer = new X12Writer(stream)) { // 2. Begin with ISA segment await writer.WriteAsync(SegmentBuilders.BuildIsa("1")); // 3. Follow up with GS segment await writer.WriteAsync(SegmentBuilders.BuildGs("1")); // 4. Then write the invoice(s) await writer.WriteAsync(invoice); } Debug.Write(stream.LoadToString()); } }
public static async Task <string> CreateTransaction(EdiMessage message, string controlNumber, string senderId = "SENDER1", string receiverId = "RECEIVER1", bool ackRequested = true, string testIndicator = "T") { //var sb = new StringBuilder(); using (var stream = new MemoryStream()) { using (var writer = new X12Writer(stream, new X12WriterSettings(Separators.X12))) { var isa = BuildIsa(controlNumber, senderId, "14", receiverId, "16", ackRequested ? "1" : "0", testIndicator); // construct the interchange header ... await writer.WriteAsync(isa); var gs = BuildGs(controlNumber, senderId, receiverId); await writer.WriteAsync(gs); await writer.WriteAsync(message); } stream.Position = 0; StreamReader reader = new StreamReader(stream); string text = reader.ReadToEnd(); return(text); } }
/// <summary> /// Generate and write EDI document to a stream async /// </summary> public static async void WriteSingleInvoiceToStreamAsync() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the invoice var invoice = X12TransactionBuilders.BuildInvoice("1"); // 2. Validate it by skipping trailer validation MessageErrorContext errorContext; if (invoice.IsValid(out errorContext, new ValidationSettings { SkipTrailerValidation = true })) { Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name, errorContext.ControlNumber); using (var stream = new MemoryStream()) { using (var writer = new X12Writer(stream)) { // 3. Begin with ISA segment await writer.WriteAsync(SegmentBuilders.BuildIsa("1")); // 4. Follow up with GS segment await writer.WriteAsync(SegmentBuilders.BuildGs("1")); // 5. Then write the invoice(s) await writer.WriteAsync(invoice); } Debug.Write(stream.LoadToString()); } } else { // The invoice is invalid Debug.WriteLine("Message {0} with control number {1} is invalid with errors:", errorContext.Name, errorContext.ControlNumber); // List all error messages var errors = errorContext.Flatten(); foreach (var error in errors) { Debug.WriteLine(error); } } }