1

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}");
        }
    }
Spaceman
  • 1,319
  • 13
  • 42
  • `StaticFileHandler` which I think honors ETag "caching". CDN? – AndyPook Jan 20 '17 at 11:06
  • Would it make sense just to embed the image into the library? Then you could just come up with a convention to use for the image locations and/or use attributes to associate an image with a specific class. – NightOwl888 Jan 20 '17 at 17:30
  • @NightOwl888 how does one embed the image into the library? – Spaceman Jan 20 '17 at 22:08
  • 1
    You just need to add the image to the DLL project. Click it and then in the `Properties` window in Solution Explorer, change the `Build Action` to `Embedded Resource`. When the project is built, the image is then embedded as part of the DLL. See [this answer](http://stackoverflow.com/a/3314213/181087) for how to access it from the DLL (which can be more complicated for plugins) and [this post](https://blogs.msdn.microsoft.com/miah/2008/11/13/extending-mvc-returning-an-image-from-a-controller-action/) for how you can then serve the image stream to the browser with MVC. – NightOwl888 Jan 20 '17 at 22:41
  • That sounds like a winner @NightOwl888 ill try it out and get back to you – Spaceman Jan 21 '17 at 10:10

0 Answers0