I have a dotnet core app with a bunch of dependencies that i am injecting.
The structure of the dependencies is like so:
- Lib.cs
- Logo.png
- LibInfo.cs
I am currently injecting these using this pattern:
var container = new Container();
container.Configure(config =>
{
config.For<ILib>().Add<Lib1>();
config.For<LibaryInfoDto>().Add<LibInfo1>();
config.For<ILib>().Add<Lib2>();
config.For<LibaryInfoDto>().Add<LibInfo2>();
/// ..... The goal is to not have this hard coded i just want to search the assemblies loaded for anything that extends interfaces/dto
config.For<ILib>().Add<LibN>();
config.For<LibaryInfoDto>().Add<LibInfoN>();
}
I am looking for a way to inject images. My current train of thought is to add a controller endpoint that returns an image based on a path build off of the libary name eg.
public ActionResult GetIcon(int libId, collection<Ilibs> libs)
{
// Base on id get correct lib
// Some code to return image yada yada yada
return File(document.Data, document.ContentType);
}
But I don't like this pattern, however its the only one I can think of. Is there a way to use Dependency Injection or some other method to put an image in one project into a directory of another. The goal is to drop a "lib" in and without any setup the application will pick it up.
--- UPDATE 1
NightOwl888 has come up with a solution that I have implemented like so. This works really well for me hope it helps anyone else.
// Controller
public ActionResult Images(string name)
{
var image = _libs.FirstOrDefault(lib => lib.Name == name).LogoStream;
string contentType = "image/png";
return new FileStreamResult(image, contentType);
}
// base class for libs
public virtual Stream LogoStream {
get
{
var assembly = GetType().GetTypeInfo().Assembly;
var name = assembly.GetName().Name;
return assembly.GetManifestResourceStream($"{name}.{Logo}");
}
}