HapiDjus
HapiDjus

Reputation: 298

How to Deserialize a list of custom objects

I've really hit a roadblock trying to deserialize a list of custom objects over a WCF service. It's using .NET 4. How do I get around the different namespaces. My ASP.NET app is running on 3.5 and the reference sets up ok. Could that be the issue? How do I get around it?

My service is set up like this:

Contract

namespace MyDomain.Services.Report
{
   [ServiceContract(Name = "ICompanyReport")]
   public interface ICompanyReport
   {
       [OperationContract]
       byte[] GetFooReport(string fooName);
   }

   [Serializable]
   [DataContract(Name="FooReportRecord", Namespace="MyDomain.com"]
   public class FooReportRecord
   {
      [DataMember]
      public int ID {get; set;}
      [DataMember]
      public string Name {get; set;}
   }

}

svc.cs

public class CompanyReport: ICompanyReport
{
    public byte[] GetFooReport(string fooName)
    {
        var data = new List<FooReportRecord>();

        // get data based on fooName and populate data

        var ms = new MemoryStream();
        var bf = new BinaryFormatter();
        bf.Serialize(ms, data);
        return ms.ToArray();
    }
}

client side:

var ls = proxy.GetFooReport("bar");
var bf = new BinaryFormatter();
var ms = new MemoryStream(ls);

// Unable to find assembly MyDomain.Services.Report error is thrown
var reportData = (List<FooReportRecord>)bf.Deserialize(ms);

Upvotes: 3

Views: 970

Answers (1)

EdmundYeung99
EdmundYeung99

Reputation: 2511

Assuming your client is your .NET 3.5 app and FooReportRecord is in your .NET 4 wcf library, you will get a compile time error.

Move FooReportRecord to a 3rd class library compiled in .NET 3.5 and reference this from both your WCF app and Client (ASP.NET)

But as @Phil mentioned, why not return FooReportRecord[] instead of byte[]

Service

public FooReportRecord[] GetFooReport(string fooName)
{
    var data = new List<FooReportRecord>();

    // get data based on fooName and populate data

    return data.ToArray();
}

Client

var proxy = new ServiceReference1.CompanyReportClient();
FooReportRecord[] ls = proxy.GetFooReport("bar");

Upvotes: 1

Related Questions