我有一个名为“WCFService”的名称空间下的服务和相关接口:
[ServiceContract]
public interface IBook
{
[OperationContract]
[WebInvoke(Method="GET"
,ResponseFormat=WebMessageFormat.Json
,BodyStyle=WebMessageBodyStyle.Wrapped
,UriTemplate="/?bookID={bookID}")]
Book Get(int bookID);
}
//========================================================
public Book Get(int bookID)
{
return bookList.Where(p => p.BookID == bookID).FirstOrDefault();
}
Book DataModel位于另一个名为“WCFModel”的名称空间下:
[DataContract]
public class Book
{
[DataMember]
public int BookID { get; set; }
[DataMember]
public string BookName { get; set; }
[DataMember]
public decimal BookPrice { get; set; }
[DataMember]
public string BookPublish { get; set; }
}
设置配置文件后,我的服务运行正常。 然后我计划在客户端使用它。我在项目中添加了一个Windows窗体应用程序,并添加了对WCFModel书籍数据模型的引用,我在下面的代码中使用它来提取数据:
string serviceURL = "http://localhost:15020/BookService.svc/?bookID=" + txtBookID.Text.Trim();
var request = WebRequest.Create(serviceURL);
var response = request.GetResponse();
DataContractSerializer serializer = new DataContractSerializer(typeof(WCFModel.Book));
var bookObj = serializer.ReadObject(response.GetResponseStream());
但结果是,VS在下面的ReadObject中抛出了一个异常:
Error when deserializing the WCFModel.Book type, Data at the root level is invalid.
下面的堆栈跟踪:
在 System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
在 System.Xml.XmlUTF8TextReader.Read()
在 System.Xml.XmlBaseReader.IsStartElement()
在 System.Xml.XmlBaseReader.IsStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
在 System.Runtime.Serialization.XmlReaderDelegator.IsStartElement(XmlDictionaryString localname, XmlDictionaryString ns)
在 System.Runtime.Serialization.XmlObjectSerializer.IsRootElement(XmlReaderDelegator reader, DataContract contract, XmlDictionaryString name, XmlDictionaryString ns)
在 System.Runtime.Serialization.DataContractSerializer.InternalIsStartObject(XmlReaderDelegator reader)
在 System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
在 System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
我不知道为什么,经过大量搜索,我仍然无法理解。有人可以给我一些参考吗? THX。
答案 0 :(得分:0)
我希望这对你有用! 试着实现这个!
WebClient proxy = new WebClient();
string serviceURL =
string.Format("http://localhost:15020/BookService.svc/?bookID=" + txtBookID.Text.Trim().ToString());
byte[] data = proxy.DownloadData(serviceURL);
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj =
new DataContractJsonSerializer(typeof(Book));
Book book = obj.ReadObject(stream) as Book;
Console.WriteLine("Book ID : " + book.BookID);
Console.WriteLine("Book Name : " + book.BookName);
Console.WriteLine("Book ID : " + book.BookPrice);
Console.WriteLine("Book ID : " + book.BookPublish);
请参阅此link
编辑:将BodyStyle=WebMessageBodyStyle.Wrapped
更改为BodyStyle=WebMessageBodyStyle.Bare
。