2014-04-03 83 views
0

作爲我的WCF Web服務返回消息的一部分,我創建了三個自定義類。它們在我的返回消息類中實現,它包含一個DataContract裝飾(很確定這是應該如何完成的)。在WCF Test Client中使用自定義類

然而,當我運行的Visual Studio 2012 WCF測試客戶端,我得到了以下錯誤消息(黑色高亮顯示)

enter image description here

應用程序代碼

類暴露在程序調用網絡服務。這個調用一個方法用的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屬性表示的街道,城市,州,郵編等(下面列出)編寫的自定義類。

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

EDIT

添加枚舉和類到後

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 

回答

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測試客戶端。

+0

仍然得到相同的錯誤信息 – NealR

+0

枚舉已知類型丟失..你可以添加並重試? –

+0

已添加並且仍然沒有運氣 – NealR

0
  1. 添加DataContract屬性AgentClone & RelationshipCode類
  2. 如果AddressTypeValues.AddressType是枚舉類型,然後應用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 
        } 
    } 
    

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

  5. 如果您仍然面臨問題,那麼我100%確定問題出在AgentInformation/RelationshipCodes上。只需評論這兩個CloneMessage類的成員並嘗試。你會得到一些指針。如果你在評論之後沒有遇到問題,那麼這與EntityObject有關。類似的問題 - Why doesn't WCFTestclient understand standard EF objects but understands STE objects

+0

'AgentClone'和'RelationshipCode'已包含'[DataContractAttribute(IsReference = true)]''。 「DataContract」仍然是必要的,如果它在上面/下面列出這個裝飾,它是否重要? – NealR

+0

好的,(2)和(3)呢? – Rajes

+0

已經完成了。有人建議將'KnownType'屬性添加到'CloneMessage'中,所以我這樣做了(對於所​​有三個類)並仍然出現相同的錯誤。 – NealR