Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional project path variables in VisualStudio solutions

We have a big C# program that is split into 2 major groups: Kernel libraries and (Customer) specific applications & services.

Our TFS structure (simplified) is like this:

Kernel

  • DLVP
  • Release 1
  • Release 2
  • Release 3
  • ...

CustomerA

  • DLVP
  • Release

CustomerB

  • DLVP
  • Release

We used nugets to compile and distribute our kernel code and include it into the Customer applications. So we can easily move to a new version/release. However, we are not content with only dll's. We want to have the full debugging and editing experience everywhere.

You can include kernel projects in the customer solutions, but they will have a reference to Release X, so if we want to move to a new release, we have to change every solution and project file for every costumer, which is a lot (N customers x M services/programs = a lot)

I read that you can use Environment variabels to change some values in the .sln & .csproj files, but we would like to have something that is more controllable across all developers. I prefer referencing a shared variable that can also be stored in TFS.

I've made a small .sln file to clarify:

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{B6B9AE41-99ED-47CE-B35C-F693C5F5F736}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibKernel", "..\..\Release 1\LibKernel\LibKernel\LibKernel.csproj", "{439BD82B-340D-4D69-B367-E52E0DF27983}"
EndProject

I want to change this part:

"LibKernel", "....\ Release 1 \LibKernel\LibKernel\LibKernel.csproj", "{439B....}"

into

"LibKernel", "....\ {CustomerA.CurrentRelease} \LibKernel\LibKernel\LibKernel.csproj", "{439B....}"

Something like that (and also for .csproj files), but if they are better approaches, I'm glad to hear them.

Thx a lot

like image 313
Eyden Jones Avatar asked May 08 '26 06:05

Eyden Jones


1 Answers

We use this kind of idea for referencing the assemblies located in particular location per as user needs.

First you need to save an external file with content as shown below..

Suppose filename is buildpath.xml

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
    <PropertyGroup>
        <AssemblyDirLocation>C:\inetpub\wwwroot\SiteInstance\Website</AssemblyDirLocation>
    </PropertyGroup>
</Project>

Next we import this config in .csproj. I believe it shall work in .sln files also

For proper understanding I have included some surrounding text here from .csproj file

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <Import Project="..\build\buildpath.xml" Condition="Exists('..\build\buildpath.xml')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <!-- more text -->

Notice following line from above text

<Import Project="..\build\buildpath.xml" Condition="Exists('..\build\buildpath.xml')" />

Usage:

In .csproj or .sln file where you would have imported above file, you can write below macro to replace the value. Notice the tag name matches the macro written i.e. $(AssemblyDirLocation).

<ItemGroup>
    <Reference Include="Site.Kernel">
      <HintPath>$(AssemblyDirLocation)\bin\Site.Kernel.dll</HintPath>
      <Private>False</Private>
    </Reference>
</ItemGroup>

Whenever you make changes in buildpath.xml external file you need to reload the referencing project.

I hope that was clear and will be helpful.

like image 112
Harsh Baid Avatar answered May 09 '26 22:05

Harsh Baid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!