Comments (22)
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.
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.
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.
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.
@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.
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.
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.
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.
IMediator
/Mediator
must also be scoped. A singleton can't/shouldn't depend on a scoped registration.
from mediatr.
@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.
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.
@khellang yeah that would be correct.
from mediatr.
Would it be worth adding a new sample for MVC 6?
from mediatr.
Probably would not hurt. I bet there will be a lot of searches for mvc 6 in the upcoming year
from mediatr.
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.
I submitted a sample with #63
from mediatr.
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.
@marcels2204 What are you expecting to be registered? Those are all in the same assembly, MediatR...
from mediatr.
for example connect all concrete classes which implements IRequestHandler<.>
from mediatr.
I've defined Commands, Queries and Handlers. Now i want to populate the services collection
from mediatr.
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.
Thank's ;)
from mediatr.
Related Issues (20)
- Seperate Interfaces to Contract project to provide abstraction layer HOT 8
- Improve base package readme
- Implement IRequest, how to enter Behavior HOT 1
- IRequestExceptionHandler from other project not loaded
- Question:
- Question: How I can setup MediatR 12.1.0 in Ninject module? HOT 2
- How do I register INotification with generics? HOT 2
- long running INotificationHandler HOT 3
- Question: Can multiple MediatR handlers be packaged into a single class HOT 3
- .NET8 upgrade from .NET Core 2.1 : Command cannot be used as type parameter 'TRequest' for 'IRequestHandler<TRequest>' HOT 3
- Irequest in library and handler in web api project HOT 2
- what use net8 aot publish HOT 2
- v12.3.0 Breaking Change HOT 27
- Question: Why IRequestExceptionHandler is not catching? HOT 2
- Question: How to register generic requests HOT 10
- Can't register generic handlers in latest version HOT 5
- Question: Why MediatR does not support ValueTasks ?
- Question: Why MediatR does not support ValueTasks ? HOT 3
- Generic Handler implementation only handles single generic argument and breaks existing implementations HOT 9
- Validation is not invoked when IRequest has no Result
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mediatr.