question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use of DI for internal types breaks Lamar

See original GitHub issue

I 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:

  1. Just mark SwaggerDocumentRegistration as public
  2. NSwag seems to use DI for SwaggerDocumentRegistration as more of a ‘storage mechanism’, than actual DI (since registrations are added in ConfigureServices using AddSwaggerDocument, 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cocowallacommented, Dec 10, 2018

Have confirmed this fix allows NSwag to work with Lamar

1reaction
cocowallacommented, Dec 6, 2018

Even better 😃

I’ll test the new Nuget package with Lamar on Monday!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found