System.DirectoryServices.AccountManagement.UserPrincipal Serialization

Español

You can’t serialize this Type, the first reason is simple: Doesn’t have [Serialize] attribute on the base classes, but more important it has a lot of dependencies on non-managed resources that has to be connected to this class. If you could serialize making some tricks the class would expect to connect to Active Directory on the client consumer side, and assuming that the class could connect then it will be non sense to send the class in the first place. It will be more easy that the final client create and connect to the class directly.

So if you need to serialize just “data” of this class like some info on the attributes of an user. The recommended way will be to implement a memento pattern.

When you had a lot of properties and you are not worried of a coupled internal constructor (for example when the class will be serialized for a .Net Web Service and that constructor will not be used on the client consumer side) you could use a internal constructor that get as an argument the original class like:

[Serialize]
public class UserInfo
{
  private string displayName;
  ...

  public UserInfo()
  {
  }

  internal UserInfo(UserPrincipal user)
  {
      this.displayName = user.DisplayName;
      ...
  }
}

This way you can use it in your return methods like this:

return new UserInfo(user);
Advertisements

One thought on “System.DirectoryServices.AccountManagement.UserPrincipal Serialization

  1. Pingback: Serialización de System.DirectoryServices.AccountManagement.UserPrincipal « Ernesto Ocampo Blog en Español

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s