3

I am having difficulty persuading FxCop 10.0 to analyse assemblies that reference AutoMapper.

I have created a simple class library, referenced AutoMapper via NuGet, and added the following code:

using System;

namespace ClassLibrary4
{
    public class Class1
    {
        public void Foo()
        {
            AutoMapper.Mapper.CreateMap<Obj1, Obj2>()
                      .ForMember(x => x.Name, opt => opt.Ignore());
        }
    }

    public class Obj1
    {
        public string Name { get; set; }
    }

    public class Obj2
    {
        public string Name { get; set; }
    }
}

I then tried to use FxCop 10.0 to analyse the assembly via the command line, and receive the message:

Could not load C:\Users\inelson\Documents\Visual Studio 2013\Projects\ClassLibrary4\ClassLibrary4\bin\Debug\ClassLibrary4.dll.

NOTE: One or more referenced assemblies could not be found. Use the '/directory' or '/reference' switch to specify additional assembly reference search paths.

Unresolved reference is to System.Core Version 2.0.5.0.

In an effort to isolate the issue, I removed the .ForMember method call, leaving Foo() as simply:

public void Foo() 
{
    AutoMapper.Mapper.CreateMap<Obj1, Obj2>(); 
}

and FxCop 10.0 now happily analyses the assembly!

What is it with the .ForMember method that is causing the FxCop analysis to fail?

Note that I am experiencing the same behaviour with .NET Framework versions 4.0, 4.5 or 4.5.1, and AutoMapper 3.0.0 and 3.1.0.

Vladimir Almaev
  • 2,358
  • 1
  • 28
  • 38
Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
  • Use Fuslogvw.exe to troubleshoot assembly resolution problems. – Hans Passant Dec 15 '13 at 18:22
  • 1
    @HansPassant I've tried that, no failed bindings are showing. – Ian Nelson Dec 15 '13 at 20:28
  • 1
    Please, provide output from command FxComCmd.exe /file:ClassLibrary4.dll /console /v . From your question isn't obvious which exactly assembly couldn't be found. Usually, this error looks like that: Could not load 'Library.dll'. The following error was encountered while reading module 'Library': Assembly reference cannot be resolved: 'dependencyStrongName'. – Vladimir Almaev Dec 16 '13 at 10:46
  • At the risk of sounding stupid, FX10 seems to be old. Does it care about lambda expressions? – shahkalpesh Dec 16 '13 at 10:57
  • Thanks @VladimirAlmaev - having run FxCop in verbose mode, I can see that the unresolved reference is to System.Core Version 2.0.5.0. I think I am experiencing the issue described in this AutoMapper GitHub thread - https://github.com/AutoMapper/AutoMapper/issues/383 – Ian Nelson Dec 16 '13 at 12:35
  • Long shot.. but have you tried adding a using statement for the AutoMapper extensions namespace? – andycwk Dec 13 '13 at 16:06
  • Not sure which namespace you mean Andy - there is no AutoMapper.Extensions namespace as far as I can see. ForMember isn't an extension method, and it is in the root AutoMapper namespace. – Ian Nelson Dec 16 '13 at 08:58

1 Answers1

8

Make sure that AutoMapper.dll exists in output directory (in your case it is C:\Users\inelson\Documents\Visual Studio 2013\Projects\ClassLibrary4\ClassLibrary4\bin\Debug)

UPDATE: It is a well known problem with Portable Libraries and FxCop. Please, see answers:

This problem also discussed in AutoMapper bugtracker.

UPDATE 2: And I'm pretty sure that problem appears because FxCop standalone version doesn't support .NET 4.5 and Portable Class Libraries. You can vote for appropriate suggestion on Visual Studio User Voice.

Did you try to run code analysis via Visual Studio?

UPDATE 3: I try to run 11.0.50727.1 version of FxCopCmd.exe which installed with Visual Studio 2012 (I found it at C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop) and it seems to work.

Also, I try to run Code Analysis in Visual Studio 2012 and it also works.

Community
  • 1
  • 1
Vladimir Almaev
  • 2,358
  • 1
  • 28
  • 38
  • Thanks Vladimir, I have also successfully analysed the assembly using FxCop 11 and FxCop 12. It seems these are not available as a standalone installation, I will see whether Visual Studio 2012/2013 can be installed on the build server. – Ian Nelson Dec 16 '13 at 13:33