Search code examples
webformsdryioc

DryIOC scoped services in ASP.NET WebForms


We have an ASP.Net WebForms application and recently in a refactoring scenario, we added DryIOC as an IOC container to it. The sample WebForms project : Link to the test project and a piece of The code is as follows:

public class ApplicationContainer
    {
        public static void Initialize()
        {
            Container = new Container();
            Container.Register<ISingletonService, SingletonService>(reuse: Reuse.Singleton);
            Container.Register<ITransienService, TransienService>(reuse: Reuse.Transient);
            Container.Register<IScopeService, ScopeService>(reuse: Reuse.Scoped);

            _serviceProvider = Container.OpenScope();
        }

        public static Container Container;
        private static IServiceProvider _serviceProvider;
        public static IServiceProvider ServiceProvider { get { return _serviceProvider; } }
    }


public class Global : HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            InitializeApplicationContainer();
        }

        internal static void InitializeApplicationContainer()
        {
            ApplicationContainer.Initialize();
        }

The problem is that request scopes are not known by DryIOC. For example, in different requests that are sent from the browser to the web application hosted on IIS or IIS Express, for Reuse.Scoped registered services, no new instance is created and the first instance injected is reused on every request. There is another problem with disposable Transients. We created a UnitOfWork that initiates a TransactionScope. It is injected by using an IUnitOfWork interface and should be disposed properly. But we have not found the correct way to use it. and on the other hand, when it is injected into Scoped services, its life time gets Scoped, not Transient. After Googling, I have found a web page about using DryIOC in a WebForms application that is targeted .Net Framework 4.7.2. We use .Net Framework 4.8 (recently migrated our projects to this framework version). Thank you in advance for your useful comments and contributions. Best regards

I have created a test WebForms application and copied the codes related to initialize and registeration of three sample services to test Singleton, Scoped and Transient scopes. The resolved instance of the Reuse.Scoped did not change in different requests that I made to one of the pages in the application, and even in different pages (Default and Contact pages).


Solution

  • I see that you do not use the DryIoc.Web.dll package. You may consider using it.

    I do not have a specific WebForms example to point to, but you may look into the implemented abstractions (on fuget), including the source code. Plus, look into the DryIoc.Web tests in the DryIoc repo https://github.com/dadhi/DryIoc/blob/master/test/DryIoc.Web.UnitTests/DryIocWebTests.cs