ajay
ajay

Reputation: 163

WCF Service collection type to Generic List instead of an array

I was wondering if there is a way to configure the collection type to Generic List instead of any array on the client end programmatically. I know if we add service reference and go to advanced settings we can specify there. But the way the project is designed is that we have separate assembly for the contracts and is referenced by both the server and the client and also the model types are defined in the same project which are used by both the service and the client. So instead of adding a service reference we are doing everything in the code by defining the bindings and endpoints programmatically. I want to know if this is something that can be achieved in code easily without writing code to convert the arrays into generic lists on the client end.

Thanks, Ajay

Upvotes: 0

Views: 1507

Answers (1)

ajay
ajay

Reputation: 163

This was due to the DataContractSerializer converting the GenericLists to arrays. To handle this issue I had to intercept the deserialization process on the object and convert them back to the lists. I am providing my class definition and the partial class definition to make my answer more easy to understand.

Class Definition:

public partial class provider
{
    public provider()
    {
        this.provider_contracts = new HashSet<provider_contracts>();
        this.provider_clinics = new HashSet<provider_clinics>();
        this.provider_custom_field_validations = new HashSet<provider_custom_field_validations>();
        this.provider_directory_languages = new HashSet<provider_directory_languages>();
        this.provider_directory_services = new HashSet<provider_directory_services>();
        this.provider_employees = new HashSet<provider_employees>();
        this.provider_field_exceptions = new HashSet<provider_field_exceptions>();
        this.provider_languages = new HashSet<provider_languages>();
        this.provider_required_fields = new HashSet<provider_required_fields>();
        this.provider_users = new HashSet<provider_users>();
        this.user_privileges = new HashSet<user_privileges>();
    }

    public virtual ICollection<provider_contracts> provider_contracts { get; set; }
    public virtual ICollection<provider_clinics> provider_clinics { get; set; }
    public virtual ICollection<provider_custom_field_validations> provider_custom_field_validations { get; set; }
    public virtual ICollection<provider_directory_languages> provider_directory_languages { get; set; }
    public virtual ICollection<provider_directory_services> provider_directory_services { get; set; }
    public virtual ICollection<provider_employees> provider_employees { get; set; }
    public virtual ICollection<provider_field_exceptions> provider_field_exceptions { get; set; }
    public virtual ICollection<provider_languages> provider_languages { get; set; }
    public virtual provider_options provider_options { get; set; }
    public virtual ICollection<provider_required_fields> provider_required_fields { get; set; }
    public virtual ICollection<provider_users> provider_users { get; set; }
    public virtual ICollection<user_privileges> user_privileges { get; set; }
}

My Partial class to handle this issue:

public partial class provider
{
    [OnDeserialized()]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        if (provider_contracts != null) provider_contracts = provider_contracts.ToList();
        if (provider_clinics != null) provider_clinics = provider_clinics.ToList();
        if (provider_custom_field_validations != null) provider_custom_field_validations = provider_custom_field_validations.ToList();
        if (provider_directory_languages != null) provider_directory_languages = provider_directory_languages.ToList();
        if (provider_directory_services != null) provider_directory_services = provider_directory_services.ToList();
        if (provider_employees != null) provider_employees = provider_employees.ToList();
        if (provider_field_exceptions != null) provider_field_exceptions = provider_field_exceptions.ToList();
        if (provider_languages != null) provider_languages = provider_languages.ToList();
        if (provider_required_fields != null) provider_required_fields = provider_required_fields.ToList();
        if (provider_users != null) provider_users = provider_users.ToList();
        if (user_privileges != null) user_privileges = user_privileges.ToList();
    }
}

Helpful resource: http://yanivh.wordpress.com/2008/11/21/datacontractserializer-and-generic-lists/

I have also answered this question on MSDN forum and am adding a link below "http://social.msdn.microsoft.com/Forums/vstudio/en-US/5ccb534c-e310-48d4-8345-160c06568220/collection-types-when-using-channelfactory-without-servicereference?forum=wcf"

Upvotes: 1

Related Questions