• Version 1.16.0
Show / Hide Table of Contents

Scheduler For Sell Side Windows Service Sample Project

This sample illustrates how to run SellSide Fix session under Scheduler as a hosted service.

Please pay attention that a Windows service app returns the C:\WINDOWS\system32 folder as its current directory, so absolute paths for EngineSettings.LicenseStore, EngineSettings.StorageDirectory and EngineSettings.Dictionary should be used.

© Onix Solutions

Source code


using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using NLog.Extensions.Logging;
using OnixS.Fix;
using OnixS.Fix.Scheduling;
#if NET5_0_OR_GREATER
using System.Runtime.Versioning;
#endif

namespace SchedulerForSellSideWindowsService
{

#if NET5_0_OR_GREATER
    [SupportedOSPlatform("windows")]
#endif
    public class SchedulerForSellSideService : BackgroundService
    {
        Scheduler scheduler;
        Session acceptor;

        static void ReportEvent(string msg)
        {
            Console.WriteLine("SchedulerForSellSideServiceSample: " +  msg);
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            if (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    EngineSettings settings = new()
                    {
                        SendLogoutOnException = true,
                        SendLogoutOnInvalidLogon = true, // E.g. to send a Logout when the sequence number of the incoming Logon (A) message is less than expected.
                        ListenPorts = { 10450 },
                        StorageDirectory = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "MsgStorage"),
                        LicenseStore = AppDomain.CurrentDomain.BaseDirectory,
                        LoggerProvider = new NLogLoggerProvider()
                    };

                    Engine.Init(settings);

                    const string SenderCompID = "SellSide";
                    const string TargetCompID = "BuySide";

                    acceptor = new Session(SenderCompID, TargetCompID, ProtocolVersion.Fix44, true, SessionStorageType.FileBasedStorage);

                    acceptor.Error += (_, args) => {ReportEvent("Session Error: " + args); };

                    acceptor.Warning += (_, args) => { ReportEvent("Session Warning: " + args ); };

                    acceptor.StateChanged += (_, e) => { ReportEvent("New session state: " + e.NewState); };

                    SessionSchedule schedule =
                        new(
                            DayOfWeek.Sunday,
                            DayOfWeek.Saturday,
                            new TimeSpan(0, 0, 0),
                            new TimeSpan(23, 59, 59),
                            SessionDuration.Day,
                            SequenceNumberResetPolicy.Never);


                    scheduler = new Scheduler();

                    scheduler.Error += (_, args) => {
                        ReportEvent($"Scheduler reported error for session {args.Session}: {args.Reason}");
                    };

                    scheduler.Register(acceptor, schedule, new AcceptorConnectionSettings());
                }
                catch (Exception ex)
                {
                    ReportEvent(ex.ToString());
                }
            }
            return base.StartAsync(cancellationToken);
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
            try
            {
                scheduler.Unregister(acceptor);

                if (acceptor != null)
                {
                    acceptor.Dispose();
                    acceptor = null;
                }

                if (scheduler != null)
                {
                    scheduler.Dispose();
                    scheduler = null;
                }

                if (Engine.IsInitialized)
                    Engine.Shutdown();
            }
            catch (Exception ex)
            {
                ReportEvent(ex.ToString());
            }
            return base.StopAsync(cancellationToken);
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            return Task.CompletedTask;
        }
    }
}

Host Run


using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
#if NET5_0_OR_GREATER
using System.Runtime.Versioning;
#endif

namespace SchedulerForSellSideWindowsService
{
#if NET5_0_OR_GREATER
    [SupportedOSPlatform("windows")]
#endif
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using IHost host = Host.CreateDefaultBuilder(args)
                .UseWindowsService(options =>
                {
                    options.ServiceName = ".NET Scheduler For SellSide Service";
                })
                .ConfigureServices(services =>
                {
                    services.AddHostedService<SchedulerForSellSideService>();
                })
                .Build();

            host.RunAsync().Wait();
        }
    }
}

In this article
Back to top Copyright © Onix Solutions.
Generated by DocFX