Silverlight 4 + WCF序列化与反序列化问题
我用VS2010作为开发环境,Silverlight 4 + WCF。我在WCF中序列化了一组数据,但在Silverlight中反序列化时,提示我没有“无参构造函数”,我明明写了啊,不知道是什么问题,望各兄弟赐教,先在此谢过。下面上代码。
WCF代码:接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
namespace WcfServiceForSilverlight
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
void GetDataFromMemory(out MemoryStream stream);
[OperationContract]
MNLValues GetMNLTest();
[OperationContract]
string GetStr(int val);
[OperationContract]
List<MNLValues> GetMNL();
}
[DataContract]
public class MNLValues
{
public MNLValues() { }
[DataMember]
public string ShiJian
{ get; set; }
[DataMember]
public double GaoDu
{ get; set; }
[DataMember]
public double ShuDu
{ get; set; }
}
}
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
namespace WcfServiceForSilverlight
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class TestService : ITestService
{
public TestService()
{
//
}
private void GetStreamData(MemoryStream stream, List<MNLValues> lists)
{
DataContractSerializer ser = new DataContractSerializer(typeof(List<MNLValues>));
ser.WriteObject(stream, lists);
}
private List<MNLValues> GetTestList()
{
MNLValues mnl;
List<MNLValues> lists = new List<MNLValues>();
Random r = new Random(2);
for (int i = 0; i < 30; i++)
{
mnl = new MNLValues() { ShiJian = i.ToString(), GaoDu = r.Next(300, 500), ShuDu = r.Next(800, 1000) };
lists.Add(mnl);
}
return lists;
}
public void GetDataFromMemory(out MemoryStream stream)
{
stream = new MemoryStream();
GetStreamData(stream, GetTestList());
}
public MNLValues GetMNLTest()
{
throw new NotImplementedException();
}
public string GetStr(int val)
{
return val.ToString();
}
public List<MNLValues> GetMNL()
{
return GetTestList();
}
}
}
在Windows中调用正常,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testService.ser;
using System.Runtime.Serialization;
using System.IO;
namespace testService
{
class Program
{
static void Main(string[] args)
{
TestServiceClient client = new TestServiceClient();
DataContractSerializer s = new DataContractSerializer(typeof(List<MNLValues>));
MemoryStream stream = client.GetDataFromMemory();
stream.Position = 0;
List<MNLValues> lists = (List<MNLValues>)s.ReadObject(stream);
foreach (MNLValues m in lists)
{
Console.WriteLine(m.GaoDu);
}
}
}
}
但在Silverlight中调用就报错,缺少“无参构造函数”,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using SilverlightApplication4.ServiceR;
using System.Runtime.Serialization;
using System.Diagnostics;
namespace SilverlightApplication4
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void FormLoad_Load(object sender, System.Windows.RoutedEventArgs e)
{
}
private void button_Click(object sender, RoutedEventArgs e)
{
GetData();
}
private void GetData()
{
TestServiceClient client = new TestServiceClient();
client.GetDataFromMemoryAsync();
client.GetDataFromMemoryCompleted += Com;
client.CloseAsync();
}
private void Com(object sender, GetDataFromMemoryCompletedEventArgs e)
{
MemoryStream stream = e.Result;
stream.Position = 0;
DataContractSerializer ser = new DataContractSerializer(typeof(List<MNLValues>));
//NetDataContractSerializer ser = new NetDataContractSerializer();
List<MNLValues> lists = (List<MNLValues>)ser.ReadObject(stream);
foreach (MNLValues m in lists)
{
this.textBox2.Text += m.GaoDu;
}
}
}
}
望各位兄弟帮忙,谢过
[解决办法]
顶一下,刚学sl+WCF ,学习中