Home All Groups Group Topic Archive Search About

PrincipalContext in class constructor

Author
15 Jun 2009 2:50 PM
musosdev
Hi

I'm trying to create a static class for my ActiveDirectory functions...
things like GetUsernameFromID, etc.

The code works, but every function has to declare its own PrincipalContext,
and what I'd really like it to have that as a static, as follows...

public class ActiveDirectory
    {
        static PrincipalContext dC;

        public ActiveDirectory()
        {
           dC = new PrincipalContext(ContextType.Domain,
"thepoint.org.uk.local");
        }
     }

    public static string GetCurrentUserFullName(string userSAM)
    {
         string currentUserFN = "(unknown)";
         string currentUserSN = "";
         UserPrincipal user = UserPrincipal.FindByIdentity(dC, userSAM);
         currentUserFN = user.GivenName;
         currentUserSN = user.Surname;

         return currentUserFN + " " + currentUserSN;
    }
}

(have removed try/catch blocks for brevity)

However, this throws the following error on the line "UserPrincipal user =
UserPrincipal.FindByIdentity(dC, userSAM);"

"Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: context"

I assume this related to the dC object not being set, even though it is
being set by the Contructor!? Any ideas on how to resolve this? I tried
making the constructor static, but that fails with "access modifiers are not
allowed on static constructors".

Any help is appreciated!

Cheers


Dan

Author
15 Jun 2009 3:40 PM
Patrice
It is not set as the *non static* constructor was never called...

What if you follow the message advice (i.e use a static constructor as you
wish and just suppress the access modifier on the static constructor ?, this
is likely because a static constructor is public by design as it has to be
called automatically).


--
Patrice

"musosdev" <musoswire@community.nospam> a écrit dans le message de groupe de
discussion : 1009D82F-2CDA-4446-92AF-6D409D638***@microsoft.com...
Show quoteHide quote
> Hi
>
> I'm trying to create a static class for my ActiveDirectory functions...
> things like GetUsernameFromID, etc.
>
> The code works, but every function has to declare its own
> PrincipalContext,
> and what I'd really like it to have that as a static, as follows...
>
> public class ActiveDirectory
>    {
>        static PrincipalContext dC;
>
>        public ActiveDirectory()
>        {
>           dC = new PrincipalContext(ContextType.Domain,
> "thepoint.org.uk.local");
>        }
>     }
>
>    public static string GetCurrentUserFullName(string userSAM)
>    {
>         string currentUserFN = "(unknown)";
>         string currentUserSN = "";
>         UserPrincipal user = UserPrincipal.FindByIdentity(dC, userSAM);
>         currentUserFN = user.GivenName;
>         currentUserSN = user.Surname;
>
>         return currentUserFN + " " + currentUserSN;
>    }
> }
>
> (have removed try/catch blocks for brevity)
>
> However, this throws the following error on the line "UserPrincipal user =
> UserPrincipal.FindByIdentity(dC, userSAM);"
>
> "Exception Details: System.ArgumentNullException: Value cannot be null.
> Parameter name: context"
>
> I assume this related to the dC object not being set, even though it is
> being set by the Contructor!? Any ideas on how to resolve this? I tried
> making the constructor static, but that fails with "access modifiers are
> not
> allowed on static constructors".
>
> Any help is appreciated!
>
> Cheers
>
>
> Dan
Are all your drivers up to date? click for free checkup

Author
15 Jun 2009 3:52 PM
Patrice
Actually technically it seems static constructors are private. Anyway the
idea is that as they are called automatically by the CLR, having an explicit
access modifier doesn't really make sense. This is why you'll have just to
remove "public" from you static constructor and then it should work as you
first intended...

--
Patrice


"Patrice" <http://www.chez.com/scribe/> a écrit dans le message de groupe de
discussion : 8238D59C-85E3-46C1-B1D1-52F3592EB***@microsoft.com...
Show quoteHide quote
> It is not set as the *non static* constructor was never called...
>
> What if you follow the message advice (i.e use a static constructor as you
> wish and just suppress the access modifier on the static constructor ?,
> this is likely because a static constructor is public by design as it has
> to be called automatically).
>
>
> --
> Patrice
>
> "musosdev" <musoswire@community.nospam> a écrit dans le message de groupe
> de discussion : 1009D82F-2CDA-4446-92AF-6D409D638***@microsoft.com...
>> Hi
>>
>> I'm trying to create a static class for my ActiveDirectory functions...
>> things like GetUsernameFromID, etc.
>>
>> The code works, but every function has to declare its own
>> PrincipalContext,
>> and what I'd really like it to have that as a static, as follows...
>>
>> public class ActiveDirectory
>>    {
>>        static PrincipalContext dC;
>>
>>        public ActiveDirectory()
>>        {
>>           dC = new PrincipalContext(ContextType.Domain,
>> "thepoint.org.uk.local");
>>        }
>>     }
>>
>>    public static string GetCurrentUserFullName(string userSAM)
>>    {
>>         string currentUserFN = "(unknown)";
>>         string currentUserSN = "";
>>         UserPrincipal user = UserPrincipal.FindByIdentity(dC, userSAM);
>>         currentUserFN = user.GivenName;
>>         currentUserSN = user.Surname;
>>
>>         return currentUserFN + " " + currentUserSN;
>>    }
>> }
>>
>> (have removed try/catch blocks for brevity)
>>
>> However, this throws the following error on the line "UserPrincipal user
>> =
>> UserPrincipal.FindByIdentity(dC, userSAM);"
>>
>> "Exception Details: System.ArgumentNullException: Value cannot be null.
>> Parameter name: context"
>>
>> I assume this related to the dC object not being set, even though it is
>> being set by the Contructor!? Any ideas on how to resolve this? I tried
>> making the constructor static, but that fails with "access modifiers are
>> not
>> allowed on static constructors".
>>
>> Any help is appreciated!
>>
>> Cheers
>>
>>
>> Dan
>
>
Author
18 Jun 2009 9:21 AM
musosdev
Brilliant - thanks for the explanations and advice guys.

static ActiveDirectory() {} works perfectly... great stuff!

Show quoteHide quote
"Patrice" wrote:

> Actually technically it seems static constructors are private. Anyway the
> idea is that as they are called automatically by the CLR, having an explicit
> access modifier doesn't really make sense. This is why you'll have just to
> remove "public" from you static constructor and then it should work as you
> first intended...
>
> --
> Patrice
>
>
> "Patrice" <http://www.chez.com/scribe/> a crit dans le message de groupe de
> discussion : 8238D59C-85E3-46C1-B1D1-52F3592EB***@microsoft.com...
> > It is not set as the *non static* constructor was never called...
> >
> > What if you follow the message advice (i.e use a static constructor as you
> > wish and just suppress the access modifier on the static constructor ?,
> > this is likely because a static constructor is public by design as it has
> > to be called automatically).
> >
> >
> > --
> > Patrice
> >
> > "musosdev" <musoswire@community.nospam> a crit dans le message de groupe
> > de discussion : 1009D82F-2CDA-4446-92AF-6D409D638***@microsoft.com...
> >> Hi
> >>
> >> I'm trying to create a static class for my ActiveDirectory functions...
> >> things like GetUsernameFromID, etc.
> >>
> >> The code works, but every function has to declare its own
> >> PrincipalContext,
> >> and what I'd really like it to have that as a static, as follows...
> >>
> >> public class ActiveDirectory
> >>    {
> >>        static PrincipalContext dC;
> >>
> >>        public ActiveDirectory()
> >>        {
> >>           dC = new PrincipalContext(ContextType.Domain,
> >> "thepoint.org.uk.local");
> >>        }
> >>     }
> >>
> >>    public static string GetCurrentUserFullName(string userSAM)
> >>    {
> >>         string currentUserFN = "(unknown)";
> >>         string currentUserSN = "";
> >>         UserPrincipal user = UserPrincipal.FindByIdentity(dC, userSAM);
> >>         currentUserFN = user.GivenName;
> >>         currentUserSN = user.Surname;
> >>
> >>         return currentUserFN + " " + currentUserSN;
> >>    }
> >> }
> >>
> >> (have removed try/catch blocks for brevity)
> >>
> >> However, this throws the following error on the line "UserPrincipal user
> >> =
> >> UserPrincipal.FindByIdentity(dC, userSAM);"
> >>
> >> "Exception Details: System.ArgumentNullException: Value cannot be null.
> >> Parameter name: context"
> >>
> >> I assume this related to the dC object not being set, even though it is
> >> being set by the Contructor!? Any ideas on how to resolve this? I tried
> >> making the constructor static, but that fails with "access modifiers are
> >> not
> >> allowed on static constructors".
> >>
> >> Any help is appreciated!
> >>
> >> Cheers
> >>
> >>
> >> Dan
> >
> >
>

Bookmark and Share