1
votes

I'm trying to build out a bunch of stateful microservices written in .NET Core 2.1. These are not ASP.NET applications, no http interface, but rather designed as console apps that are consuming messages off a service bus.

I'm having a deuce of a time trying to figure out how to reference my appsettings.json file into the service packages. There's a bunch of articles related to using the appsettings.json file in an ASP.NET service fabric app (basically spelling it out in a webhost builder) but that's not applicable for me, since I'm not using ASP.NET.

I have a single appsettings.json file that all the microservices in this application should be able to use.

I also found this article Service Fabric include additional files which talks about putting it into an sfproj target for MSBuild, but there seems to be no information whatsoever about what the file structure looks like on the target end.

What I tried to do is add targets to my sfproj file like so:

<Target Name="AfterPackage" AfterTargets="Package">
    <Copy SourceFiles="appsettings.json" DestinationFolder="$(PackageLocation)\appsettings.json" />
</Target>

and then in my service, I'd try to instantiate an IConfiguration object based upon ..\appsettings.json but as one might guess by now, the service, upon attempting to publish, throws a FileNotFoundException looking for the appsettings.json file, so clearly the file is in a different location relative to the micro service. (Of course, the publish failure just says there was a problem and rolls back, you have to dig into the the SF Explorer to actually see that message)

What should this configuration look like? I can't find any documentation describing what the file system of a stateful microservice looks like, I'm not clear on where I should be putting this common file, or how the services should be accessing the file. Some help would be appreciated.

1
Does this appsettings.json is located in some shared folder or it is included directly in projects?Oleg Karasik
For right now, just to get this thing working, I put it into a Data folder in each service project... but I really want it in the Application project as a shared location.Jeremy Holovacs

1 Answers

0
votes

I hope I got everything right.

Here are the steps:

  1. Put appsettings.json into shared folder in *.sfproj and include it in project using Visual Studio.
  2. Unload *.sfproj and start editing it as .xml
  3. Add the bellow code right after the last <Target> element.
<Target Name="AfterPackage" AfterTargets="Package">
    <Copy SourceFiles="appsettings.json" 
          DestinationFolder="$(PackageLocation)\%(_DeployableServiceProjectReference.ServiceManifestName)\%(_DeployableServiceProjectReference.CodePackageName)" />
</Target>

The _DeployableServiceProjectReference is the internal <ItemGroup> initialized by the previous targets.

These should force the package command to copy appsettings.json file to service's code package directory for each service references in *.sfproj.

Hope this helps.