0

Im writing an web app that consists of a 3 projects. The ASP.net MVC5 website, a windows service which runs jobs which users schedule using the website, and then a class library which the other two projects reference. I have a JSON config file that the class library references. It contains data as to how jobs should run. I want to include it in my source control and I want it to be JSON so my colleagues who use the app can easily change it without having to recompile the solution. Only the class library needs to access the json file - the other two projects reference the class library so they essentially reference the settings in the json file by accessing classes in the class library.

My issue is, i am referencing the json config file path in the class library classes like so: AppDomain.CurrentDomain.BaseDirectory + "config.json" However, when the web server instantiates a class from the class library the path resolves the the base directory of the web server instead of the class library, which isn't surprising.

Whats the best way to do this? Can I keep the config file permanently in the webapp project and then have a link to the config file in the windows service project perhaps? Not sure how that would work after deployment of the solution though?

Web project is .NET standard MVC 5. JSON much preferred over XML for config file type.

Matt
  • 66
  • 7
  • not sure if this can help, but in one of my project I use this: `var configPath = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory, "config.json");` the key point here is that in WebAPI project the correct path is in `AppDomain.CurrentDomain.RelativeSearchPath` which is `null` for desktop app, so `AppDomain.CurrentDomain.BaseDirectory` is used in this case – vasily.sib Mar 20 '19 at 03:20
  • Is it a .NET Core project? Is the file needs to be JSON? Can it be an XML configuration file? – Chetan Mar 20 '19 at 03:29
  • Web project is .NET standard MVC 5. JSON much preferred over XML for config file type. – Matt Mar 20 '19 at 03:32
  • The better approach would be to write ConfigurationSection handler which will parse the config section and provide the data... this is very well supported with XML config files... You can create this section handlers in Class Library... And then you need to have the XML config file in Web Project and Windows Service project... you don't need to worry about path.. it will read it from the applications path by default... – Chetan Mar 20 '19 at 03:35
  • Yes, thats essentially how its currently working only it uses json rather than xml. But the problem becomes that the config file needs to reside in both the web & service projects. I want to only have one config file for the solution, otherwise to make a change you must change both files and ensure they are kept exactly the same. Is it possible to have the windows service config file to just be a pointer to the config file in the web project for example? Or what are my options? – Matt Mar 20 '19 at 03:38
  • If you know the exact path where the web application is running and it never changes then you can put that path in the code of Windows Service to read the file. That's a dirty fix. Better approach would be to have one config file in the solution and the build script should take care of copying the file to both WindowsService package and WebService package. So it is available in both the applications at run time. – Chetan Mar 20 '19 at 03:44
  • One more way is to write post build action for Web Project and Windows service project to copy the JSON file from class library project to respective project directories. You can do that by opening project properties and select Build events..[read here](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-specify-build-events-csharp?view=vs-2017) The command to copy would look like `xcopy /S /I /Y /R $(ProjectDir)..\config.json $(ProjectDir)` This should work for you. – Chetan Mar 20 '19 at 03:47
  • https://stackoverflow.com/questions/11001822/copy-files-from-one-project-to-another-using-post-build-event-vs2010/11001949 – Chetan Mar 20 '19 at 03:50
  • Yea i think ill make it part of the build process as you said. Thanks Chetan! – Matt Mar 20 '19 at 03:51

1 Answers1

0

I've just tested it to make sure, so this should work:

If your file is in your class library project, you can just go to solution explorer, right click on it, > properties, then in the window look for copy to output directory. Set that to Always or only if newer which ever you feel is best, and it should be copied to your bin folders of your projects that reference the class library.

Topher
  • 1,011
  • 11
  • 19