Using StyleCop in Asp.net Core

我是研究僧i 提交于 2019-12-05 10:40:49

Short Answer

Those NullReferenceException errors happen when StyleCop cannot find your stylecop.json. Here is the related GitHub issue.

To fix it (assuming that your stylecop.json is in the root of your project) change the path to this:

"additionalArguments": [
  "/additionalfile:./stylecop.json"
]

Full Working Example

Directory structure

bin
obj
Program.cs
project.json
project.lock.json
stylecop.json
StyleCop.ruleset

Project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true,
    "warningsAsErrors": true,
    "nowarn": [
      "1591"
    ],
    "xmlDoc": true,
    "additionalArguments": [
      "/ruleset:./StyleCop.ruleset",
      "/additionalfile:./stylecop.json"   <----- This is probably the problem.
    ]
  },
  "dependencies": {
    "StyleCop.Analyzers": {
      "type": "build",
      "version": "1.0.0"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        }
      }
    }
  }
}

stylecop.json

{
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "documentExposedElements": false,
      "documentInterfaces": false
    }
  }
}

StyleCop.ruleset

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="14.0">
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA0000" Action="Warning" />
    <Rule Id="SA1005" Action="Warning" />
    <Rule Id="SA1208" Action="Warning" />
    <Rule Id="SA1028" Action="Warning" />
    <Rule Id="SA1210" Action="Warning" />
  </Rules>
</RuleSet>

Clone and Run the Example

git clone git@github.com:bigfont/StackOverflow.git
cd .\StackOverflow\AspNetCoreStyleCop\
dotnet restore
dotnet build
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!