Disable transitive project reference in .NET Standard 2

前端 未结 2 1063
挽巷
挽巷 2020-11-28 13:58

I\'m writing an MVC website using ASP.NET Core 2.0.

In the ASP.NET Core project (let\'s call it Web), I reference a .NET Standard 2 project in the same

2条回答
  •  情深已故
    2020-11-28 14:22

    Transitive project references are new feature of SDK-style csproj (1,2) format used in .NET Core/.NET >= 5. You can also use this csproj for old .NET Framework projects (1,2,3) but with some exceptions.

    In this SDK-style format project references (represented by entry in .csproj file) are transitive. This is different to old non-SDK .csproj used previously.

    But you have two options to go back to old non-transitive behavior.

    1. Use true property in .csproj that references projects for which you don't want their transitive dependencies to be visible by compiler.

      In your case you can add it to Web project. (first project that reference other projects, Web -> Service -> Business)

      You can also set this behavior globally for all .csprojs by doing it in Directory.Build.props file that you put in the root folder that contains your source.

      
            
          true
        
      
      

      With this file you basically have old project reference behaviour. Useful when you do migration of old .NET Framework solution that uses old csproj format to new SDK-style .csprojs.

    2. On the project that you reference you can set which dependencies shouldn't flow further when the project is referenced. You use PrivateAssets="All" attribute on for this. So for example you can edit Service.csproj like this:

      
          
      
      

      This is more flexible and fine-grained approach. You can control with particular transitive project references should be visible when the project is referenced.

    It depends what you prefer. If you are used to old csproj behavior or want to migrate old solution to .NET Core then just go with DisableTransitiveProjectReferences. It's the easiest solution.

提交回复
热议问题