Giter Club home page Giter Club logo

Comments (22)

henkmollema avatar henkmollema commented on September 17, 2024

I add it as a transient using the lambda epxression overload:

services.AddTransient<SingleInstanceFactory>(sp => t => sp.GetService(t));
services.AddTransient<MultiInstanceFactory>(sp => t => sp.GetServices(t));

Where sp is an instance of IServiceProvider, provider by the runtime and t is the type requested by MediatR.

from mediatr.

DanielToft avatar DanielToft commented on September 17, 2024

I am also trying to use dependency injection in mvc 6. I cannot find a good way to register Request and Notification handlers.

It works if I do it manually for every handler like this:

services.AddSingleton<IRequestHandler<CreateUserRequest, CreateUserRequest.Result>, UserHandler>()

How can I register all classes that implement IRequestHandler<,> without adding all manually? And is it possible to register decorators?

from mediatr.

khellang avatar khellang commented on September 17, 2024

You can use something like https://github.com/khellang/Scrutor, a library I wrote to do assembly scanning to register types by convention.

from mediatr.

jmzagorski avatar jmzagorski commented on September 17, 2024

after many hours scratching my head I found this post that helped. However, Scoped lifestyle still did not work properly. Following http://stackoverflow.com/questions/33612609/per-request-scope-with-asp-net-5-and-built-in-di-container, I injected IHttpContextAccessor into the Configure method of the Startup.cs (not sure if that is a good thing to do or not) and set it as a private field in the Startup.cs class that the factories called like so

 .AddTransient<SingleInstanceFactory>(sp => t => context.HttpContext.RequestServices.GetService(t))
 .AddTransient<MultiInstanceFactory>(sp => t => context.HttpContext.RequestServices.GetServices(t))

from mediatr.

khellang avatar khellang commented on September 17, 2024

@jmzagorski That's definitely not a good thing to do. What are you trying to achieve? Why can't you resolve the service from sp?

from mediatr.

jmzagorski avatar jmzagorski commented on September 17, 2024

i've been trying to solve my issue for over a day, can't seem to find it until I did that. My NotificationHandler takes a repository and that repository takes a DbContext. My NotificationHandler is being Transient, but my Repository and DbContext are not being Scoped on subsequent calls from my Angular Controller. My MVC Controller is being called from an Angular Controller through $http.post. That Angular controller does not refresh or redirect after the post, it simply does a $route.reload to refresh the data and stay on the same page. So, for example, if i post from that controller's view the first time it works, but the second call fails because my repository and DbContext are not being newed up again. I pasted snippets below. Do you see the problem?

    public void ConfigureServices(IServiceCollection services)
    {
      #region ASP.net default stuff here

      services.AddMvc(config =>
      {
#if !DEBUG
        config.Filters.Add(new RequireHttpsAttribute());
#endif
      })
      .AddJsonOptions(opt =>
      {
        opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
      });

      #endregion

      #region EF

      services.AddIdentity<CoolRoomUser, IdentityRole>(config =>
      {
        config.User.RequireUniqueEmail = true;
        config.Password.RequiredLength = 8;
        config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
        config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
        {
          OnRedirectToAccessDenied = ctx =>
          {
            if (ctx.Request.Path.StartsWithSegments("/api") &&
                ctx.Response.StatusCode == 200)
            {
              ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            }
            else
            {
              ctx.Response.Redirect(ctx.RedirectUri);
            }

            return Task.FromResult(0);
          }
        };
      })
      .AddEntityFrameworkStores<CoolRoomContext>();

      services.AddLogging();

      services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<CoolRoomContext>();

      #endregion

      #region DI 

      services
        .AddTransient<CoolRoomContextSeedData>()
        .AddScoped<ICoolRoomRepository, CoolRoomRepository>()
        .AddScoped<IAppCache, AppCache>()
        .AddSingleton<IMemoryCache, MemoryCache>()
        .AddSingleton<IMediator, Mediator>()
        .AddTransient<SingleInstanceFactory>(sp => t => context.HttpContext.RequestServices.GetService(t))
        .AddTransient<MultiInstanceFactory>(sp => t => context.HttpContext.RequestServices.GetServices(t))
        .AddTransient<INotificationHandler<Receive.Model>, Receive.ModelHandler>()
        .AddTransient<INotificationHandler<Relocate.Model>, Relocate.ModelHandler>()
        .AddTransient<INotificationHandler<Activation.Model>, Activation.ModelHandler>()
        .AddTransient<IRequestHandler<Receive.Query, IEnumerable<Receive.ReadModel>>, Receive.QueryHandler>()
        .AddTransient<IRequestHandler<Relocate.Query, IEnumerable<Relocate.ReadModel>>, Relocate.QueryHandler>();

#if DEBUG
      services.AddScoped<IMailService, DebugMailService>();
#endif

      #endregion

      services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
    }
// itemLocatorController.js angular controller

(function () {
  "use strict";

  angular.module("app-inventoryitems")
    .controller("itemLocatorController", itemLocatorController);

  var isSuccess = false;

  function itemLocatorController($routeParams, $route, itemRepository, staticItemCache) {

    // initialize vars & model here
    var vm = this;
    vm.itemName = $routeParams.itemName;
    vm.errorMessage = "";
    vm.isGettingLocators = false;
    vm.newLocator = {};
    vm.isBusy = true;

    // Repository simply calls return $http.post(baseUrl + "/" + itemName + "/locators", newLocator);

    vm.moveInventory = function () {
      vm.isBusy = true;
      vm.errorMessage = "";
      itemRepository.addNewLocator(vm.itemName, vm.newLocator)
        .then(function (response) {
          vm.newLocator = {};
          isSuccess = true;
          $route.reload(); // refresh the data since business logic is complex
        }, function (error) {
          vm.errorMessage = "Failed to save new item.";
        })
        .finally(function () {
          vm.isBusy = false;
        })
    }
  }

})();
    public class ModelHandler : INotificationHandler<Model>
    {
      // not being scoped on second call
      private readonly ICoolRoomRepository repository;
      private readonly IAppCache cache;

      public ModelHandler(ICoolRoomRepository repository, IAppCache cache)
      {
        this.repository = repository;
        this.cache = cache;
      }

      public void Handle(Model notification)
      {
        // handle
      }
    }

from mediatr.

khellang avatar khellang commented on September 17, 2024

What happens if you change all those to AddScoped?

-.AddTransient<SingleInstanceFactory>(sp => t => context.HttpContext.RequestServices.GetService(t))
+.AddScoped<SingleInstanceFactory>(sp => t => sp.GetService(t))
-.AddTransient<MultiInstanceFactory>(sp => t => context.HttpContext.RequestServices.GetServices(t))
+.AddScoped<MultiInstanceFactory>(sp => t => sp .GetServices(t))
-.AddTransient<INotificationHandler<Receive.Model>, Receive.ModelHandler>()
+.AddScoped<INotificationHandler<Receive.Model>, Receive.ModelHandler>()
-.AddTransient<INotificationHandler<Relocate.Model>, Relocate.ModelHandler>()
+.AddScoped<INotificationHandler<Relocate.Model>, Relocate.ModelHandler>()
-.AddTransient<INotificationHandler<Activation.Model>, Activation.ModelHandler>()
+.AddScoped<INotificationHandler<Activation.Model>, Activation.ModelHandler>()
-.AddTransient<IRequestHandler<Receive.Query, IEnumerable<Receive.ReadModel>>, Receive.QueryHandler>()
+.AddScoped<IRequestHandler<Receive.Query, IEnumerable<Receive.ReadModel>>, Receive.QueryHandler>()
-.AddTransient<IRequestHandler<Relocate.Query, IEnumerable<Relocate.ReadModel>>, Relocate.QueryHandler>();
+.AddScoped<IRequestHandler<Relocate.Query, IEnumerable<Relocate.ReadModel>>, Relocate.QueryHandler>();

If you have services accepting scoped dependencies, you should make sure that those are registered as scoped as well, otherwise you'd end up with lifetime mismatches.

from mediatr.

jmzagorski avatar jmzagorski commented on September 17, 2024

that did not work as well. Now my NotificationHandler was not newed up on my second call. It went right to the Handle method.

      services
        .AddTransient<CoolRoomContextSeedData>()
        .AddScoped<ICoolRoomRepository, CoolRoomRepository>()
        .AddScoped<IAppCache, AppCache>()
        .AddSingleton<IMemoryCache, MemoryCache>()
        .AddSingleton<IMediator, Mediator>()
        .AddScoped<SingleInstanceFactory>(sp => t => sp.GetService(t))
        .AddScoped<MultiInstanceFactory>(sp => t => sp.GetServices(t))
        .AddScoped<INotificationHandler<Receive.Model>, Receive.ModelHandler>()
        .AddScoped<INotificationHandler<Relocate.Model>, Relocate.ModelHandler>()
        .AddScoped<INotificationHandler<Activation.Model>, Activation.ModelHandler>()
        .AddScoped<IRequestHandler<Receive.Query, IEnumerable<Receive.ReadModel>>, Receive.QueryHandler>()
        .AddScoped<IRequestHandler<Relocate.Query, IEnumerable<Relocate.ReadModel>>, Relocate.QueryHandler>();

from mediatr.

khellang avatar khellang commented on September 17, 2024

IMediator/Mediator must also be scoped. A singleton can't/shouldn't depend on a scoped registration.

from mediatr.

khellang avatar khellang commented on September 17, 2024

@pranavkm When you register a factory delegate as singleton/transient/scoped. I assume the IServiceProvider passed to the delegate matches the provider of the current scope? So the provider would be app/app/request (or whatever scope it's resolved from) respectively?

from mediatr.

jmzagorski avatar jmzagorski commented on September 17, 2024

that seemed to do the trick. I guess I never thought about changing that pieces of code. So does Mediator need to be scoped because those factories need a new ServiceProvider on every request? I was used to SimpleInjector where it was Singleton, and I guess i thought the DI would give me a scoped service even on a Singleton Mediator when it called the IServiceProvider for the service

from mediatr.

pranavkm avatar pranavkm commented on September 17, 2024

@khellang yeah that would be correct.

from mediatr.

jbogard avatar jbogard commented on September 17, 2024

Would it be worth adding a new sample for MVC 6?

from mediatr.

jmzagorski avatar jmzagorski commented on September 17, 2024

Probably would not hurt. I bet there will be a lot of searches for mvc 6 in the upcoming year

from mediatr.

marcels2204 avatar marcels2204 commented on September 17, 2024

It would be great to have an sample for MVC 6 (ASP.NET Core!) with build in Dependency Injection and/or in combination with Autofac

from mediatr.

khellang avatar khellang commented on September 17, 2024

I submitted a sample with #63

from mediatr.

marcels2204 avatar marcels2204 commented on September 17, 2024

I'm using Scrutor by this way:
services.Scan(scan => scan.FromAssembliesOf(typeof(IRequest<>)).AddClasses().AsImplementedInterfaces());

        services.Scan(scan => scan.FromAssembliesOf(typeof(IRequestHandler<,>)).AddClasses().AsImplementedInterfaces());

        services.Scan(scan => scan.FromAssembliesOf(typeof(RequestHandler<>)).AddClasses().AsImplementedInterfaces());

but nothing is registered.
Please give an example for wiring up the services.

from mediatr.

khellang avatar khellang commented on September 17, 2024

@marcels2204 What are you expecting to be registered? Those are all in the same assembly, MediatR...

from mediatr.

marcels2204 avatar marcels2204 commented on September 17, 2024

for example connect all concrete classes which implements IRequestHandler<.>

from mediatr.

marcels2204 avatar marcels2204 commented on September 17, 2024

I've defined Commands, Queries and Handlers. Now i want to populate the services collection

from mediatr.

khellang avatar khellang commented on September 17, 2024

You're picking types in the MediatR assembly. Instead you want types in your own assembly, then add AddClasses(classes => classes.AssignableTo(typeof(IRequestHandler<,>))) to register types that implement IRequestHandler<,> 😄

from mediatr.

marcels2204 avatar marcels2204 commented on September 17, 2024

Thank's ;)

from mediatr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.