简体   繁体   中英

How to upgrade a Console app from .net 5 to .net 7 to use DirectoryServices

To connect to an -Server i want to use Directory Services in a -console application.

These are the steps to reproduce:

  1. On a console execute these commands
  2. do.net new console -f.net5.0 -o=ConsoleFiveToSeven
  3. cd ConsoleFiveToSeven
  4. do.net run
  5. Inside ConsoleFiveToSeven.csproj change <TargetFramework.net5.0</TargetFramework> to <TargetFramework.net7.0</TargetFramework> and save
  6. Execute do.net run again. This creates \ConsoleFiveToSeven\bin\Debug.net7.0
  7. Execute do.net add package System.DirectoryServices.Protocols --version 7.0.0
  8. Add the code below
  9. Execute do.net build

What is the problem?

  • Expected: The solutions compiles without a problem
  • Actual: Error CS0103 distinguishedName and ldapFilter and searchScope and attributeList does not exist in the current context

The docs for SearchRequest contains this constructor

public SearchRequest (string distinguishedName
       , string ldapFilter
       , System.DirectoryServices.Protocols.SearchScope searchScope
       , params string[] attributeList);

Question

To my understanding the following code below should be fine. What is the problem?

Code for step 8 above

using System;
using System.Net;   
using System.DirectoryServices.Protocols;

namespace ConsoleFiveToSeven
{
    class Program
    {
        static void Main(string[] args)
        {
            SearchRequest search = new SearchRequest(
                distinguishedName = "foo",
                ldapFilter = "foo",
                searchScope = SearchScope.Subtree,
                attributeList = new string [] {"cn=foo", "cn=bar", "cn=com"}
            );
        }
    }
}

Not entirely. In C#, = to specify parameters by names is invalid. You should use :

SearchRequest search = new SearchRequest(
    distinguishedName: "foo",
    ldapFilter: "foo",
    searchScope: SearchScope.Subtree,
    attributeList: new string [] {"cn=foo", "cn=bar", "cn=com"});

But if you use default order of parameters you can omit their names:

SearchRequest search = new SearchRequest(
    "foo",
    "foo",
    SearchScope.Subtree,
    new string [] {"cn=foo", "cn=bar", "cn=com"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM