我在理解如何在WCF REST中使用DataContractSerializer时遇到了问题
我使用了一个通道工厂,如下所示:
Uri uri = new Uri("http://localhost:50000/people");
WebChannelFactory<IPersonService> chFactory = new WebChannelFactory<IPersonService>(uri);
IPersonService iPerson = chFactory.CreateChannel();然后可以直接从通道调用通道方法,如下所示
List<Person> allPeople = new List<Person>();
allPeople = iPerson.getAll();这就是我到目前为止所了解的如何使用DataContractSerializer以便输出响应的方法
MemoryStream stream = new MemoryStream();
<--------------- how to i read iPerson.getAll() into stream? --------->
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
List<Person> allpeople2 = (List<Person>)dcs.ReadObject(reader, true);
reader.Close();
stream.Close();我不太确定如何将这些片段组合在一起,使其全部工作。
发布于 2012-06-04 05:53:36
我觉得你把事情搞得有点复杂了。
我将按照这个介绍页面启动一个新项目:http://msdn.microsoft.com/en-us/magazine/dd315413.aspx
当您在web.config上配置序列化问题时,您只需在接口/类上声明属性,而不必为您的对象编写一行序列化/反序列化代码(除非您需要获得定制的序列化,而这在您的情况下是不需要的)
通过提供的url "http://localhost:50000/people",我假设您正在寻找RESTful服务,所以如果您需要一些更高级的功能,您也可以查看一下:https://github.com/mikeobrien/WcfRestContrib
https://stackoverflow.com/questions/10872468
复制相似问题