Enabling StyleCop.Analyzers on ASP.NET Core projec

2019-05-25 06:52发布

问题:

According to the docs I've read online all you need to do is add the following to the dependencies in project.json:

  "version": "1.0.0-*",

  "dependencies": {
    "DependencyA": "1.0.0-*",
    "DependencyB": "1.0.0-*",
    "StyleCop.Analyzers": {
      "version": "1.0.0",
      "type": "build"
    }
  },

  "frameworks": {
    "net46": { }
  }

(where DependencyA and DependencyB are two .NET Core projects from the same solution) and then StyleCop.Analyzers will run with the default rule set upon building the project. It doesn't. I get no warnings or anything and I'm pretty sure the code at the moment violates a lot of rules. Did anyone successfully get this working?

回答1:

My .NET Core version was apparently too old. After upgrading to the latest one it now works.



回答2:

I was able to get this working with Visual Studio 2015 Update 3 using the following steps:

  1. Create a new project from the ASP.NET Core Application (.NET Core) template.
  2. Add the StyleCop.Analyzers build dependency as you described in your question.
  3. Build the project in Visual Studio and make sure the Error List output is set to Build + IntelliSense.

This is what I get:

I've confirmed that this also works for projects targeting net451 instead of netcoreapp1.0, as well as Class Library projects targeting netstandard1.X.