1

I have just asked a similar question regarding auto generated code from Service References and Xml summary errors. An answer was posted suggesting that I could add the Service Reference into its own project and then it can avoid Xml summary warnings and Stylecop errors completely.

But I realised that I was able to get around the Xml problem by setting the Service Reference to Internal and am now stuck on the StyleCop errors.

I am using FxCop version 10.0.

In the properties of my project, in the Code Analysis tab, I have checked the option for

Suppress results from generated code (managed only)

Also, from looking at the generated code in the Reference.cs file, I see that the code is decorated with the following attribute:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]

..which Stylecop should then skip, right? Although this is not working, I am still getting StyleCop errors for the generated code.

Edit: The following header can also be found in the generated code:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18033
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

So why isn't it being ignored?

I also found this question and tried using the following in my StyleCop setings:

<CollectionProperty Name="GeneratedFileFilters">
    <Value>Reference\.cs$</Value>
 </CollectionProperty>

But this doesn't work either, I have been struggling with the regex but tried to add the following values to the above example to get Stylecop to ignore my generated code.

Reference.cs
\Reference\.cs
\Reference\.cs$
Reference\.cs$

Does anyone know how to suppress all Stylecop errors for a Reference.cs file?

Edit2: The errors being displayed for the Reference.cs file:

Error 1 CA1812 : Microsoft.Performance : ... is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static methods, consider adding a private constructor to prevent the compiler from generating a default constructor.

Error 2 CA2239 : Microsoft.Usage : Add a 'private void OnDeserialized(StreamingContext)' method to type ... and attribute it with the System.Runtime.Serialization.OnDeserializedAttribute.

Error 4 CA1811 : Microsoft.Performance : ... appears to have no upstream public or protected callers.

I have 27 of these 3 types of errors.

Edit 3: I have Style/Fx cop setup to run on build. I have added CODE_ANALYSIS in the Conditional compilation symbols in the build tab and

"$(ProgramFiles)\Microsoft FxCop 10.0\FxCopCmd.exe" /file:"$(TargetPath)" /ruleid:-"Microsoft.Design#CA1006" /ruleid:-"Microsoft.Design#CA1020" /console /cul:en-GB /q /assemblyCompareMode:StrongNameIgnoringVersion

... in the Post-build event command line in the Build Events tab.

Community
  • 1
  • 1
DevDave
  • 6,700
  • 12
  • 65
  • 99
  • Can you please detail what errors you are seeing? – Mightymuke Feb 14 '13 at 20:57
  • @Mightmuke, see edit 2 – DevDave Feb 15 '13 at 11:08
  • It seems to work fine for me when I select the `Suppress results from generated code` option (although I don't have that text in the brackets, I'm wondering how and why that's there). Are you running this from the IDE (aka Code Analysis) or from the FxCop runner? If from the IDE, do you have several projects? There is an issue when running from the IDE in that the code analysis settings from the first project override the settings for a 'sub' project (at least in VS2010). This of course wont be an issue if you only have one project. – Mightymuke Feb 15 '13 at 18:03

1 Answers1

5

The CAxxxx errors are from FxCop, not StyleCop. If they are appearing for generated code, and you want to keep the generated code without changing the generator, simply add project-level exclusions (e.g.: in GlobalSuppressions.cs).

Following question edit #3: Since you're running fxcopcmd.exe "manually", your C# project setting for ignoring generated code isn't being used at all. You'll need to add the /ignoregeneratedcode switch to your fxcopcmd.exe command line to get this to be applied.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • Is there no way, to skip the generated file with FxCop rather than having to write suppressions for each error? – DevDave Feb 15 '13 at 15:37
  • If you've already set FxCop to ignore generated code, then you've pretty much done all you can from that end. I can't repro the problem, but I can't tell if that's because my service ref is different or your FxCop config is wrong. How are you running FxCop when you see the CA warnings? – Nicole Calinoiu Feb 15 '13 at 15:42
  • Is there a different way to ignore generated code for fxCop than the examples I have included? – DevDave Feb 15 '13 at 16:14
  • That depends on how you are running FxCop. If you would answer the question of how you are running it, it would go a long way to indicating whether setting the option in VStudio is sufficient. – Nicole Calinoiu Feb 15 '13 at 18:43