Skip to content

Does this work with Dependency injection? #327

Open
@joshmouch

Description

@joshmouch

So first off, if you're using MS's Dependency injection with AspectCore, there's not a single example available that doesn't the custom IServiceCollection.BuildDynamicProxyProvider().

I used GPT-4o to try and show me how to get AspectCore working with MS DI and it just kept hallucinating how to do it with CastleCore.

So, without documentation, I dug through the code for a few hours, which is hard since a lot is in a different language. I came to the conclusion that IProxyGenerator is only registered in one place in the whole code base, and so it's kind of the "bottleneck" for getting everything working:
ServiceCollectionBuildExtensions.WeaveDynamicProxyService().

So I tried calling that manually, but then I get an error about some service provider being keyed but another one not supporting keys. I didn't dig into it.

So I think that basically means the only way to get this to work is to call one of the BuildDynamicProxyProvider extensions, which in turn calls the WeaveDynamicProxyService() extension? Unfortunately, that throws the error I just mentioned.

But even if the error does go away, it's very odd to have the library setup on IServiceCollection, but then not have any of it work unless that IServiceCollection is built in a non-standard way. This is the first time I've seen something like that.

Lastly, the WeaveDynamicProxyService appears to actually build the DI container prematurely (the rest of Service setup likely isn't complete at this point), create a replacement IServiceCollection and then dispose of the IServiceProvider it created. This extra DI service build could potentially cause issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions