0

As part of my WCF web service's return message I created three custom classes. They are implemented in my the return message class, which contains a DataContract decoration (pretty sure that's how it's supposed to be done).

However when I run the Visual Studio 2012 WCF Test Client I get the following error message (highlighted in black)

enter image description here

App Code

Class exposed to the program calling the web service. This calls a method with a return type of CloneMessage (detailed below)

namespace OKeeffeDataService
{
    public class MonetToDss : IMonetToDss
    {
        private AgentCloneRules _agentClone;

        public MonetToDss()
        {
            _agentClone = new AgentCloneRules();
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "AgentPaymentUpdater")]
        public CloneMessage CloneRequest(string agentId)
        {
            //TODO: Validate agent Id?
            EventLog.WriteEntry("OKeeffe", "Made it to CloneRequest", EventLogEntryType.Information);
            return _agentClone.CloneRequest(agentId);
        }
    } 
}

App Code Interface

namespace OKeeffeDataService
{
    [ServiceContract]
    public interface IMonetToDss
    {
        [OperationContract]
        CloneMessage CloneRequest(string agentId);
    }

}

Clone Message Class

This is the class the WCF service returns. The AgentClone and RelationshipCode classes were generated by the Entity Framework and extend System.Data.Objects.DataClasses.EntityObject. AgentAddresses is a custom class I wrote with standard string properties representing Street, City, State, Zip, etc (listed below).

namespace BusinessEntities
{
    [DataContract]
    public class CloneMessage : ICloneMessage
    {
        [DataMember]
        public AgentClone AgentInformation { get; set; }
        [DataMember]
        public IList<AgentAddress> AgentAddresses { get; set; }
        [DataMember]
        public IList<RelationshipCode> RelationshipCodes { get; set; }
        [DataMember]
        public string ErrorMessage { get; set; }

        public CloneMessage(){}

        public CloneMessage(AgentClone agtTran, IList<AgentAddress> addresses, IList<RelationshipCode> relationshipCodes)
        {
            this.AgentInformation = agtTran;
            this.AgentAddresses = addresses;
            this.RelationshipCodes = relationshipCodes;
        }
    }
}

Clone Message Interface

namespace BusinessEntities
{
    public interface ICloneMessage
    {
        AgentClone AgentInformation { get; set; }
        IList<AgentAddress> AgentAddresses { get; set; }
        IList<RelationshipCode> RelationshipCodes { get; set; }
        String ErrorMessage { get; set; }
    }
}

EDIT

Adding the enum and classes to the post

AgentAddresses class

AddressType is custom enum.

namespace BusinessEntities
{
    [DataContract]
    public class AgentAddress : IAgentAddress
    {
        [DataMember]
        public AddressTypeValues.AddressType AddressType { get; set; }
        [DataMember]
        public string Street1 { get; set; }
        [DataMember]
        public string Street2 { get; set; }
        [DataMember]
        public string Street3 { get; set; }
        [DataMember]
        public string City { get; set; }
        [DataMember]
        public string State { get; set; }
        [DataMember]
        public string ZipCode { get; set; }
    }
}

AddressTypeValues enum

namespace BusinessEntities
{
    public class AddressTypeValues
    {
        [DataContract(Name = "AddressType")]
        public enum AddressType
        {
            [EnumMember(Value = "Home")]
            Home,
            [EnumMember(Value = "Mailing")]
            Mailing,
            [EnumMember(Value = "Location")]
            Location,
            [EnumMember(Value = "Other")]
            Other
        }
    }
}

AgentClone and RelationshipCode class headers

[EdmEntityTypeAttribute(NamespaceName="AgentResourcesReturn", Name="AgentClone")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class AgentClone : EntityObject

[EdmEntityTypeAttribute(NamespaceName="AgentResourcesReturn", Name="RelationshipCode")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class RelationshipCode : EntityObject
NealR
  • 10,189
  • 61
  • 159
  • 299

2 Answers2

0

Try adding the following known types to your CloneMessage data contract.

[DataContract]
[KnownType(typeof(AgentClone))]
[KnownType(typeof(AgentAddress))]
[KnownType(typeof(RelationshipCode))]
public class CloneMessage : ICloneMessage

And this AddressTypeValues type to the AgentAddress class.

[DataContract]
[KnownType(typeof(AddressTypeValues))]
public class AgentAddress : IAgentAddress

Once you do this, rebuild the service and try to browse it again the WCF test client.

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
0
  1. Add DataContract attribute to AgentClone & RelationshipCode classes
  2. If AddressTypeValues.AddressType is Enum type, then apply the DataContractAttribute attribute to the type. You must then apply the EnumMemberAttribute attribute to each member that must be included in the data contract. refer - http://msdn.microsoft.com/en-us/library/aa347875(v=vs.110).aspx
  3. Add the below attributes to CloneMessage class [KnownType(typeof(AgentAddress))] [KnownType(typeof(RelationshipCode))]
  4. try changing like this..

    namespace BusinessEntities
    {
        [DataContract(Name = "AddressType")]
        public enum AddressType
        {
            [EnumMember(Value = "Home")]
            Home,
            [EnumMember(Value = "Mailing")]
            Mailing,
            [EnumMember(Value = "Location")]
            Location,
            [EnumMember(Value = "Other")]
            Other
        }
     }
    

    [DataMember] public AddressType AddressType { get; set; }

  5. If you are still facing issue, then I am 100% sure that the problem is with AgentInformation/RelationshipCodes. Just comment these two members of CloneMessage class and try. you will get some pointers. If you don't face issue after commenting, then it is something to do with EntityObject. similar issue - Why doesn't WCFTestclient understand standard EF objects but understands STE objects

Community
  • 1
  • 1
Rajes
  • 1,036
  • 10
  • 15
  • `AgentClone` and `RelationshipCode` already contain `[DataContractAttribute(IsReference=true)]`. Is the `DataContract` still necessary and if so does it matter if it is listed above/below this decoration? – NealR Apr 03 '14 at 22:08
  • Already done that as well. Someone suggested the `KnownType` attributes being added to `CloneMessage`, so I did that (for all three classes) and still get the same error. – NealR Apr 03 '14 at 22:17
  • added some more info about the enum and `EntityObject` classes to the post. – NealR Apr 03 '14 at 22:18
  • This wouldn't compile (not sure if you meant for the `DataMember` be a property of the class `AddressTypeValues` or the namespace but tried both). Could this be because I am using the Entity Framework to generate the `AgenClone` and `RelationshipCode` classes? – NealR Apr 03 '14 at 22:35