173

I am getting this error:

The type or namespace name 'AutoMapper' could not be found (are you missing a using directive or an assembly reference?)

The funny thing is that I have that reference in my project already:

ProjectThatFails

And this is my code:

using System.Collections.Generic;
using DataContract;
using SelectorDAL;
using AutoMapper;

namespace SpecimenSelect
{
    public class SpecimenSelect : ISpecimenSelect
    {
        public SpecimenSelect()
        {
            SetupMaps();
        }

        private static void SetupMaps()
        {
            Mapper.CreateMap<SpecimenDetail, SpecimenDetailContract>();
        }

The other weird thing is that I have two other projects in my solution that both use AutoMapper and are referencing the exact same AutoMapper.dll file. They both work perfectly fine.

Here is a screen shot of one:

ProjectThatWorks

and here is that code (that compiles fine):

using System.Collections.Generic;
using AutoMapper;
using DataContract;
using SelectorDAL;

namespace PatientSelect
{

    public class PatientSelect : IPatientSelect
    {
        public PatientSelect()
        {
            SetupMaps();
        }

        private void SetupMaps()
        {
            Mapper.CreateMap<Patient, PatientContract>();
            Mapper.CreateMap<OrderedTest, OrderedTestsContract>();
            Mapper.CreateMap<Gender, GenderContract>();
        }

Both references seem to have the same data on the properties page.

What am I missing?

I tried:

  1. Restarting Visual Studio
  2. Referencing without a using statement (ie AutoMapper.Mapper.CreateMap)
  3. Clean and Rebuild

Any other ideas?

Vaccano
  • 78,325
  • 149
  • 468
  • 850

21 Answers21

279

Check to make sure that your project isn't set up to use the .NET Framework 4 Client Profile.

You can check/change this by right-clicking your project (not the solution), select Properties -> Application -> Target framework. The target framework is a dropdown on that page.

This is a problem in Visual Studio (I would even go so far as to call it a bug). AutoMapper requires assemblies that are excluded from the .NET Framework 4 Client Profile. Since your project is using that version of the framework it breaks.

A similar error will propagate to the build process when the .NET Framework version for the project you are referencing is higher than the project making the reference. i.e. A project targeting 4.5 that references a project targeting 4.5.1 will give you this same error.

There needs to be a better error message when this happens because there is no rational explanation as to why it would not build as the error message tells you to reference an assembly you have clearly referenced.

Matt
  • 1,674
  • 2
  • 16
  • 34
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 7
    This was exactly what the problem was! Thanks you! I agree that this error is very misleading. I also don't get why the Client Profile is the default for a new project. Most computers are going to have the full .net framework right? (Or does MS just put the client framework on Windows Update?) Anyway, all the computers I develop for will have the full framework. I wish there was a way to change the default for a new project so it does bite me like this did. Anyway. Thanks again! I was stuck and I did not think to look there. – Vaccano Nov 19 '10 at 20:33
  • I had exactly the same problem! The types were not recognized in my windows service project even if I had added the references correctly. I changed the target framework from .NET Framework 4 Client Profile to .NET Framework 4. Note that I also had to re-add my references to make it compile. Seems to be an issue in Visual Studio 2010. Thanks and greetings from Budapest. – Varga Tamas Apr 14 '11 at 09:27
  • Good one, this also happened to me. My test project did not recognize referenced namespaces even though they were there. This was because I changed my library to .NET 4.5 platform but test project remained as 4.0. In any case, your answer lead me on the right track. Thanks, for figuring this out. – Jukka Puranen Apr 14 '13 at 10:56
  • I faced same issue when i tried to use .Net 2.0 assembly in .Net 3.5 client profile project. and switching to 3.5 full profile solve the issue. – Palani Jun 13 '14 at 06:01
  • My issue was similar. The projects were using different Framework versions. The main project was using 4.5 and the newly created project was using 4.5.1. It would be nice if the error message was better. – L_7337 Oct 12 '15 at 15:31
  • Thanks for the answer! I came across this answer while trying to figure out why I had the same error (which disapeared if I tried to view it) and that was kind of my problem. I was targeting version 4.5 in every project except one (which was 4.5.1). I changed it to 4.5 and it builds perfectly now! – Marie Mar 29 '16 at 17:02
  • The best way to locate this problem is to check the Warnings after the build completes. You will get it in the form of referenced [Project Name] was built with .NET Framework 4.5.2 but the current [Project Name] is targeting .NET Framework 4. If you see this warning you know what the problem is :). – Siva Senthil May 12 '17 at 10:58
  • problem solved, mine was an issue related to a few of the layers build framework being set above the main projects build version. rebuilding after setting all to the same version made it work. – Niklas May 15 '17 at 11:39
  • I've found as well that had one project with .net 2.00 framework, which referenced some projects of .net 3.5 framework and was issues with this.. Then just set same framework version on all of them and it compiled without problems. Maybe this will help for someone. –  Jul 04 '17 at 12:36
  • My solution have 5 projects, all of them compiling to 4.5.2, and sometimes one of them failed to compile. It´s Visual Studio (2013 in my case). Sadly, there is not a good (solid) replacement, having into account the plugins. – marcelo Jul 28 '17 at 22:25
  • My problem was different .NET Framework versions of some project in the solution... – koviroli Jul 05 '18 at 07:55
44

This has to be the simplest solution if all the other answers does not help you

I was searching for what's wrong with my setup among the answers, Tried all of them - none worked, Then I realized Visual Studio 2018 was developed by Microsoft. So I did what most people do,

Restarted Visual Studio And It worked

insomniac
  • 11,146
  • 6
  • 44
  • 55
  • 2
    Worked for me but deleted bin and obj folder before restarting. – Chandan Y S Oct 14 '19 at 21:26
  • 7
    In all my years on stackoverflow, this is the best salty response to a bug (that actually provides a solution) and it worked on the first restart. insomniac ftw! – Collin White Jun 29 '20 at 15:39
  • I ran into this when renaming / moving projects around, simply restarting visual studio did not work. But when I deleted all output files from the project(s) involved (in the local obj directory and the output directory). Also removed the reference to the problem assembly. Restarted Visual Studio, added the reference back and it worked. – trevorcroft May 30 '22 at 08:19
28

Let me ask a stupid question: Could there be two automapper.dll files? One with an AutoMapper namespace and one without? Confirm the paths in both projects.

I also noticed that the order of the using commands is different. It shouldn't matter, but have you tried to shuffle them?

honk
  • 9,137
  • 11
  • 75
  • 83
n8wrl
  • 19,439
  • 4
  • 63
  • 103
20

If your class does not compile, even if it is in the project check these:

  1. whether class name is exactly the same
  2. whether name space is exactly the same
  3. whether class properties show build action = compile
Anonymous
  • 201
  • 2
  • 2
  • 6
    I copied a .cs file with Explorer and then included it in the project. VS.Net set the build action as "Content" instead of "Compile" and so wasn't recognising the namespace. Good catch! – AUSteve Mar 10 '13 at 11:31
  • 1
    I added the class through the Add -> Class feature, and it set the build action to content. Just curious as to why? This helped me anyway, something so simple, but never before encountered. Which is why I didn't even bother looking there, and googled it instead. – Stuyvenstein Feb 17 '17 at 09:58
16

I resolved this issue by right clicking on the folder containing the files and choosing Exclude From Project and then right clicking again and selecting Include In Project (you first have to enable Show All Files to make the excluded folder visible)

Florian Winter
  • 4,750
  • 1
  • 44
  • 69
user3251328
  • 188
  • 1
  • 8
  • I had this error for tons of files, just removing one and re-including it resolved the issue for the whole solution. – Daryl Sep 14 '18 at 18:51
  • Right-clicking on what? "Exclude from project" removes the folder from solution explorer. There is no "Include In Project" – Florian Winter Sep 17 '18 at 07:02
  • 2
    @FlorianWinter To right click the excluded folder you need to have **Show All Files** on. This can be toggled on via the Solution Explorer, it is found next to the **Collapse All** button. – user3251328 Sep 17 '18 at 23:54
  • Don't know why, but this worked in VS2019 when I added a new file containing a new class to a project, but could not create a new instance of that class in another project which already had a reference to the new class's containing project... `¯\_(ツ)_/¯ ` – mfcallahan Jul 30 '19 at 22:37
  • This solved for me. This is a very annoying bug on Visual Studio, and happens a lot. – jairhumberto Mar 08 '21 at 14:07
7

I have a similar problem with references not being recognized in VS2010 and the answers herein were not able to correct it.

The problem in my solution was related to the extension of the path where the project referenced was located. As I am working with SVN, I made a branch of a repository to do some testing and that branch increased two levels in path structure, so the path became too long to be usable in windows. This did not throw any error but did not recognize the namespace of the project reference. When I correct the location of the project to have a smaller path everything went fine.

onurb84
  • 71
  • 1
  • 2
  • 2
    This was an issue for us as well and it was the path length that was causing the problem. VS needs to do a better job of giving a better error in that case as the error we got was pretty misleading. – VoodooChild Nov 28 '13 at 16:54
  • Thanks to your answer an idea came to my mind. I looked at the path of my project and realized that the root folder, the one created by source tree, had "%20" instead of _ in the name. I changed it to _ and everything works fine now. – Fernando Wolff May 30 '19 at 19:04
5

In my case, the referenced dll was build in higher version of .Net Framework. After I added the reference, I could use it. But as soon as I did a build, the 'missing reference' error will pop up. I refresh the dll the error will go but it would never build. This post made me check the framework version and thus I could resolve it by building the referenced project in same version.

Ankur-m
  • 528
  • 6
  • 14
3

The question has already been awarded, but there are additional details not yet described that need to be checked.

I too was having this behavior, where project B was referenced in project A, but the namespace of project B was not recognized in project A. After some digging, I found my path was too long. By reducing the path of the projects (both A and B) the references became visible and available.

I tested this theory by creating project C at a much lesser path depth. I referenced project C in project A. The references worked correctly as expected. I then removed project C from the solution, merely moved project C to a deep path, the same as project B, and added project C back to the solution, and tried to compile. I then had no visibility to project C objects any longer.

barrypicker
  • 9,740
  • 11
  • 65
  • 79
3

Perhaps the project's type table is in an incorrect state. I would try to remove/add the reference and if that didn't work, create another project, import my code, and see if that works.

I ran into this while using VS 2005, one would expect MS to have fixed that particular problem by now though..

dave
  • 559
  • 3
  • 12
3

Restarting Visual Studio 2019 - that did it.

2

In my case i had copied a classlibrary, and not changed the "Assembly Name" in the project properties, so one DLL was overwriting the other...

2

This happened to me in Visual Studio 2019. For me, I was trying to reference another project in my solution. Here are the steps I took in case it helps anyone else:

  1. Ensured the project I wanted to reference was listed under References
  2. Ensured both projects were using the correct version of .NET Framework
  3. Built the project (clicked green "Start" arrow)

I was confused because I was still getting the error after steps 1 and 2, but building the project seemed to resolve it.

mtfalcon31
  • 37
  • 1
  • 8
1

I faced similar problem of namespace/method not being found during execution although it was fine during compilation, and the reason for this appears to be that the assembly I was referencing was deployed to GAC and since then was changed, so when I referenced the assembly in Visual Studion it was using the most recent one, but during runtime the version fro GAC had been used.

1

In my case I got the error only in VS 2015. When opening the project in VS 2017 the error was gone.

daniel
  • 34,281
  • 39
  • 104
  • 158
1

Crazy. I know.

Tried all options here. Restarting, cleaning, manually checking in generated DLLs (this is invaluable to understanding if it's actually yourself that messed up).

I got it to work by setting the Verbosity of MSBuild to "Detailed" in Options.

Program.X
  • 7,250
  • 12
  • 49
  • 83
  • for me it was a build configuration set to AnyCpu at PlatformTarget when the Configuration Name was x86. This setting helped me find it out. – Dbl Dec 05 '18 at 21:37
1

This question has already been answered for the original poster, but in case someone encounters this in an MS-Test project:

from within Visual Studio, click the Test menu -> Test Settings -> Default Processor Architecture and ensure that the architecture matches that of the other assembly that you're referencing. If the other assembly is x64 and your test settings are x86, you may experience the symptoms that the original poster had.

S. Hooley
  • 284
  • 3
  • 9
1

In my case removing/adding that assembly worked.

Mohammed
  • 637
  • 13
  • 26
0

I was working on Xamarin project and as always, deleting obj folder and rebuilding solved my issue, the namespace my VS was not recognizing was a code in my own project BTW

mohas
  • 1,911
  • 1
  • 16
  • 20
0

I've had a similar issue, that took a bit to troubleshoot, so I thought to share it:

The namespace that could not be resolved in my case was Company.Project.Common.Models.EF. I had added a file in a new Company.Project.BusinessLogic.Common namespace.

The majority of the files were having a

using Company.Project;

And then referencing the models as Common.Models.EF. All the files that also had a

using Company.Project.BusinessLogic;

Were failing as VS could not determine which namespace to use.

The solution has been to change the second namespace to Company.Project.BusinessLogic.CommonServices

MaPi
  • 1,601
  • 3
  • 31
  • 64
0

Acknowledging this as an older post but still figured I'd add this suggestion to the list of responses since I hadn't seen it mentioned.

If the unresolved reference exists within context of another Project in the Solution:

Right-click the Project having the problem, select Build Dependencies --> Project Dependencies and ensure the desired Project is selected for reference.

I had the same issue as described in the post however none of the suggested manners for resolution worked. I'd never seen such a quirk in VS before (running VS 2019 at present)

I checked for potential namespace issues and a plethora of the more obvious causes and nothing made sense. Intellisense even acknowledged the existence of the other Project and suggested to a the using statement but even after having added the using reference; VS 2019 still wouldn't acknowledge the other project.

Forcing the dependency in the manner described resolved the issue.

Mark
  • 1,667
  • 2
  • 24
  • 51
0

My solution was to delete the .vs folder from my solution folder. This resets the intellisense fixing the problem. You will need to turn on "Hidden items" (if running windows) as it's a hidden folder.

Solution found here - https://weblog.west-wind.com/posts/2018/Aug/07/Fixing-Visual-Studio-Intellisense-Errors

user2978141
  • 43
  • 1
  • 1
  • 3