我试图创建WCF REST服务”,我发现 here.C# - 客户端的WCF REST服务JSON
我增加了服务的参考,我写了这个代码的简单客户端:
private void button1_Click(object sender, EventArgs e)
{
WebClient proxy = new WebClient();
string serviceURL =
string.Format("http://localhost:53215/IBookService.svc/GetBooksNames");
byte[] data = proxy.DownloadData(serviceURL);
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj =
new DataContractJsonSerializer(typeof(finalProject_ClientX.ServiceReference3.Book));
finalProject_ClientX.ServiceReference3.Book book = obj.ReadObject(stream) as finalProject_ClientX.ServiceReference3.Book;
MessageBox.Show("book ID : " + book.BookName);
}
当我运行的代码(按下按钮),我收到以下错误:
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll
Additional information: The type 'finalProject_ClientX.ServiceReference3.Book' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.
当我在我的本本浏览器中运行“http://localhost:53215/IBookService.svc/GetBooksNames”:
"["MVC Music Store - Tutorial - v3.0","Pro.ASP.NET.MVC.3.Framework","Application Architecture Guide v2","Gang of Four Design Patterns","CS4 Pocket Reference"]"
什么问题?
您是否拥有该服务? –
@PrestonGuillot在哪里?在客户端? – user3868442
如您所说,您是否拥有您所引用服务的定义?你能改变它的合同吗? –