How to use MediatR with Unity in legacy ASP.NET 4.7.2?

183 views Asked by At

I am working on a legacy ASP.NET application with target framework .NET 4.7.2. There is already a Unity container implemented (using Microsoft.AspNet.WebFormsDependencyInjection.Unity v1.0.0). Now, I want to use Mediatr with the same unity container implementation. Is this feasible?

The following NuGet packages are used:

Here is the code to register dependency with unity container

public class Global : System.Web.HttpApplication
{
    public void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        IdentityModelEventSource.ShowPII = true;

        // Code for dependency injection
        var diContainer = this.AddUnity();
        diContainer.RegisterType<MyServices>(new TransientLifetimeManager());
        diContainer.RegisterType<IUnitOfWork, UnitOfWork>(new TransientLifetimeManager());
        diContainer.RegisterType<MapperConfig>(new ContainerControlledLifetimeManager());

        // Services
        diContainer.RegisterType<DashboardService>(new TransientLifetimeManager());
        diContainer.RegisterType<UserVisitsService>(new TransientLifetimeManager());

        diContainer.RegisterType<MyBasePage>(new InjectionConstructor(diContainer.Resolve<MyServices>()));
        diContainer.RegisterType<UnifiedSite>(new InjectionConstructor(typeof(MyServices), typeof(NavigationService)));

        // Web API controllers
        diContainer.RegisterType<Controllers.DashboardController>(new InjectionConstructor(diContainer.Resolve<DashboardService>()));
        diContainer.RegisterType<Controllers.FeedbackController>(new InjectionConstructor(diContainer.Resolve<IUnitOfWork>()));
        diContainer.RegisterType<Controllers.UserVisitsController>(new InjectionConstructor(diContainer.Resolve<UserVisitsService>()));

        GlobalConfiguration.Configuration.DependencyResolver = new UnityIoCContainer(diContainer);
    }
}

Please guide me if anyone has used MediatR with .NET 4.7.2. If you can share a Github repository for sample code it will really help.

0

There are 0 answers