2010-09-03 24 views
19

Ho riscontrato questo problema e mi sono passato i capelli sopra. Ho l'errore followin:Impossibile serializzare il membro .... perché è un'interfaccia

Exception Details: System.NotSupportedException: Cannot serialize member HannaPrintsDataAccess.Customer.CustomerAddresses of type System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

Source Error:

Line 196: Customer customer = OperationsManager.Instance.CustomerService.GetCustomer(7); Line 197: Line 198: string xml = OperationsManager.Instance.CustomerService.GetCustomerAddressesXml(CustomerAddress.FindAll()); Line 199: Line 200: Order order = OperationsManager.Instance.OrderService.CreateOrderFromCart(xml);

Source File: c:\HostingSpaces\greetwus\galadavetiye.com\wwwroot\HannaPrints\HannaPrints\WebUI\CreateGreetingCard.aspx.cs Line: 198

Stack Trace:

[NotSupportedException: Cannot serialize member HannaPrintsDataAccess.Customer.CustomerAddresses of type System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.]

[InvalidOperationException: Cannot serialize member 'HannaPrintsDataAccess.Customer.CustomerAddresses' of type 'System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.] System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +889917 System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +132........

ho cambiato tutto di alla lista di vedere se questo sarebbe fare qualsiasi cosa, ma non ha ancora, infatti, che non ha ancora nemmeno prendere un secondo a caricare dopo aver apportato tali modifiche mia IList, im guessing perché l'errore si verifica anche prima che arrivi a quella parte. Ho controllato i miei file remoti per vedere se stava caricando correttamente ed è stato.

Ecco il codice:

using System; 
using System.Collections.Generic; 
using Castle.ActiveRecord; 
namespace HannaPrintsDataAccess { 
    public partial class Customer { 
     private IList _customerAddresses; 


     public CustomerAddress GetPrimaryCustomerAddress() 
     { 
      foreach (CustomerAddress address in _customerAddresses) 
      { 
       if (address.IsPrimary) 
        return address; 
      } 
      return null; 
     } 


     [HasMany(typeof(CustomerAddress), ColumnKey = "CustomerId", Table = "Customer")] 
     public virtual IList<CustomerAddress> CustomerAddresses 
     { 
      get 
      { 
       return this._customerAddresses; 
      } 
      set 
      { 
       this._customerAddresses = value; 
      } 
     } 
    } 
} 

L'errore si verifica quando tale codice è attivato: classe

protected void orderButton_Click(object sender, EventArgs e) 
{ 
    Customer customer = OperationsManager.Instance.CustomerService.GetCustomer(7); 

    string xml = OperationsManager.Instance.CustomerService.GetCustomerAddressesXml(CustomerAddress.FindAll()); 

    Order order = OperationsManager.Instance.OrderService.CreateOrderFromCart(xml); 
    OperationsManager.Instance.CartService.MoveCart("MyDesigns"); 

    Response.Redirect("~/Customer/PayByCreditCard.aspx?orderGuid=" + order.OrderGuid); 
} 

Il CustomerAddress:

using System.IO; 
using System.Xml.Serialization; 
using Castle.ActiveRecord; 


namespace HannaPrintsDataAccess 
{ 
public partial class CustomerAddress 
{ 
    public string ToXml() 
    { 
     XmlSerializer serializer = new XmlSerializer(GetType()); 
     MemoryStream memoryStream = new MemoryStream(); 
     serializer.Serialize(memoryStream, this); 
     memoryStream.Seek(0, SeekOrigin.Begin); 
     return new StreamReader(memoryStream).ReadToEnd(); 
    } 

    [BelongsTo("CustomerId")] 
    public virtual Customer Customer { get; set; } 
} 
} 

risposta

22

Nel codice pubblicato, il tipo di CustomerAddresses è IList<CustomerAdress>. Questa è un'interfaccia. Come dice il messaggio di errore, non è possibile serializzare un'interfaccia.

+0

Lo capisco, ma ho provato a cambiarlo in Elenco normale, ma mi ha dato l'errore SAME e mi sono assicurato che il codice fosse stato caricato e modificato correttamente – anthonypliu

+0

@anthony: no, non lo hai cambiato in un elenco e lo hai fallire allo stesso modo. Scusa, hai fatto qualcosa di sbagliato. Probabilmente qualcosa di stupido lascia il valore di ritorno della proprietà virtuale come 'IList ' e restituisce semplicemente 'Lista ' dall'override. –

+0

Sto avendo lo stesso problema. Quando lo faccio, "serializzatore XmlSerializer = new XmlSerializer (kevinObject)" dove kevinObject è un oggetto con l'attributo, [DataContract]. In kevinObject, c'è un campo, chiamato "INewObject" perché ci sono più implementazioni di INewObject. C'è un modo per includere un'interfaccia contrassegnata [DataContract]? –

-1
Non

l'origine del problema, ma è necessario

using (MemoryStream memoryStream = new MemoryStream()) 
{ 
    serializer.Serialize(memoryStream, this); 
    memoryStream.Seek(0, SeekOrigin.Begin); 
    using (StreamReader reader = new StreamReader(memoryStream)) 
    { 
     return reader.ReadToEnd(); 
    } 
} 
+1

Come funzionerà se 'serializer' è un XmlSerializer creato come l'utente ha dichiarato e il tipo non può essere serializzato? –

+0

Nulla funzionerà se il tipo non può essere serializzato. –

Problemi correlati