-
Notifications
You must be signed in to change notification settings - Fork 29
Resolving concrete service type does not work in Middleware.Invoke #37
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
Comments
You've left out quite a bit of information here... What are the dependencies of ASP.NET Core uses |
This is used for a web socket middleware. It's basically a modified clone of https://radu-matei.com/blog/aspnet-core-websockets-middleware/ Used to have the service provider in the ctor but after upgrade to .NET Core 2.0, for some reason, it wasn't possible to use reosolve the service provider from a ctor. I got exception:
|
I tested with TryGetInstance as you suggested and it indeed returns null. GetInstance returns correct service. After calling GetInstance it works with TryGetInstance, so this seems to be a StructureMap issue? |
This seems to be by design in StructureMap, if something isn't registered in the container. http://structuremap.github.io/resolving/try-getting-an-optional-service-by-plugin-type/ |
Yeah, that's what I was afraid of. I'm not sure what I can do with this in the adapter. For some reason, StructureMap doesn't "try as hard" when you do Does that also mean that the following is true?
We've had some of the same issues with open generic and enumerable services as well 😢 The big issue is that the Maybe we can get around things by always calling |
"For some reason, StructureMap doesn't "try as hard" when you do" -- The reason is that the semantics of You could beat this issue by making StructureMap still use its default concrete type discovery rules with a custom And for the record, I griped a lot to the ASP.Net team about exactly this issue. |
I have a very similar problem, a particular class cloud not get loaded using
This works, but if i remove the constructor, then the service provider stops working, i have no idea why:
The solution was really stupid, using From what i understand, the only difference between required or now, was that required throws an exception if no implementation is found, so, why this behabiour is presenting? |
IServiceProvider.GetService() calls into StructureMap's TryGetInstance(), which is a little bit semantically different in that it will resolve as a null if the concrete type isn't explicitly registered. GetRequiredService() calls to StructureMap's GetInstance(), which in the case of a concrete type with public constructors, will quietly create the registration for you and let you continue on your way. All of that is functioning as designed in StructureMap. Arguably, the confusion comes in because of the adapter and the different semantic meanings of the methods in the aspnet core abstractions versus the older meaning in StructureMap itself. |
Yes, it is confusing, i was confident in this stackoverflow question. I will add this info to future users. Just one more question, if the semantics are different, why injecting the object in the constructor causes the instance to be build correctly with the serviceProvider.getService? |
I ran across this issue in trying to migrate a large ASPNET Core 1.1 application to 2.1, where our database migrations code rely on DI, but were failing due to being unable to resolve concrete types. Taking @jeremydmiller's suggestion and inspiration from lamar I wrote a ConcreteTypeFamilyPolicy for StructureMap. It gets the "works on my machine" badge; I wouldn't rely on it for production applications. |
Trying to resolve a concrete type with dependencies does not work when I pass the
IServiceProvicer
in as an argument to the Invoke-method. However, if I get the service via Container first, and then try to resolve withIServiceProvider
it works:EDIT: This is for .NET Core 2.0.
The text was updated successfully, but these errors were encountered: