Visualstudio-docs: Architecture Dependency Validation/Layer diagram not working in .Net core (ie not show errors when dependency diagram is violated)

Created on 25 Apr 2018  路  13Comments  路  Source: MicrosoftDocs/visualstudio-docs

I'm trying to setup a simple architecture layer diagram to validate that we don't break our layering rules by using namespaces/dll's from incorrect layers.

I setup a simple solution that has a console app, and 4 dll's (all using .Net Core). The console app calls into Dll1, Dll1 calls into Dll2, Dll2 calls into Dll3, etc. Then I setup a layer diagram to enforce this layering and try to have the console app call into Dll 4 and expected to get an error; however, I don't.

If I change this to use a windows form app (instead of the .net core console app) and change all the dll's to be .net standard, then I do get the error enforcing the layer diagram.

I've tried adding the Microsoft.DependencyValidation.Analyzers NuGet package to the .net core console app, but that still doesn't work. Is this a problem in .Net Core? Or something I'm missing?

Also tried doing another test with all .net core but used an MVC app instead of a console app. I get the following error when building the solution (and the dependency layer diagram doesn't go instead the MVC app). CSC : warning DV2001: The project does not reference any Dependency Validation diagrams or referenced diagrams are not valid. Dependency validation will not be performed.

See link below where I was advised to add an issue here.

https://forums.asp.net/p/2139788/6203822.aspx?Architecture+Dependency+Validation+Layer+diagram+not+working+in+Net+core+ie+not+show+errors+when+dependency+diagram+is+violated+

Pri3 doc-bug

Most helpful comment

@LarsKemmann you're right: dependency validation works fine for old-style MSBuild projects, and the only reason it appears not to work for new-style MSBuild projects is that dependency validation designer doesn't add the NuGet package and the link to the model file because it doesn't recognise the new-style projects as being C#/VB projects.

This is purely an IDE issue - the dependency validation designer doesn't know the GUIDs for the new-style C# and VB project types, so it doesn't show a gold bar to fix up the project files.
The underlying validation mechanism is just using Roslyn analyzers and linked files, both of which are fully supported in MSBuild15.

Manual workaround:

For each .Net Standard/Core project that you want to validate:

  1. Add a reference to the _Microsoft.Dependency.Validation.Analyzer_ NuGet package
  2. Add the layer diagram as a _linked file_ with the _ItemGroup_ type of _AdditionalFiles_

e.g.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <!-- Add support for Layer validation -->
  <ItemGroup>
    <PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.9.0" />

    <!-- Include the layer model as an additional file that will be passed to the analyzer />
    <AdditionalFiles Include="..\LayerTest1\DependencyValidation1.layerdiagram">
      <Link>DependencyValidation1.layerdiagram</Link>
      <Visible>False</Visible>
    </AdditionalFiles>
  </ItemGroup>

</Project>

Background

The dependency validation engine was re-written in VS2017 to use Roslyn analyzers, rather than a separate command line executable. One implication of this is that every code project being validated now needs to have a reference to the NuGet package containing the analyzers, and also a pointer to the layer model file. This needs to be added as a _linked_ file so there is only one copy of the model that is shared between all projects, and it needs to belong to the _AdditionalFiles_ ItemGroup, as this is the mechanism used by Roslyn to pass additional configuration to analyzers.

(Aside: another implication is that the only way to reliably see all dependency validation errors in the IDE is to rebuild the solution. Otherwise, VS will try not build projects that haven't changed, so the Roslyn analyzers won't be called -> no validation errors produced).

"Projects need to be updated" gold bar

If a solution contains a dependency validation diagram, the dependency validation designer in the Visual Studio IDE will scan the solution for project types it recognises as containing C# or VB code, and check that those projects contain the analyzer NuGet reference and the link to the model file. If they don't then the "_[o]ne or more projects needs to be updated to perform dependency validation_" gold bar will be shown.

However, the dependency validation designer only knows about the old-style C# and VB project types (GUIDs _FAE04EC0-301F-11D3-BF4B-00C04F79EFBC_ and _F184B08F-C81C-45F6-A57F-5ABD9991F28F_ respectively). It doesn't know about the new-style C# and VB projects (_9A19103F-16F7-4668-BE54-9A1E7A4F7556_ and _778DAE3C-4631-46EA-AA77-85C1314464D9_) because they were released after VS2017 RTM.

It should be a straightforward fix to add support for the additional GUIDs.

All 13 comments

@GregE123456 thanks, I will try to find out the answer for you.

Topic: https://docs.microsoft.com/en-us/visualstudio/modeling/layer-diagrams-guidelines

@jmprieur would you be able to help out with this question?

I'm running into the same issue in our solution -- .NET Core/Standard projects aren't seeing their dependencies validated. Is it because the current modeling SDK doesn't add the necessary NuGet packages & diagram links to those project types?

@BijuVenugopal can you provide any insight into why @GregE123456 and @LarsKemmann aren't seeing the expected validation errors in layer diagrams for .NET Core projects?

+1... exact same issue, been there for months... is there a point when MSFT will start supporting their own tooling on what appears to be their own framework? This is silly... nothing in the enterprise dev stack works w/ net core... If you want unit tests, need to modify project xml so tools do what's needed, if you want dependency validation, it doesn't work...

GET YOUR ACT TOGETHER!!!!

Having the same issue. @gewarren or @BijuVenugopal, any thoughts/fixes for this ?

@GregE123456: My apologies for the delayed response.

We have removed support for dependency validation/layer diagrams from Visual Studio 2017. While removing support for this feature has been a hard decision, we want to ensure that our resources are invested in features that deliver the most customer value. If you are a significant user of the dependency validation, you can continue to use Visual Studio 2015 or earlier versions, whilst you decide on an alternative tool for your needs. Thanks for writing to us

@subsri Are you sure that's correct? Can you please point us to a blog post that mentions this? I know that the UML modeling projects were removed, but dependency validation is still very much supported in VS 2017 as of the last time I checked. Exhibit A: it works fine for full .NET Framework projects. :)

@LarsKemmann you're right: dependency validation works fine for old-style MSBuild projects, and the only reason it appears not to work for new-style MSBuild projects is that dependency validation designer doesn't add the NuGet package and the link to the model file because it doesn't recognise the new-style projects as being C#/VB projects.

This is purely an IDE issue - the dependency validation designer doesn't know the GUIDs for the new-style C# and VB project types, so it doesn't show a gold bar to fix up the project files.
The underlying validation mechanism is just using Roslyn analyzers and linked files, both of which are fully supported in MSBuild15.

Manual workaround:

For each .Net Standard/Core project that you want to validate:

  1. Add a reference to the _Microsoft.Dependency.Validation.Analyzer_ NuGet package
  2. Add the layer diagram as a _linked file_ with the _ItemGroup_ type of _AdditionalFiles_

e.g.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <!-- Add support for Layer validation -->
  <ItemGroup>
    <PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.9.0" />

    <!-- Include the layer model as an additional file that will be passed to the analyzer />
    <AdditionalFiles Include="..\LayerTest1\DependencyValidation1.layerdiagram">
      <Link>DependencyValidation1.layerdiagram</Link>
      <Visible>False</Visible>
    </AdditionalFiles>
  </ItemGroup>

</Project>

Background

The dependency validation engine was re-written in VS2017 to use Roslyn analyzers, rather than a separate command line executable. One implication of this is that every code project being validated now needs to have a reference to the NuGet package containing the analyzers, and also a pointer to the layer model file. This needs to be added as a _linked_ file so there is only one copy of the model that is shared between all projects, and it needs to belong to the _AdditionalFiles_ ItemGroup, as this is the mechanism used by Roslyn to pass additional configuration to analyzers.

(Aside: another implication is that the only way to reliably see all dependency validation errors in the IDE is to rebuild the solution. Otherwise, VS will try not build projects that haven't changed, so the Roslyn analyzers won't be called -> no validation errors produced).

"Projects need to be updated" gold bar

If a solution contains a dependency validation diagram, the dependency validation designer in the Visual Studio IDE will scan the solution for project types it recognises as containing C# or VB code, and check that those projects contain the analyzer NuGet reference and the link to the model file. If they don't then the "_[o]ne or more projects needs to be updated to perform dependency validation_" gold bar will be shown.

However, the dependency validation designer only knows about the old-style C# and VB project types (GUIDs _FAE04EC0-301F-11D3-BF4B-00C04F79EFBC_ and _F184B08F-C81C-45F6-A57F-5ABD9991F28F_ respectively). It doesn't know about the new-style C# and VB projects (_9A19103F-16F7-4668-BE54-9A1E7A4F7556_ and _778DAE3C-4631-46EA-AA77-85C1314464D9_) because they were released after VS2017 RTM.

It should be a straightforward fix to add support for the additional GUIDs.

@LarsKemmann , @GregE123456 : I am sorry for the confusion in my message. While Dependency validation/Layered diagrams are supported for .Net Framework in VS 2017, they are not supported for .Net core projects in any of the VS versions. I agree that currently it works for C#, Visual Basic and C# projects in VS 2017. What I meant was there is no official support for DV in .Net Core projects. I hope this clarifies. Please feel free to use the workaround if it works for you.

@duncanp-sonar: Thanks for sharing a solution.

At least someone add a big info box saying dotnet core it is not supported:
https://docs.microsoft.com/en-us/visualstudio/modeling/validate-code-with-layer-diagrams?view=vs-2017

After using the workaround listed (and setting the <TargetFrameworkVersion /> to "v4.7.2"), has anyone else run into an error like this? Anyone know a way to suppress it?

image

External tools such as Softagram and NDepend will help you do the job, even for .NET Core projects.

Was this page helpful?
1 / 5 - 1 ratings