简体   繁体   中英

Is there a way to tell Visual Studio to use only specific kinds of references in a .Net Core solution?

I keep running into an issue in Visual Studio where I reference a NuGet package that is installed in my local NuGet cache, but is not added as a reference in the NuGet package references for the project. Visual Studio defaults to adding a hint path reference to the package on my local file system instead of a package reference. IE this

<ItemGroup>
    <Reference Include="Awesome.Package.dll">
        <HintPath>local/path/to/NuGet/cache/Awesome.Package.dll</HintPath>
    </Reference>
</ItemGroup>

instead of this

<ItemGroup>
    <PackageReference Include="awesome-package" Version="x.y.z" />
</ItemGroup>

Now, I know how to install NuGet packages correctly, that's not the issue. I want to prevent Visual Studio/.Net (whichever is the responsible party) from reaching outside the solution for dll references. In my projects there should only be 3 valid kinds of references:

  • other projects in the solution
  • .Net Core SDK
  • NuGet packages referenced with a <PackageReference> tag

If VS/.Net can't reference code like this, I need it to raise an error or otherwise indicate that the code I am trying to use does not exist. Assuming that it is OK to just reach out wherever it feels like to find dlls is not acceptable. The bad references get committed, which cause my build system to fail and other team members to get errors when they pull down commits containing the bad references.*

Is there a way to whitelist/blacklist particular means of referencing external code in Visual Studio and/or .Net Core?

*yes, we can do a better job filtering these issues out when committing code or doing PRs, but that is relying on humans to catch errors that could be prevented from occurring in the first place by software

Is there a way to tell Visual Studio to use only specific kinds of references in a .Net Core solution?

I'm afraid the answer is negative. As I know there's no such option in VS can manage references in .net core projects in this way.

The original issue you encountered may have something to do with the way you consumed the package. In a .net core project, if you add reference to that nuget package by Nuget Package Manager UI or Package Manager Console , it should be normal PackageReference format. So actually what you need to do is use the right way to consume nuget packages since VS don't support the behavior you want in any option as I know.

*yes, we can do a better job filtering these issues out when committing code or doing PRs, but that is relying on humans to catch errors that could be prevented from occurring in the first place by software

For now, since VS supports the scenario in which we reference normal assemblies in Reference format, so the Intellisense won't raise error when you have that kind of reference in .net core project file. As an alternative, maybe you can build the project locally before committing it to repos.

Try a Directory.Build.props file. Create a text file and rename it as Directory.Build.props , you can add content into it like:

<Project>
  <Target Name="CheckIfThereExistsNotValidReferences" BeforeTargets="build">
    <ItemGroup>
      <InValidReferences Include="@(Reference)" />
      <SdkReferences Include="@(Reference)" Condition="$([System.String]::new('%(Reference.HintPath)').Contains('C:\Program Files\dotnet\sdk\NuGetFallbackFolder'))" />
      <InValidReferences Remove="@(SdkReferences)" />
    </ItemGroup>
    <Message Text="aaaaaaaaa @(InValidReferences)" Importance="high" Condition="'@(InValidReferences)'!=''" />
    <Error Text=" Raise the error cause there exists invalid references!" Condition="'@(InValidReferences)'!=''" />
  </Target>
</Project>

Put this file in solution directory, then it will help check if there exists the reference format you don't want in project file. If it exists, it will raise error. It works in my machine locally.With this file in solution directory, every time you build it locally before committing the changes, it there's exists the Reference format you dislikes, the build will fail and raise error. But it's only for build time, to raise error like Intellisense way, this is not supported now. You can post Suggest a Feature request inDeveloper Community . Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM