簡體   English   中英

如何將控制台應用程序從 .net 5 升級到 .net 7 以使用 DirectoryServices

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

要連接到服務器,我想在應用程序中使用目錄服務

這些是重現的步驟:

  1. 在控制台上執行這些命令
  2. do.net new console -f.net5.0 -o=ConsoleFiveToSeven
  3. cd ConsoleFiveToSeven
  4. do.net run
  5. ConsoleFiveToSeven.csproj <TargetFramework.net5.0</TargetFramework>更改為<TargetFramework.net7.0</TargetFramework>並保存
  6. 再次執行do.net run 這將創建\ConsoleFiveToSeven\bin\Debug.net7.0
  7. 執行do.net add package System.DirectoryServices.Protocols --version 7.0.0
  8. 添加下面的代碼
  9. 執行do.net build

問題是什么?

  • 預期:解決方案編譯沒有問題
  • 實際:錯誤 CS0103 distinguishedName and ldapFilter and searchScope and attributeList does not exist in the current context

SearchRequest的文檔包含此構造函數

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

問題

據我了解,下面的代碼應該沒問題。 問題是什么?

上面第8步的代碼

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

不是完全。 在 C# 中, =以名稱指定參數是無效的。 你應該使用:

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

但是如果你使用參數的默認順序,你可以省略它們的名字:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM