Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:44:59+00:00 2026-05-25T14:44:59+00:00

My scenario is this. I am using System.DirectoryServices.AccountManagement for dealing with AD users and

  • 0

My scenario is this.

I am using System.DirectoryServices.AccountManagement for dealing with AD users and groups. My Main method (this is a console app for now) calls into a method that returns PrincipalSearchResult<Principal>. Both objects implement IDisposable.

If I return this, how can I ensure I can dispose of all these disposable objects?

class Searcher
{    
    private PrincipalSearchResult<Principal> SearchForObjects(string searchString)
    {
        PrincipalContext ctx = null;
        PrincipalSearcher principalSearcher = null;
        Principal principal = null;

        ctx = new PrincipalContext(ContextType.Domain, "blah", "dc=blah,dc=com");

        principal = new GroupPrincipal(ctx) { Name = searchString };
        principalSearcher = new PrincipalSearcher { QueryFilter = principal };

        return principalSearcher.FindAll();
    }
}

static void Main(string[] args)
{
    Searchers searchers = new Searchers();

    PrincipalSearchResult<Principal> theGroups = searchers.SearchForGroupsMatching("some*");

    foreach (GroupPrincipal group in theGroups)
    {
        Console.WriteLine(group.DisplayName);
        // do stuff...
    }
}

Is passing back the PrincipalSearchResult<Principal> a really bad idea, for reasons relating to unmanged object disposal?

Am I better off creating a managed proxy object?

I imagine that if I’m only caring about reading a subset of properties, it may be “better” to create a custom object containing only these properties. When writing back, in this case to an AD group, all I really need to pass into a method are the changed properties and the key. This would allow me to constrain the creation of unmanaged objects to one scope. Or is all of this unnecessary and more trouble than it’s worth? My apologies for the semi-scattered stream of consciousness…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:45:00+00:00Added an answer on May 25, 2026 at 2:45 pm

    No it is not a bad idea. There are a lot examples in the .NET framework, that do this, e.g. SqlClient.ExecuteReader.

    However, you need to document that the user needs to dispose the object as soon as he is finished using it.

    You would do it like this:

    using(PrincipalSearchResult<Principal> theGroups = 
          searchers.SearchForGroupsMatching("some*"))
    {
        foreach (GroupPrincipal group in theGroups)
        {
            Console.WriteLine(group.DisplayName);
            // do stuff...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using System; using System.Threading; // Simple threading scenario: Start a static method running //
I'm trying to implement this scenario using Unity and i can't figure out how
I have this scenario where I would like to redirect my domains using the
Detailed scenario is like this : I am creating 10 buttons using a loop
Scenario: I have a document I created using LaTeX (my resume in this case),
Hey, This is my scenario: Using visual studio 2008. Project is in c# Using
How to load flex components using XML. The scenario is simple as like this,
This scenario comes up often, now that I use LinkToSql and the classes it
I want to write a web app order system using the REST methodology for
So I have a scenario: Using System.currentTimeMillis() I get the initial start time of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.