Visual Studio : Project Structure

Have always been a bit curious to understand about VS’s project structure. Also, the use of config files and stuff. So here goes :

  • I create a solution ‘vsprojstructure’
    • I eventually wan to create a git repo out of it.
  • class library ‘configlib’
    • It has an app.config file
  • class library ‘algolib’
    • when algolib is independent, it doesn’t know anything about ‘configlib’
    • when i add ‘configlib’ as a reference, 2 things happen:
      • VS automatically updates the ‘Project Dependencies’ for ‘algolib’ project. It marks that ‘algolib’ depends on ‘configlib’
      • when i build the project, it copies ‘configlib’s dll and pdb files inside ‘algolibs’ bin\Debug folder
      • Note : it does not copy the .config file. This is worrisome
  • Now add a console app called ‘demo’
    • ‘demo’ has ‘algolib’
    • when i build the project, it copies ‘algolib’ and ‘configlib’s dll and pdb files inside ‘demos’  bin\Debug folder. However compilation fails (see code why)
    • So i also add ‘configlib’ as a reference. Compilation succeeds now.
    • However, it shows ‘AppSettings is empty’. This makes sense because as we noted before, the .config was never copied
  • It seems the only way to solve this problem is via post build events.
    • See reference.
    • Using build events.
    • This explains why in many cases we see projects having post build events writing to a common folder
  • Also, note by default the ‘demo’ exe will try to read its own exe. The .config file in the configlib directory needs to be crafted to ensure it reads its own .config file

 

Examples:

1.

robocopy $(SolutionDir)configlib\bin\Debug\ $(TargetDir) configlib.dll.config /xo /xn
IF %ERRORLEVEL% GEQ 8 exit 1
exit 0

2.

robocopy $(ProjectDir)dependencies $(SolutionDir)bin\$(Configuration) /xo /xn
IF %ERRORLEVEL% GEQ 8 exit 1
exit 0

Pro Tip:

  • A critical aspect is to understand the OutputPath parameter in the .config file
  • For each project, this is what controls where the output is going to.

Code:

Github

References:

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s