Problems with Directory Services and Account Management in ASP.NET Core

孤者浪人 提交于 2019-12-01 18:35:24

In ASP.NET Core (the new Name for ASP.NET 5 and vNext isn't being officially used for months) a project will by default target multiple platforms.

You have added the references to dnx451 framework moniker, which targets the full .NET 4.5 Framework.

Now if you use

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

inside a *.cs file, it will work for .NET 4.5, but fail for .NET Core, because there is no reference for .NET Core to this namespaces/assemblies. On build always both versions will be compiled, that's why you get this error.

You don't see it in the Visual Studio Editor, because it shows you the code of .NET 4.5 (above the code window is a pulldown menu where you can switch between the target frameworks).

That being said, you have two choices:

  1. You want to target only .NET 4.5

    In this case, you just delete the dnxcore50 moniker from your frameworks section of the project.json file (net451 too because it's for class libraries only). This will turn your project an ASP.NET Core project which targets only the full .NET 4.5.

  2. You want/need to target .NET Core w/o working against the full .NET 4.5 Framework

    This is a bit more difficult. You either need a replacement for the same library in .NET Core. If it uses the same namespaces, just add its references to the dnxcore50 moniker and you are good to go.

    If there is no direct replacement, you have to remove this code parts from .NET Core projects, by using preprocessor directives.

    #if DNX451
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    #endif
    

Now your namespace will be used only in the .NET 4.5 target, but not in the .NET Core target.

On a sidenote: You can remove the net451 moniker, as it's only used for Class Library (Package), the new project type for .NET Core libraries which compiles into a package which targets multiple framework targets. See the RC1 announcement for more.

For applications (ASP.NET Core Web Project or xUnit project) always use the application moniker (dnx451 and dnxcore50 respectively). For class libraries it's net451 and dotnet5.x.

To the anser of @Tseng I want to add that Directory Services are planned for .NET Core 1.2. Here is the feature request on GitHub: https://github.com/dotnet/corefx/issues/2089. You can subscribe there and get notifications when something is happening there!

When it's released you can just use Directory Services in ASP.NET Core with .NET Core! It might take some time, considering the current state and version of .NET Core, though.

Microsoft has released pre-release version for System.DirectoryServices in asp.net core 2.0. You can get it from nuget package manager using this command:

Install-Package System.DirectoryServices -Version 4.5.0-preview1-25914-04

This is working fine for me till now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!