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

419 Views Asked by At

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. dotnet new console -f=net5.0 -o=ConsoleFiveToSeven
  3. cd ConsoleFiveToSeven
  4. dotnet run
  5. Inside ConsoleFiveToSeven.csproj change <TargetFramework>net5.0</TargetFramework> to <TargetFramework>net7.0</TargetFramework> and save
  6. Execute dotnet run again. This creates \ConsoleFiveToSeven\bin\Debug\net7.0
  7. Execute dotnet add package System.DirectoryServices.Protocols --version 7.0.0
  8. Add the code below
  9. Execute dotnet 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"}
            );
        }
    }
}
1

There are 1 best solutions below

1
Michał Turczyn On BEST ANSWER

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"});