Link to home
Start Free TrialLog in
Avatar of nkewney
nkewneyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Returning a custom object in a WCF data contract

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

Avatar of Darren
Darren
Flag of Ireland image

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
If the UserExchangeCredentialSetCollection contains other objects then these will also have to have the DataContract and DataMember attributes added.

Avatar of nkewney

ASKER

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

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
ASKER CERTIFIED SOLUTION
Avatar of SameerJagdale
SameerJagdale
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nkewney

ASKER

This solved my problem.