Silverlight 4 + WCF序列化与反序列化问题

lantingxiaosheng 2011-11-07 09:43:27
我用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;
}
}

}
}

望各位兄弟帮忙,谢过
...全文
127 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lantingxiaosheng 2011-11-22
  • 打赏
  • 举报
回复
看来只能自己解决了,好像不能用MemoryStream来进行序列化和反序列化。
lantingxiaosheng 2011-11-08
  • 打赏
  • 举报
回复
朋友们,这是怎么了,没人回复哦
qianguohua 2011-11-08
  • 打赏
  • 举报
回复
顶一下,刚学sl+WCF ,学习中

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧