在WCF测试客户端中使用自定义类

时间:2014-04-03 21:08:40

标签: c# wcf visual-studio-2012

作为我的WCF Web服务返回消息的一部分,我创建了三个自定义类。它们在我的返回消息类中实现,该类包含DataContract装饰(非常确定它应该如何完成)。

但是当我运行Visual Studio 2012 WCF测试客户端时,我收到以下错误消息(以黑色突出显示)

enter image description here

应用代码

暴露给调用Web服务的程序的类。这会调用一个返回类型为CloneMessage的方法(详见下文)

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);
        }
    } 
}

应用代码界面

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

}

克隆消息类

这是WCF服务返回的类。 AgentCloneRelationshipCode类由实体框架生成并扩展System.Data.Objects.DataClasses.EntityObjectAgentAddresses是我用标准string属性编写的自定义类,代表Street,City,State,Zip等(如下所列)。

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;
        }
    }
}

克隆消息界面

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

修改

将枚举和类添加到帖子

AgentAddresses类

AddressType是自定义枚举。

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枚举

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和RelationshipCode类标题

[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

2 个答案:

答案 0 :(得分:0)

尝试将以下已知类型添加到CloneMessage数据协定中。

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

这个AddressTypeValues类型为AgentAddress类。

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

执行此操作后,重新构建服务并尝试再次浏览WCF测试客户端。

答案 1 :(得分:0)

  1. 将DataContract属性添加到AgentClone&amp; RelationshipCode类
  2. 如果AddressTypeValues.AddressType是Enum类型,则将DataContractAttribute属性应用于该类型。然后,必须将EnumMemberAttribute属性应用于必须包含在数据协定中的每个成员。参考 - http://msdn.microsoft.com/en-us/library/aa347875(v=vs.110).aspx
  3. 将以下属性添加到CloneMessage类 [KnownType(typeof运算(AgentAddress))] [KnownType(typeof运算(RelationshipCode))]
  4. 尝试改变这样..

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

    [数据成员] public AddressType AddressType {get;组; }

  5. 如果您仍然面临问题,那么我100%确定问题出在AgentInformation / RelationshipCodes上。只需评论CloneMessage类的这两个成员并尝试。你会得到一些指示。如果您在评论后没有遇到问题,那么它与EntityObject有关。类似的问题 - Why doesn't WCFTestclient understand standard EF objects but understands STE objects