在Asp.net Core中使用StyleCop

pej*_*man 5 stylecop asp.net-core

根据这篇文章在Asp.net Core中使用StyleCop,

1)将以下内容添加到project.json文件的dependencies部分:

 "StyleCop.Analyzers": {
  "version": "1.0.0",
  "type": "build"
}
Run Code Online (Sandbox Code Playgroud)

并构建项目.

2)创建stylecop.json并添加你的配置,这是我的stylecop.json内容:

{
   "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
   "settings": {
     "documentationRules": {
       "documentInterfaces": true,
       "documentInternalMembers": false    
   }
  }
 }
Run Code Online (Sandbox Code Playgroud)

3)project.json文件中的buildOptions节点下面的以下内容:

  "additionalArguments": ["/additionalfile:path/to/stylecop.json" ]
Run Code Online (Sandbox Code Playgroud)

但是我遇到了一些错误:问题是什么?

在此输入图像描述

Sha*_*tin 2

简答

NullReferenceExceptionStyleCop 找不到您的stylecop.json. 这是相关的 GitHub 问题

要修复它(假设您stylecop.json位于项目的根目录中),请将路径更改为:

"additionalArguments": [
  "/additionalfile:./stylecop.json"
]
Run Code Online (Sandbox Code Playgroud)

完整的工作示例

目录结构

bin
obj
Program.cs
project.json
project.lock.json
stylecop.json
StyleCop.ruleset
Run Code Online (Sandbox Code Playgroud)

项目.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"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

stylecop.json

{
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "documentExposedElements": false,
      "documentInterfaces": false
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

StyleCop.规则集

<?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>
Run Code Online (Sandbox Code Playgroud)

克隆并运行示例

git clone git@github.com:bigfont/StackOverflow.git
cd .\StackOverflow\AspNetCoreStyleCop\
dotnet restore
dotnet build
Run Code Online (Sandbox Code Playgroud)