views:

157

answers:

3

Hey Guys, I have been having this problem and been pulling my hair out over it. I have the followin error:

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........

I have changed all my IList's to List's to see if that would do anything, but it didnt, infact, it didnt even take a second to load after making those changes, im guessing because the error happens even before it gets to that part. I checked my remote files to see if it was uploading correctly and it was.

Here is the code:

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

}

The error happens when this code is activated:

enter code here  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);
}

}

The CustomerAddress class:

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

}

If anyone could help that would be much appreciated! Thankss!!!

A: 

Not the source of your problem, but you need

using (MemoryStream memoryStream = new MemoryStream())
{
    serializer.Serialize(memoryStream, this);
    memoryStream.Seek(0, SeekOrigin.Begin);
    using (StreamReader reader = new StreamReader(memoryStream))
    {
        return reader.ReadToEnd();
    }
}
John Saunders
A: 

Is CustomerAddresses marked as Serializable?

Jonathan
+2  A: 

In the code you posted, the type of CustomerAddresses is IList<CustomerAdress>. That's an interface. Like the error message says, you can't serialize an interface.

John Saunders
I understand that, but I tried changing it to normal List, but it gave me the SAME error, and I made sure code was uploaded and changed properly
anthonypliu
@anthony: no, you didn't change it to a list and have it fail the same way. Sorry, you did something wrong. Probably something silly like leave the return value of the virtual property as `IList<T>` and just return `List<T>` from the override.
John Saunders