在Asp.net Core中使用StyleCop [英] Using StyleCop in Asp.net Core

查看:170
本文介绍了在Asp.net Core中使用StyleCop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此本文,以在Asp中使用StyleCop .net核心,

According to this this article for using StyleCop in Asp.net Core,

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

1)Add the following to the dependencies section of the project.json file:

 "StyleCop.Analyzers": {
  "version": "1.0.0",
  "type": "build"
}

并构建项目.

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

2) Create stylecop.json and add your configuration on it, this is my stylecop.json content:

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

3)在project.json文件中的buildOptions节点下进行以下操作:

3) the following under the buildOptions node in the project.json file:

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

但是我遇到了一些错误:问题出在哪里?

but i got some errors: what is the problem?

推荐答案

简短回答

当StyleCop找不到您的stylecop.json时,会发生这些NullReferenceException错误.这是相关的GitHub问题.

Short Answer

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

要对其进行修复(假设您的stylecop.json位于项目的根目录中),请更改以下路径:

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

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

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

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>

克隆并运行示例

git clone git@github.com:bigfont/StackOverflow.git
cd .\StackOverflow\AspNetCoreStyleCop\
dotnet restore
dotnet build

这篇关于在Asp.net Core中使用StyleCop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆