Returning a custom object in a WCF data contract

nkewney
nkewney used Ask the Experts™
on
Dear Experts,

I was hoping somebody could help me with a problem I'm having with a WCF Service.

I'm trying to return a cusom object (defined elsewhere in my application) via my WCF service.

The object I'm trying to return is "UserExchangeCredentialSetCollection" which contains many "UserExchangeCredentialSet" items.

My Interface, (IExchangeService), has the following:

        [OperationBehavior]
        public UserExchangeCredentialSetCollection GetActiveExchangeUserCollection()
        {
            return UserExchangeCredentialSetMethods.GetActiveUserExchangeCredentialSetCollection();
        }

My service contract contains:

   [OperationContract]
        UserExchangeCredentialSetCollection GetActiveExchangeUserCollection();

Do I need to define this in my data contract?

If so, could somebody give me an idea of the best way to do this to enable me to use this data in my client application!

Many thanks in advance.

Nick
Current DataContract.cs (for testing only, with no mention of this collection)

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace LayerExchangeServices
{
    [DataContract]
    public class Request
    {
        string name;

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    [DataContract]
    public class Response
    {
        string message;

        [DataMember]
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DarrenSenior Software Engineer

Commented:
Hi,

I'm not sure that I follow...

You are returning a collection of UserExchangeCredentialSet

If you are returning this then you will have to ad the DataContract and DataMember attributes to UserExchangeCredentialSet

Maybe I've missunderstood....

Cheers,

Darren
DarrenSenior Software Engineer

Commented:
If the UserExchangeCredentialSetCollection contains other objects then these will also have to have the DataContract and DataMember attributes added.

Author

Commented:
Thanks Guys.

Like this?


[DataContract] 
    public class UserExchangeCredentialSet
    {

        [DataMember] 
        public Boolean EXCHG_APPT_SYNC { get; set; }
        [DataMember] 
        public string EXCHG_SVR { get; set; }
        [DataMember] 
        public string EXCHG_EMAIL { get; set; }
        [DataMember] 
        public string EXCHG_DOMAIN { get; set; }
        [DataMember] 
        public string EXCHG_USER { get; set; }
        [DataMember] 
        public string EXCHG_PASSWD { get; set; }

        public UserExchangeCredentialSet() { }





 [DataContract]
    public class UserExchangeCredentialSetCollection : CollectionBase
    {
        [DataMember]
        public UserExchangeCredentialSet this[int index]
        {
            get
            {
                return (UserExchangeCredentialSet)base.List[index];
            }

            set
            {
                base.List[index] = value;
            }
        }

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

DarrenSenior Software Engineer

Commented:
Hi,

Some post say that there are problem others say not

Yes, there is a problem with collections...
http://borismod.blogspot.com/2009/04/wcf-collectiondatacontract-and.html
http://just2thepoint.blogspot.com/2008/05/wcf-collectiondatacontract-and.html

No, there is not a problem with collections...
And from another post:http://social.msdn.microsoft.com/forums/en-US/wcf/thread/3749248c-c3b0-40eb-88db-95574205dd9a

You need to ensure that WCF knows how to serialize and deserialize all messages/paramaters used in your operations. This error can be caused by forgetting to apply e.g. [DataContract] to an class used in a message or collection. If you use derived objects you need to use the [ServiceKnownType(typeof(...))] attribute to let WCF know about how to de-/serialize objects not directly references in the operation contracts.

Thanks,

Darren
>>The object I'm trying to return is "UserExchangeCredentialSetCollection" which contains many "UserExchangeCredentialSet" items.
Have you tried with List<UserExchangeCredentialSet> ?

Author

Commented:
This solved my problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial