Use of DI for internal types breaks Lamar
See original GitHub issueI recently found an issue with NSwag.AspNetCore
and the Lamar IoC library (successor to the venerable StrutureMap), where a call to UseSwagger
causes an exception.
Full details are available here.
The issue is caused by NSwag.AspNetCore
’s unusual use of DI to resolve internal
types, which can be seen here:
var documents = app.ApplicationServices.GetRequiredService<IEnumerable<SwaggerDocumentRegistration>>();
While it could be argued that this is Lamar’s problem, I’m of the opinion that using DI to resolve internal
types is an anti-pattern.
This can be resolved in a couple of ways:
- Just mark
SwaggerDocumentRegistration
aspublic
- NSwag seems to use DI for
SwaggerDocumentRegistration
as more of a ‘storage mechanism’, than actual DI (since registrations are added inConfigureServices
usingAddSwaggerDocument
, so an alternative would be to not add these to the container, but use some kind of storage class instead
Option 2 obviously changes the public API, whereas option 1 involves changing a single keyword and shouldn’t break anything, so option 1 seems like the obvious way to resolve this.
I’m happy to make this change and test it, if you’re willing to accept a PR for this?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
ASP.NET Core with Lamar : Solving the “single interface
This article presented a way to avoid a “hard” dependency on the DI library with all calls/references to that library residing in the...
Read more >Internal classes not visible to Lamar Assembly · Issue #75
I have some classes and interfaces that I want its protection level to be "internal". I made them visible to consuming assemblies using ......
Read more >How can I use Internal Classes with Dependency Injection
I found an answer to my question. I can use the following pattern to achieve what I wanted. In the Assembly that has...
Read more >c# - Can I use Dependency Injection without breaking ...
The most important reason to make classes internal is to prevent external parties (that you don't have control over) to take a dependency...
Read more >Auto Resolving Concrete Types - Lamar
Lamar allows you to resolve instances of concrete classes without configuring that concrete type with a few provisos: The concrete type must have...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Have confirmed this fix allows NSwag to work with Lamar
Even better 😃
I’ll test the new Nuget package with Lamar on Monday!