question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeLoadException with .NET Core RC4 and malformed stylecop.json

See original GitHub issue

I added StyleCop.Analyzers to my project and managed to manually add a ruleset file to the csproj.

Creating the project

md test
cd test
dotnet new console

The ruleset file

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="WebDAV-Server Ruleset" Description=" Rules for the WebDAV-Server project" ToolsVersion="15.0">
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA0001" Action="None" />
    <Rule Id="SA1101" Action="None" />
    <Rule Id="SA1300" Action="None" />
    <Rule Id="SA1309" Action="None" />
    <Rule Id="SA1400" Action="None" />
    <Rule Id="SA1503" Action="None" />
    <Rule Id="SA1600" Action="None" />
    <Rule Id="SA1633" Action="None" />
  </Rules>
</RuleSet>

The project file

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <CodeAnalysisRuleSet>test.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

  <ItemGroup>
    <AdditionalFiles Include="stylecop.json" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta001">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

The stylecop.json file

This file is malformed, because comments aren’t allowed in the project.json file.

{
  // ACTION REQUIRED: This file was automatically added to your project, but it
  // will not take effect until additional steps are taken to enable it. See the
  // following page for additional information:
  //
  // https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md

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

Commands

dotnet restore
dotnet build

Output

Microsoft (R) Build Engine version 15.1.545.13942
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(1,1): warning SA1200: Using directive must appear within a namespace declaration [C:\temp\test\test.csproj]
CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.SpecialRules.SA0002InvalidSettingsFile' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'. [C:\temp\test\test.csproj]
  test -> C:\temp\test\bin\Debug\netcoreapp1.0\test.dll

Build succeeded.

Program.cs(1,1): warning SA1200: Using directive must appear within a namespace declaration [C:\temp\test\test.csproj]
CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.SpecialRules.SA0002InvalidSettingsFile' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'. [C:\temp\test\test.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.02

Expected output

Either a good error message that the project.json is invalid or accepting the json file and not throwing the TypeLoadException

Additional information

The error doesn’t appear when run from inside VS 2017.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
sharwellcommented, May 29, 2017

This should be fixed with 1.0.2.

3reactions
cdroulerscommented, Mar 17, 2017

@vweijsters There doesn’t seem to be a due date for 1.0.1 or 1.1.0-beta2. When can we expect it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using StyleCop in Asp.net Core
Short Answer. Those NullReferenceException errors happen when StyleCop cannot find your stylecop.json . Here is the related GitHub issue.
Read more >
Enabling stylecop.json
At this time, the code fix is not able to fully configure the newly-created stylecop.json file for use. This is tracked in bug...
Read more >
How to select certain rules to implement with StyleCop and ...
net core 1.1 web application and I have added stylecop.analyzers 1.0.0. How can I disable / deselect certain rules? Currently a lot of ......
Read more >
C#, PHP, C++, iOS, MySQL, SQL, ASP.NET, Objective-C, ...
NET, Objective-C, C, SQL Server, Ajax, JSON, XML, ASP. ... 2d, MS Access 2007, MonoDroid, Entity Framework 5, out-of-memory, closures, Scrapy, TortoiseSVN, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found