不能使用WCF REST服务的WCF客户端
我有一个服务,并在命名空间中的相关型号的接口称为“WCFService”:不能使用WCF REST服务的WCF客户端
[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();
}
和书的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)
我不知道为什么,搜索了很多之后,我仍然不能得到的地步。任何人都可以给我一些参考?谢谢。
我希望这可能适合你! 尝试执行此操作!
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
。
我收到一本空书,BookID为0,书名为空,BookPrice为0,BookPublish为空。 – CharlieShi
我终于搞定了它:BodyStyle = WebMessageBodyStyle.Bare,thx。 – CharlieShi
对!良好的研究:) –
可能是一个奇怪的问题,但为什么让你安然若想消费数据契约?宁静的整个目标是盲目地打电话已经知道返回格式。 – Franck