2014-02-24 120 views
1

我有一個服務,並在命名空間中的相關型號的接口稱爲「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) 

我不知道爲什麼,搜索了很多之後,我仍然不能得到的地步。任何人都可以給我一些參考?謝謝。

+0

可能是一個奇怪的問題,但爲什麼讓你安然若想消費數據契約?寧靜的整個目標是盲目地打電話已經知道返回格式。 – Franck

回答

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.WrappedBodyStyle=WebMessageBodyStyle.Bare

+0

我收到一本空書,BookID爲0,書名爲空,BookPrice爲0,BookPublish爲空。 – CharlieShi

+0

我終於搞定了它:BodyStyle = WebMessageBodyStyle.Bare,thx。 – CharlieShi

+0

對!良好的研究:) –