Nuget Conflicting Project and Batch Binding

I am running an ASP.NET Core app for .NET 4.6.1 framework. I have 1 solution with multiple projects. All projects are class libraries that reference each other via PackageReferences in their .csproj (this way we can create, package and modify them ourselves). However, I want to be able to test their integration with each other without inserting them into NuGet first - otherwise I want to use them as ProjectReferences in the solution, and PackageReferences when building them through my Jenkins build process to version the component separately.

When .NET Core was based on project.json this worked great. I would set the version at the top of project.json, and if the project existed with that version in the solution, it would refer to it as a project, otherwise it would look for it in my NuGet feed.

The problem with using ProjectReferences is that the entire project will get the same version when they are created and sent to NuGet.

Is there a way to do this in csproj? Looking for a project reference if it exists, else look at NuGet?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <VersionPrefix>1.3.0</VersionPrefix>
    <TargetFramework>net461</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <AssemblyName>MyProject1</AssemblyName>
    <PackageId>MyProject1</PackageId>
    <NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MyProject2" Version="1.4.0-*" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.1" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

</Project>

      

In the example above, I would like MyProject2 to reference the ProjectReference if 1.4.0 exists in the solution.

+3


source to share


1 answer


I recently tried to do the same and couldn't find an answer, but figured out what works for me. You can use a condition Exists

in MSBuild csproj

to include a project reference when it exists and exclude a package reference if it exists:



<Project Sdk="Microsoft.NET.Sdk">
   ...
   <ItemGroup>
     <PackageReference Condition="!Exists('[path-to-project].csproj')" Include="[package-id]" Version="[pacakage-version].*" />
   </ItemGroup>
   ...
   <ItemGroup>
     <ProjectReference Condition="Exists('[path-to-project].csproj')" Include="[path-to-project].csproj" />
   </ItemGroup>
   ...
</Project>

      

+1


source







All Articles