-
Notifications
You must be signed in to change notification settings - Fork 33
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
Interface interception fails when used with open generic assembly scanning #27
Comments
Any updates here? This poses a problem for a project I am working on. |
@claerhouth, I am not affiliated with the Autofac.Extras.DynamicProxy package, nor with Autofac as a whole. So I probably shouldn't be saying anything, but after a conversation with @tillig that arose out of his blog post asking for maintainers, my understanding is that most of the Autofac extension projects are severely understaffed—the core Autofac maintainers are just stretched too thin. If you don't see any indication that an issue is being worked on (like this one), it probably is not. And there's no guarantee that the Autofac maintainers will find the time to do so in the near future. If this issue causes such problems for you, I'd suggest investigating it yourself. It appears that all the code necessary to reproduce it is available in this issue. Maybe you can find a solution, which would move your project forward while simultaneously helping out this project and its maintainers. |
@claerhouth Sorry, but @blairconrad is right. There's only two maintainers and we're pretty overwhelmed. If you don't see an update posted, there's no update. If there's something you need an immediate fix for, a pull request with a solution will be your best way to go. |
Going to take a look at it. If I am able to find a solution I'll certainly try and get it accepted... |
It's been a while with no action, so I took a look. Remember, I don't use autofac, so I'm going to use the wrong terms for everything and completely misunderstand stuff. Fun! Here's what I see
I don't see a quick fix, but I have some thoughts:
@tillig, @claerhouth, am I wrong? Is option one possible? Otherwise, how do we feel about the second? |
Oh, @claerhouth, if your concrete components are well-behaved, you might do all right using |
@alexmg was looking at this and mentioned to me that the problem largely stems from needing to know the resolved type (i.e., thought #1 mentioned by @blairconrad). That hasn't really been passed along as yet, though some of the work done recently in core Autofac with decorator syntax that got close to having a more rich resolve context passed around that exposed the required information. I haven't got specific details on it at hand, but it may be that some modifications to the Autofac core internals could help address this in a better way than simply trying to address it by working around things at this level. |
I was about to say that I think there'd still need to be a little work done in this package to resolve the bug, if we could know what we are trying to resolve. But the more I think about it, I think the If the core changed in the way you suggest, @tillig, I'd recommend adding a 3rd method ( If it comes to that, I'd be happy to do the work, assuming nobody else wanted to. |
I admit I got lost in tracing some of the work that got done with the decorator enhancements so I can't speak too closely to that. I think there's information that would be helpful during a resolve operation that could be passed around to aid in things like this, but also in things like circular dependency detection. As mentioned, I can't really get specific about what needs to happen; I've spent a lot of my allocatable free time lately on Moq integration, MvvmCross integration, and answering general questions through the various channels we monitor. It's hard to get momentum on the larger internal updates that need to happen. |
I went back to some earlier decorator work that I stashed and managed to get the From: object ResolveComponent(IComponentRegistration registration, IEnumerable<Parameter> parameters); To: object ResolveComponent(Service service, IComponentRegistration registration, IEnumerable<Parameter> parameters); Need to consider if this is something that will have to wait for the |
I had a similar issue in and as @blairconrad alluded to the problem is in My solution was to just implement an interface-only version of Here's the implementation.... /// <summary>
/// Specifies that a type from a scanned assembly is registered if it implements an interface
/// that closes the provided open generic interface type.
/// </summary>
/// <typeparam name="TLimit">Registration limit type.</typeparam>
/// <typeparam name="TScanningActivatorData">Activator data type.</typeparam>
/// <typeparam name="TRegistrationStyle">Registration style.</typeparam>
/// <param name="registration">Registration to set service mapping on.</param>
/// <param name="openGenericServiceType">The open generic interface or base class type for which implementations will be found.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<TLimit, TScanningActivatorData, TRegistrationStyle> AsClosedInterfacesOf<TLimit, TScanningActivatorData, TRegistrationStyle>(this IRegistrationBuilder<TLimit, TScanningActivatorData, TRegistrationStyle> registration, Type openGenericServiceType) where TScanningActivatorData : ScanningActivatorData
{
if ((object)openGenericServiceType == null)
throw new ArgumentNullException(nameof(openGenericServiceType));
if (!openGenericServiceType.IsInterface)
throw new ArgumentException("Generic type must be an interface", nameof(openGenericServiceType));
return registration
.Where(candidateType => candidateType.IsClosedTypeOf(openGenericServiceType))
.As(candidateType =>
candidateType.GetInterfaces()
.Where(i => i.IsClosedTypeOf(openGenericServiceType))
.Select(t => (Service)new TypedService(t)));
} |
I think that's a decent workaround but it'd be nice if we could solve this by adjusting the actual internals. For example, we know internally when Autofac is resolving something what the target type (interface, class, whatever) should be; it just, from what I can tell, isn't made accessible via any sort of event arguments. If it was available in the |
Hi @tillig, @alexmg I have just had a look on this issue and I see that there will be some breaking changes:
It may also effect the other extensions. Do you think it's worth to fix ? I can spend time on this breaking changes if it is the point ;) |
I don't think it'll be too bad if we have to add some stuff to those events. It's doubtful anyone has implemented their own I know we have a lot of information coming through in the Also, for a singleton... if I'm not mistaken, you only get builder.RegisterType<Component>()
.As<IService1>()
.As<IService2>()
.SingleInstance(); Ideally, though, this wouldn't break all the extensions. We just went through a whole major release breaking change thing; if the solution is that we need another breaking change, I don't know that we're ready to fire off an Autofac 6.0 and do all that again right now. |
This comes from a StackOverflow question
Using assembly scanning in conjunction with interface interception yields an exception:
The component ... cannot use interface interception as it provides services that are not publicly visible interfaces. Check your registration of the component to ensure you're not enabling interception and registering it as an internal/private interface type.
Code to reproduce the issue as a console app:
If you remove the
EnableInterfaceInterceptors()
andInterceptedBy()
calls from the registration then the resolution proceeds as expected.The text was updated successfully, but these errors were encountered: