37

I'm trying to use System.DirectoryServices in a web site project and I'm getting this error:

The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)

My project has a reference to System.DirectoryServices in web.config:

<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>  

And I do have using System.DirectoryServices in the files where I want to use it.

Does anybody have a clue where to look for the problem?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
jansokoly
  • 1,994
  • 2
  • 18
  • 25

9 Answers9

113
  1. Right click on References under your solution.
  2. Select Add Reference. The reference can be found under the Framework Assemblies list. Select System.DirectoryServices and click Add.
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
user1947015
  • 1,131
  • 1
  • 7
  • 2
10

I think you should install Directory Services Package.

Install-Package System.DirectoryServices -Version 4.0.0 

Directory Services Package

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Basma Elshoky
  • 151
  • 1
  • 10
8

Shot in the dark: have you tried adding to the web.config:

<compilation debug="true">
     <assemblies>
          <add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
     </assemblies>
</compilation>
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • what about any assemblies that System.DirectoryServices is dependent upon? – Mitch Wheat Nov 05 '08 at 13:42
  • 1
    The PublicKeyToken might need to be lower case, I think it might be case-sensitive -- I didn't have this line, and I just added it, with a copy/paste of the PublicKeyToken from the properties of my DLL in C:\Windows\Assembly (aka GAC) and that's what solved it, for me. – vapcguy Nov 06 '14 at 23:24
  • Even after adding the necessary references when I build the solution it got succesfully build and when I ran the project then I got the error as _CS0234: The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)_ I didnt got why I got the error again after successsful build – Nad Mar 16 '17 at 07:03
7

Is the web-server (IIS or whatever) configured to run the folder as an application (i.e. shows as a cog), and is it using the correct version of ASP.NET? If it is running as 1.1, bits of it might work - but it would fail to find that 2.0 assembly in the 1.1 GAC.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
7

This is a very old thread but just to provide a complete answer for the sake of posterity ;)

This issue occurs if the project is missing a reference to the .Net Component System.DirectoryServices

Adding this reference in the usual manner prefered by you will resolve the issue.

Salman Siddiqui
  • 340
  • 3
  • 13
1

On Solution Explorer right-click your project, then from the resulting menu, click on Add Reference, then under the .NET tab navigate to DirectoryServices.AccountManagement

1

Is this a web site project, or a web application project. With the latter, references are handled via the .csproj - i.e. via the "References" node in Solution Explorer.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

These problems occur when you are working with older .net version and trying to build with the latest IDE

It depends on which version of IDE you are using and also the current code version.

Check the web config,

In my case, I was using the Latest version i.e 4.7 and directoryService assembly are still referring to C#4.0.

Add below if you are using Latest version of id i.e 4.7

  <system.web>
  <location>

<compilation debug="false" numRecompilesBeforeAppRestart="100" targetFramework="4.7">
        <assemblies>
<add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
 </assemblies>
      </compilation>
</system.web>
  </location>
Liam
  • 27,717
  • 28
  • 128
  • 190
Pratheek S
  • 11
  • 1
  • 3
0

I had the same problem when I tried to convert website to web-app. It looks like vs failing to load the assembly should be related to versioning. switch to web.config and add the assembly to it as bellow. make sure the DLL version is matching your application targeted .NET version.

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </assemblies>
    </compilation>
  </system.web>
</configuration>

for getting a public key you need to launch Developer Command Prompt for VS. Change to GAC directory related framework on above ex C:\Windows\Microsoft.NET\Framework\v4.0.30319 and call

sn -T System.DirectoryServices.dll
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90