In my ASP.NET MVC project I use a custom controller factory that instantiates an Entity Framework-based data repository and passes it to the controller's constructor. The repository object implements IDisposable, but where should I call its Dispose method? The most straightforward approach that comes to mind is to override the controller's Dispose method and do it there, but since the repository was injected into the controller and not created there, disposing it in the controller doesn't seem quite right to me.
What is the correct place to dispose a repository created in a custom controller factory?
432 Views Asked by SlimShaggy At
2
There are 2 best solutions below
0
Miguel Veloso
On
I guess is a little late by now, but you do it in the ReleaseController method of your IControllerFactory. Take a look at this: http://www.jasinskionline.com/technicalwiki/(S(wvw00ibwlzs5na45orv53qyl))/Custom-Controller-Factory-Putting-Controllers-in-an-External-Assembly-ASP-NET-MVC.ashx?AspxAutoDetectCookieSupport=1
Related Questions in ASP.NET-MVC
- I have a problem outputing the roles on the page ListRoles.cshtml
- Dropdown list showing SQLServer2005SQLBrowserUser$DONSERVER instead of Active Directory group name in ASP.NET MVC C#
- Hosting ASP.NET MVC application on IIS web server using Windows 2019 server
- How to display only department fields associated with a selected department in student automation system?
- How to send select input data for form submission?
- Multi level project reference using dll
- How to upload file to Onedrive using ASP.NET MVC?
- ASP.NET MVC web app looping between fields only on some devices
- Is there any automatic job to load AD-groups?
- How to restrict admin js files to download
- Download PDF in ASP.NET MVC application
- How to add bootstrap theme/example into ASP.NET MVC 5?
- Web API works with Windows authentication enabled when consumed via Swagger but throws an unauthorized issue when accessed through web app
- ASP.Net Core 7.0 Web App (Model-View-Controller) ErrorViewModel OnGet OnPost do not get called or executed
- OAuth 2.0 keep getting Authorization has been denied for this request
Related Questions in DEPENDENCY-INJECTION
- How to add logging to an abstract class in php
- Nest.js can't resolve dependencies of the external library's Reflector dependency
- Do we need IoC containers in typescript if ts-mock-imports exists
- Blazor/Razor resolve components using dependency injection
- Access Registed Scoped Services and Transient Services using GetService()
- using state data class alongside ComposeDestinations
- Messing up with conflict between spring jcl and commons-logging.jar
- How to write pytest tests for a FastAPI route involving dependency injection with Pydantic models using Annotated and Depends?
- .NET Core Include Method in Dependency Injection
- Injecting IHubContext into my background service for SignalR in .NET 5
- Issue with service method call in .NET Background Service , Issue with Scope
- How can I inject the prisma io module using inversify in my node.js project?
- Angular service injection hierarchy
- Trouble with "dotnet run seeddata" when trying to seed a database with EntityFrameworkCore. Specifically with ASP.NET Core 6 Web API
- NX Angular unit tests fail because of NullInjectorError
Related Questions in IDISPOSABLE
- Chained calls may cause memory leaks?
- Why do we need the finalizer in the disposable pattern if it is not guaranteed that it will be called by the garbage collector?
- C# IDisposable pattern - private disposed flag
- Return an IDisposable helper object for cleaning up
- How to properly handle a list of IDisposable items in my Dispose()?
- IDisposable not [ComVisible(true)] in .NET 6?
- C# .NET Core - Will a disposable persist across a try/catch block when declared outside? Is this poor practice?
- Will a parent object that is no longer referenced be reclaimed through GC if the parent object itself holds a live reference?
- IDisposable and/or IAsyncDisposable pattern for IDisposable T in Task<T>
- How do you correctly implement IDispose and IAsyncDispose in a class that contains multiple disposable objects
- Do I need to dispose of a handler on a static class?
- How to properly manage an IAsyncEnumerable that returns IDisposables
- CA2000 When returning the disposable object (using a try/finally block)
- Do you need to make your current class IDisposable if using an IDisposable field in a using statement?
- C# Dependency between classes and IDisposable
Related Questions in CONTROLLER-FACTORY
- Controllers created by System.Web.Mvc.IControllerFactory.CreateController are not disposed
- Issue with passing constructor parameter via controller factory
- ASP.NET Core 3 - Overriding the default ControllerFactory on custom, how save it call in else cases?
- Modify ControllerContext in CreateController method of DefaultControllerFactory in asp.net core mvc
- How to create new controller in runtime?
- Seems No Controllers Recognized by my controller factory
- what exactly ObjectFactory is, and, whats is it used for?
- Dependency injection in constructor of controller with using a ControllerFactory in ASP.NET MVC 5
- Implementing Custom Controller Factory involving multiple Assemblies
- Creating a Custom Controller Factory in ASP.NET MVC
- ASP.NET MVC creates controllers which require authentication although the user is unauthenticated which leads to a dependency injection issue
- How do I get Web API / Castle Windsor to recognize a Controller?
- How do I set a custom controller factory in Orchard?
- JavaFX custom controller factory
- Custom ControllerFactory does not replace DefaultControllerFactory when there is a controller collision
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
In your repository, you should use the Entity Framework data contexts within a using statement. This means that after the data access is finished the Dispose method will be called on the context, closing the connection.