-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autofac and Pipeline Behavior #128
Comments
I am having the same issue. The pipeline's Handle method gets called 3 times after sending Ping as @martosource describes. If I create an OuterBehavior and an InnerBevavior and register them as follows:
I get the following output:
Removing the following line seems to fix the problem in both cases:
|
@JamesFenton all fixed, thanks. |
It seems Autofac does not support registering open generics when doing assembly scanning: autofac/Autofac#215 What this means is that the built in pipeline behaviors
This also means that any open generic
The built-in behaviours can be registered using
But registering of all open generic |
I switched to using Autofac instead of using the ASP.NET Core default container because Autofac supports better generic constraints. I wanted to use interfaces as markers for certain pipeline behaviors like this and Autofac does it: public class DistributedCachingHandler<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : class, IUseDistributedCache
where TResponse : class Anyway, I believe I would see this issue but I don't because I'm still using the Maybe using |
What I would like to do is write some tests for each of the different containers. Ideally I'd like to keep the registration code in each of the samples so that people can easily find it. I'd also like to avoid "infecting" the current test project with all the different containers, so perhaps another test project that references all the same projects? What do you think @jbogard? |
Well we do have the example projects, I wouldn't mind extending those perhaps to highlight what capabilities each container has through tests. |
Yeah that sounds good to me!
…On Thu, Jan 19, 2017 at 1:29 PM James ***@***.***> wrote:
What I would like to do is write some tests for each of the different
containers. Ideally I'd like to keep the registration code in each of the
samples so that people can easily find it. I'd also like to avoid
"infecting" the current test project with all the different containers, so
perhaps another test project that references all the same projects? What do
you think @jbogard <https://github.com/jbogard>?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#128 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGYMrHl61zG8LHvS_VpI-pehPJlogEMks5rT7mFgaJpZM4LfBd5>
.
|
@JamesFenton If you do not register ContravariantRegistrationSource, it seems that you will lose out on the polymorphic dispatch. For example, if you had an implementation of Are you seeing this same limitation? |
So was there a solution without caveats? I'm feeling pressure to use the same di container as Jimmy :) |
You can look at the way StructureMap does this and borrow its implementation? |
I should have checked the autofac issue, just reposting this here for posterity, a suggested workaround for autofac is:
Swapping IRepository for IPipelineBehavior i guess. Havent tried it yet. |
Part of the problem with the ContravariantRegistrationSource is that it applies to any service resolved. Instead, we want it limited in scope. For a project I ended up implementing my own ScopedContravariantRegistrationSource (largely copy/pasted the original source but added code to restrict which services it applied to via constructor parameter). Then register this source for only the services you want to have contravariant resolution. It's certainly not as flexible as StructureMap's configuration, but it works decently well. |
@barryschulz you are correct, if you remove the The setup of Autofac does seem to require a lot more ceremony than StructureMap, unfortunately. |
Hey guys, I had the same problem this morning and I solved by implementing my own ScopedContravariantRegistrationSource (as advised by jdaigle). It's a simplistic (probably incomplete) implementation but it works quite well with my easy use case: Here is the code:
Then simply call in Startup: Regards, |
@seroche Can you give an example of usage as well? |
I have the same behavior on behaviors as @martosource or @JamesFenton described on dotnet core default DI. Any hints on how to fix it without my own error prone ScopedContravariantRegistrationSource? |
I'm using the recommended ASP.NET Core DI registration, see below |
@maldworth I guess you should change your ContravariantRegistrationSource to this
|
I had a similar issue with MediatR and
and I use it like this :
This issue is also discussed here : https://stackoverflow.com/questions/46464944/autofac-contravariance-and-resolving-open-generic-types/71010423#71010423 |
Autofac fixed this issue in 6.2.0. But my generic Did anyone else solve this problem with the latest version? |
I am trying to get new pipeline working with autofac but it is running the same pipeline many times with different types.
Adding the following class the MediatR example
Added this to the BuildMediator for the autofac example program.cs
Produced the following output:
For the structure map example you get:
The text was updated successfully, but these errors were encountered: