Skip to content

ASP.NET Core Embedded Resource

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


ASP.NET Core Embedded ResourcesIn my previous blog about adding gzip compression when using NancyFX, I wanted to test the compression by outputting a large json data file in one of my endpoints. At first I was just going to generate some random data and serialize it at runtime but then I found Mockaroo, which provided a small dataset of random data that I could download. So now that I had this generated file, I  wanted to include the file in my project as an embedded resource in which would be read and at returned as output. If you have any questions, please follow me on Twitter.

Project.json

The project.json file is new to ASP.NET Core, which defines project information and dependencies, similar to the combination of a web.config, csproj files, package.config is in ASP.NET 4. There is a resource section in the project.json which is for adding resources to your assemblies as embedded resources.  The values are relative to the project.json file itself and allows you to specify glob patterns (wildcards). In my demo project, I had a fakedata.json file which resides in the same path as the project.json file.  Here is my project.json file, including the new resource section.

Stream

All of your resources will be included in the assembly when built.  You can read those resources by getting a stream of the resource.

Exclude

If you use a glob in your resource path to possibly include all files within a directory, but want to exclude specific files, you can do so with the resourceExclude.

Docs

If you are looking for more information about the project.json, I’d recommend taking a look at the GitHub wiki.  The asp.net docs are helpful but I couldn’t find a ton of info the project.json file specifically (yet).  I assume the asp.net docs will improve over time and they are a work in progress.

6 thoughts on “ASP.NET Core Embedded Resource”

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2027

  2. I have an embedded resource (*.RESX) file in a 4.5.1 Class Library. I have an ASP.NET Core 1.0 (RC1, Update 1) referencing this Class Library – and at runtime when referencing the embedded resource (*.RESX) file I get a IO.FileNotFound exception.

    I’ve tried adding the embedded resource to project.json, but it’s not working.

    I know this is not the scenario you were describing, but I was wondering if you knew the trick to get an ASP.NET Core web app to properly use a 4.5.1 Library that has an embedded resource.

      1. Awesome Derek, there are some good tips there. it looks like ‘naming’ is the key. I’m not 100% of how to properly format the namespace in the .json file – but this looks like it’s the right track. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *