question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Per request scopes in e.g. Autofac will eat resources

See original GitHub issue

Sample here: https://gist.github.com/danielwertheim/887868c17ab8cd67be44

Kind of followed your sample code… Probably have missed something. And yes, I know I could inject Func<SomeDependency> instead and have the handler resolve and dispose…

But how do I acomplish per-routed message scoped handlers? If not possible with MediatR, perhaps state that clearly in the sample?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
leddtcommented, Apr 4, 2016

@kappy I see!

I wonder if this can work with Owin? The challenge with Owin is that there is not really a static way of getting the current request context. I’ll have to think about it 😄

1reaction
khellangcommented, Mar 19, 2016

So the Q is how to achieve this with MediatR in e.g. something like a long running windows service?

Just resolve the stuff you need from a lifetime scope (as shown in the blog post mentioned)? Make sure you dispose it when you’re finished. Or just dispose the container when you’re done with it.

As you would need to get hooked into before and after the func for resolving the handler…

I’m not sure what you’re talking about here. What func?

Just raising my voice as this easily could lead to people getting more and more handler resources that in turn has resources on … …

I don’t think anyone here are experts on all the containers in the samples that have been contributed to this repo, and by all means, they could absolutely have bugs. I guess it’s generally expected to know how the container you’re using works. That should probably be a requirement anyway - whether you’re using MediatR or not 😉 If you’re having trouble wiring up the container properly, maybe it’s best to just go “pure DI”-style? 😄

If you’re looking to learn more about Autofac and its lifetime scoping and transient tracking, I suggest you read NIck’s blog, specifically An Autofac lifetime primer. There are also some good resources on Controlling Scope and Lifetime in the Autofac docs, specifically the bits about Disposal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I work with per-request lifetime scope?
Per -request lifetime makes use of tagged lifetime scopes and the “Instance Per Matching Lifetime Scope” mechanism. The request scope is tagged with...
Read more >
Controlling Scope and Lifetime - Autofac's documentation!
Effectively, a lifetime scope equates with a unit of work in your application. A unit of work might begin a lifetime scope at...
Read more >
Why does Autofac's implicit relationship type for dynamic ...
They can keep channels open and eat up resources if you don't do ... Your MVC controller gets resolved from the per-request lifetime...
Read more >
Dependency injection guidelines - .NET
Discover effective dependency injection guidelines and best practices for developing .NET apps. Deepen your understanding of inversion of ...
Read more >
Comparing ASP.NET Core IoC container service lifetimes ...
Instance per request builds on top of instance per matching lifetime scope by providing a well-known lifetime scope tag, a registration ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found