-
Notifications
You must be signed in to change notification settings - Fork 0
EdifactParser en
Leksiqq edited this page Apr 6, 2024
·
2 revisions
Attention! This article, as well as this announcement, are automatically translated from Russian.
EDIFACT to XML translator class. When used, must be registered as a service in the dependency injection system. Also expects that the interface Net.Leksi.IStreamFactory is registered.
-
public event InterchangeEventHandler? Interchange;
- called at the beginning and end of the session (see InterchangeEventHandler. -
public event GroupEventHandler? Group;
- called at the beginning and end of a functional group, if any (see GroupEventHandler. -
public event MessageEventHandler? Message;
- called at the beginning and end of a message (see MessageEventHandler.
-
public async Task ParseAsync(EdifactParserOptions options, CancellationToken? cancellationToken)
- starts the translation process from EDIFACT to XML in accordance with theoptions
options (see EdifactParserOptions.
Program.cs
:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSingleton<EdifactParser>();
builder.Services.AddHostedService<EdifactParserService>();
builder.Services.AddKeyedSingleton<IStreamFactory, LocalFileStreamFactory>("file");
IHost host = builder.Build();
await host.RunAsync();
EdifactParserService.cs
:
...
public class EdifactParserService(IServiceProvider services): BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
try
{
EdifactParser parser = services.GetRequiredService<EdifactParser>();
parser.Interchange += Parser_Interchange;
parser.Group += Parser_Group;
parser.Message += Parser_Message;
EditParserOptions options = new()
{
...
};
while(!cancellationToken.IsCancellationRequested){
options.Input = ...;
await parser.ParseAsync(options, cancellationToken);
options.Input.Close();
}
}
finally
{
await services.GetRequiredService<IHost>().StopAsync(cancellationToken);
}
}
...
}