How To Use Autofac In Acumatica With Global Graph And Single Registration

How to use Autofac in Acumatica with global graph and single registration

Hello everybody,

today I want to leave a short notice on how to use Autofac in Acumatica, but with single registration. In this article I've descirbed how to use Autofac for resolving interface < -- > class implementation. 

But one of my collegues Deebhan Hari pointed that with my usage registration of classes will happen on each roundtrip and for each graph leading to potential memory leack.

Definetely not something, we would like to have. After small conversation, we managed to add to Autofac singleton, which allowed us to have situation when only once per lifetime of the process Autofac 

registers class only once.

 

 

using System;

using PX.Data;

using Autofac;

 

namespace SingleRegistrationDemo

{

       public class AllGraphsExtension : PXGraphExtension<PXGraph>

       {

         public override void Initialize()

         {

             // Implemented Singleton, so that our KNCustomCaseCommonEmailProcessor is registered only once when needed.

             var single = SingletonCustomCaseCommon.Instance;

         }

       }

 

    public sealed class SingletonCustomCaseCommon

    {

        private static readonly Lazy<SingletonCustomCaseCommon>

            lazy =

            new Lazy<SingletonCustomCaseCommon>

                (() => new SingletonCustomCaseCommon());

 

        public static SingletonCustomCaseCommon Instance { get { return lazy.Value; } }

 

        private SingletonKNCustomCaseCommon()

        {

            PX.Objects.EP.EmailProcessorManager.Register(new ClosedCasesReOpenV2.CustomCaseCommonEmailProcessor());

        }

    }

}

 

Comments are closed