Search code examples
c#roslyn-code-analysis

Checking project configuration in Roslyn analyzer


In my solution I have configuration stored in multiple configuration files which are in multiple projects. It looks like the this:

  • MainProj
    • appsettings.json
  • Proj2
    • appsettings.json
  • Proj3
    • appsettings.json

All these appsettings are added to IHostBuilder as configuration providers so I can use these settings in regular IConfiguration manner.

What I want to achive is to check wheather the data in some appsettings files doesn't overlap each other. The situation I want to detect:

MainProj

  • appsettings.json { "setting1" : "X" }

Proj2

  • appsettings.json { "setting1" : "Y" }

I was thinking about using a Roslyn analyzer but I haven't found any exmaple of analyzer which uses confiuration from a configuration provider. Is this even possible?


Solution

  • As mentioned in comments adding JSON files as additional files and analyzing them is the only reasonable solution for me.

    Additionally, according to this answer and Microsoft documentation

    Configuration is loaded in serially, in the order the configuration providers are registered, and each successive source overrides any previous source

    So there is no way to detect duplication when the configuration is already loaded.