Runtime: System.IO.FileNotFoundException System.DirectoryServices.AccountManagement

Created on 6 Oct 2017  ·  30Comments  ·  Source: dotnet/runtime

@CarlosOnline commented on Fri Sep 15 2017

System.DirectoryServices.AccountManagement won't load from asp.net Core 2.0 (netcoreapp2.0) website

Referencing .net 4.5.1 framework class library from asp.net Core 2.0 (netcoreapp2.0), where class library references System.DirectoryServices.AccountManagement leads to receives FileNotFound exception.

System.IO.FileNotFoundException occurred
HResult=0x80070002
Message=Could not load file or assembly 'System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
Source=
StackTrace:
at SUR.Util1.ActiveDirectory.GetGroupNames(String userName) in combined\Util1\ActiveDirectory.cs:line 33
at spa.Controllers.HomeController.Index() in combined\spa\Controllers\HomeController.cs:line 15
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

Repro

Problem solution uploaded to DotNetCore2DirectoryServicesBug

Manual Repro

dotnet new aurelia
npm install
add new Utils .Net Framework 4.51 project _(version doesn't matter)_
add class below to Utils .Net Framework 4.51

    public class ActiveDirectory
    {
        public static List<string> GetGroupNames(string userName)
        {
            var result = new List<string>();

            if (string.IsNullOrEmpty(userName))
                return result;

            var domain = userName.Split('\\').FirstOrDefault();

            var pc = new PrincipalContext(ContextType.Domain, domain);
            var identity = UserPrincipal.FindByIdentity(pc, userName);
            if (identity != null)
            {
                var src = identity.GetGroups(pc);
                src.ToList().ForEach(sr => result.Add(sr.SamAccountName));
            }
            return result;
        }

    }

add Utils project reference to asp.net core app project
update HomeController.cs

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            var groups = ActiveDirectory.GetGroupNames(User.Identity.Name);

            return View();
        }
    }

start debugging

area-System.DirectoryServices bug

Most helpful comment

Hey @CarlosOnline and @avantidesaigit here is a sample project that contains a .NET Framework Library that uses System.DirectoryServices.AccountManagement and a .NET Core 2.0 App that has a project reference to the .NET Framework Library and calls the API that uses AccountManagement.

The .NET Core 2.0 App needs to have the PackageReference to System.DirectoryServices.AccountManagement in order to find the assembly, if not you would get the FileNotFoundException. As you can see here.

Note that the .NET Framework Library has to include a reference to System.DirectoryServices.AccountManagement

The System.DirectoryServices.AccountManagement version that I'm referencing is coming from NuGet.
https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.5.0-preview1-25914-04

Also a few weeks ago we published a compatibility package that includes references to various packages that where ported from .NET Framework, including System.DirectoryServices.AccountManagement you can find more about it in this blog post

Please let me know if this fixed your issue or if you have any questions :)

All 30 comments

This is expected as System.DirectoryServices.AccountManagement is not supported on .NET Core today. There is work in progress to support parts of System.DirectoryServices. @danmosemsft can you give any more details on which parts and when?

The goal is all the API in these ref files, which I believe is all of directory services:

https://github.com/dotnet/corefx/blob/master/src/System.DirectoryServices/ref/System.DirectoryServices.cs
https://github.com/dotnet/corefx/blob/master/src/System.DirectoryServices.Protocols/ref/System.DirectoryServices.Protocols.cs
https://github.com/dotnet/corefx/blob/master/src/System.DirectoryServices.AccountManagement/ref/System.DirectoryServices.AccountManagement.cs

@tarekgh is there any API here that you believe we can not have working on schedule?

This is Windows only support: although a future Unix port is possible for some parts (discussed here)

My understanding is all APIs already there including the AccountManagement namespace.

CC @tquerec as he is the one ported the whole namespaces to .net core.

I think this can be the problem of trying to load the desktop version of the assembly and not the net core version.

CC @weshaggard @AlexGhiondea if they have input about that

Do we need any types of facades to map these things to .NET Framework?

@Petermarcu yes, the build needs to create such facades; we will include them in the compat package (actually a separate package referenced by the metapackage). @tarekgh I see that directory services isn't making facades right now. That's easy to fix. I'll open a bug.

Do we need any types of facades to map these things to .NET Framework?

No we don't need a facade this assembly already exists with the same identity in the .NET Framework.

Based on the description what is needed is a reference to our .NET Core package as they are trying to consume a net451 library on .NET Core.

Is there any resolution this issue?

@CarlosOnline did you try what @weshaggard suggested?

Thanks that worked.
Install-Package System.DirectoryServices in the dotnet core 2.0 project worked.

This is not intuitive, since the net451 is already referencing the dll.

@CarlosOnline your app is netcore app and not full framework app, so it makes sense to load the net core bits and not the full framework bits. This is similar to any other core types (e.g. String for instance) which expected to load the net core type and not the full framework.

CC @safern

Sorry it fixed one issue but not the original issue. Still getting the
missing dll. I'll update the solution with the Install-Package.
https://github.com/CarlosOnline/DotNetCore2DirectoryServicesBug
Should I open a new bug?

~Carlos~

On Fri, Oct 27, 2017 at 8:29 AM, Tarek Mahmoud Sayed <
[email protected]> wrote:

@CarlosOnline https://github.com/carlosonline your app is netcore app
and not full framework app, so it makes sense to load the net core bits and
not the full framework bits. This is similar to any other core types (e.g.
String for instance) which expected to load the net core type and not the
full framework.

CC @safern https://github.com/safern

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/corefx/issues/24488#issuecomment-340004129,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADhVd2FtwjodVIwynbRWJ1RokOakkQ5iks5swfbAgaJpZM4PxAxF
.

I reopened the issue. please update the project and we'll take a look. thanks for your help

I updated the solution. https://github.com/CarlosOnline/DotNetCore2DirectoryServicesBug

Install-Package System.DirectoryServices

Would it be simpler if I upgraded from net451 to net46 or net47? My only concern over upgrading is making sure everything still works afterwards.

I have looked quickly at your solution and it looks there were some confusions. It looks your net451 library project is referencing the net core System.DirectoryServices package. This is wrong, what @weshaggard suggesting is to have the net core app (spa in your case) is the one referencing the net core package. your net net451 library should just build as any other bits and should reference the full framework System.DirectoryServices library.

Anyway, I'll try to take a closer look later at that.

The spa - net core project is using PackageReference to System.DirectoryServices. There was no need to modify the net451 project since it has a traditional reference to the System.DirectoryServices

spa.csproj _net core 2.0 project_

    <PackageReference Include="System.DirectoryServices" Version="4.0.0" />

Util.csproj _net451 project_

    <Reference Include="System.DirectoryServices">
      <HintPath>..\packages\System.DirectoryServices.4.0.0\lib\System.DirectoryServices.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.DirectoryServices.AccountManagement" />

You should remove that hint path in your net451 project as it should be in the framework and doesn't need to point into the package.

I have removed the hint path for System.DirectoryServices. Bug still repros.

<Reference Include="System.DirectoryServices"/>

https://github.com/CarlosOnline/DotNetCore2DirectoryServicesBug

@CarlosOnline You need to also install 'System.DirectoryServices.AccountManagement` which is another package that matches the file-not-found error you are seeing.

@CarlosOnline I'm curious why is it that you are trying to write a .NET 4.5.1 class library and consume it from your .NET Core web application? Can you not write a .NET Core or .NET Standard class library?

There is no package System.DirectoryServices.AccountManagement. Can you provide the proper nuget command?

Why:

1 - I am building intranet websites.

2 - I am upgrading dotnet 1.0 project to 2.0. We have a large code base that was started with net451. Until till now we have been able to use with dotnet web sites.

3 - The new spa frameworks all work with dotnet 2.0 via dotnet new.

Here is the output from Install-Package System.DirectoryServices.AccountManagement into the dotnet core 2.0 project.

In addition the installs from https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.AccountManagement also do not work.

Install-Package System.DirectoryServices.AccountManagement
Install-Package : Unable to find package 'System.DirectoryServices.AccountManagement'
At line:1 char:1
+ Install-Package System.DirectoryServices.AccountManagement
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
 ```

**Install-Package System.DirectoryServices.AccountManagement -Version 4.5.0-preview2-25707-02 -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json**

Restoring packages for C:\temp\projects\DotNetCore2DirectoryServicesBug\spa\spa.csproj...
Package 'System.DirectoryServices 4.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
Install-Package : Detected package downgrade: System.DirectoryServices from 4.5.0-preview2-25707-02 to 4.0.0. Reference the
package directly from the project to select a different version.
spa (>= 1.0.0) -> System.DirectoryServices.AccountManagement (>= 4.5.0-preview2-25707-02) -> System.DirectoryServices (>=
4.5.0-preview2-25707-02)
spa (>= 1.0.0) -> System.DirectoryServices (>= 4.0.0)
At line:1 char:1

  • Install-Package System.DirectoryServices.AccountManagement -Version 4 ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Package restore failed. Rolling back package changes for 'spa'.
At line:1 char:1

  • Install-Package System.DirectoryServices.AccountManagement -Version 4 ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

      ```

Install-Package System.DirectoryServices.AccountManagement -Version 4.5.0-preview2-25707-02 -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json

You would need to change your dependency in your csproj to System.DirectoryServices to match the same version or higher than System.DirectoryServices.AccountManagement.

Better would be to just remove your dependency to System.DirectoryServices in your csproj (remove this) and add the dependency to System.DirectoryServices.AccountManagement 4.5.0-preview2-25707-02 and that will bring the System.DirectoryServices dependency automatically into you project since System.DirectoryServices.AccountManagement depends on it.

The issue that you're seeing is because System.DirectoryServices.AccountManagement 4.5.0-preview2-25707-02 has a dependency to System.DirectoryServices 4.5.0-preview2-25707-02 but in your .csproj you're telling nuget that you need to depend on System.DirectoryServices 4.4.0 which is smaller or older than the one that System.DirectoryServices.AccountManagement requires and that is not compatible.

You can now get it from Nuget as part of this: https://docs.microsoft.com/en-us/dotnet/core/porting/windows-compat-pack
@CarlosOnline is this working for you now? can we close this issue?

Away for holidays. I will check when I return. Thanks.

On Nov 22, 2017 7:19 PM, "Dan Moseley" notifications@github.com wrote:

You can now get it from Nuget as part of this:
https://docs.microsoft.com/en-us/dotnet/core/porting/windows-compat-pack
@CarlosOnline https://github.com/carlosonline is this working for you
now? can we close this issue?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/corefx/issues/24488#issuecomment-346533820,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADhVd8xv4oDxfgvXV2D2QejvHLmZ3OXXks5s5QBtgaJpZM4PxAxF
.

doesnt work for me yet on netcoreapp2.0

@safern could you please have a look. I believe we tested the compatibility pack so would be nice if you can share a project sample. Thanks.

@safern could you please have a look. I believe we tested the compatibility pack so would be nice if you can share a project sample. Thanks.

Yes I will, I just haven't had a chance today because of the next release, definitely will take a look tomorrow :)

Hey @CarlosOnline and @avantidesaigit here is a sample project that contains a .NET Framework Library that uses System.DirectoryServices.AccountManagement and a .NET Core 2.0 App that has a project reference to the .NET Framework Library and calls the API that uses AccountManagement.

The .NET Core 2.0 App needs to have the PackageReference to System.DirectoryServices.AccountManagement in order to find the assembly, if not you would get the FileNotFoundException. As you can see here.

Note that the .NET Framework Library has to include a reference to System.DirectoryServices.AccountManagement

The System.DirectoryServices.AccountManagement version that I'm referencing is coming from NuGet.
https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.5.0-preview1-25914-04

Also a few weeks ago we published a compatibility package that includes references to various packages that where ported from .NET Framework, including System.DirectoryServices.AccountManagement you can find more about it in this blog post

Please let me know if this fixed your issue or if you have any questions :)

SOLVED: Both packages solved the issue.

Thank You. The preview1 nuget package versions worked.

Either
_Install-Package Microsoft.Windows.Compatibility -Version 2.0.0-preview1-25914-04_
or
_Install-Package System.DirectoryServices.AccountManagement -Version 4.5.0-preview1-25914-04_
Worked.

Repo has been updated with fix. https://github.com/CarlosOnline/DotNetCore2DirectoryServicesBug
Thanks again 👍

Thanks for reporting the issue, I’m glad it worked. Will close for know, if you have more questions please don’t hesitate to ask them :)

FYI: Similar issue found for System.Threading.AccessControl

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omariom picture omariom  ·  3Comments

matty-hall picture matty-hall  ·  3Comments

GitAntoinee picture GitAntoinee  ·  3Comments

noahfalk picture noahfalk  ·  3Comments

nalywa picture nalywa  ·  3Comments