Search code examples
jsonnuget

Best way to share a JSON file via nuget?


I have a JSON schema describing how to communicate with a .NET microservice I am developing. I would like to share this JSON schema (along with various other poco objects) in a nuget file with other services who wish to communicate with me over Kafka.

My question is, how can I share this file using nuget? It's trivial to share the poco objects, but I'm not sure how to share something like a static JSON file, and am also unsure how the consumer of this nuget package would then reference and consume that JSON file.

I've tried searching the nuget documentation but couldn't find anything. Is this possible?


Solution

  • Here are the docs on content files in nupkgs, but note that this is the reference for the .nuspec file. If you're packing a project file this is typically automatically generated for you.

    There are two ways for a project to use NuGet packages, either with packages.config or PackageReference (SDK projects, used by .NET Core, only support PackageReference). When installing a package for a packages.config project, NuGet will look at all the files under the content directory within the nupkg and copy them to the project's folder. On the other hand, projects that use PackageReference will not copy the files, instead MSBuild Content items are creates for files in the contentFiles directory of the package in the global packages folder. Therefore, if you want to support users of your package whether they use packages.config or PackageReference, you need to duplicate content files in both the content and contentFiles directories in the nupkg. If you pack a SDK style project, the pack targets will automatically add any content items to both the content and contentFiles directories in the nupkg.

    So, you need to consider how users of your package will use the json file (both whether they will be using packages.config or PackageReference, and what they will do with the file once it's on their computer). If it's something that they'll only use programatically, it might be easier for you to just embed the json file in your assembly as an embedded resource, and provide them a method to call to get the file as a string. That way the difference between packages.config and PackageReference is not relevant.