C#-WCF REST服务JSON的客户端
我试图为我找到的WCF REST服务创建一个简单的客户端 我添加了服务引用,并编写了以下代码:C#-WCF REST服务JSON的客户端,c#,.net,json,wcf,rest,C#,.net,Json,Wcf,Rest,我试图为我找到的WCF REST服务创建一个简单的客户端 我添加了服务引用,并编写了以下代码: private void button1_Click(object sender, EventArgs e) { WebClient proxy = new WebClient(); string serviceURL = string.Format("http://localhost:53215/IBookService.svc/GetBooksName
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);
}
当我运行代码(按下按钮)时,出现以下错误:
类型的未处理异常
中发生“System.Runtime.Serialization.SerializationException”
System.Runtime.Serialization.dll
附加信息:类型
“finalProject\u ClientX.ServiceReference3.Book”无法序列化为
JSON,因为其IsReference设置为“True”。JSON格式不支持
不支持引用,因为没有用于
表示引用。要启用序列化,请禁用
IsReference设置的类型或相应的父类
类型
当我在浏览器中运行“”时,我得到了以下书籍:
“[”MVC音乐商店-教程-
v3.0、Pro.ASP.NET.MVC.3.Framework、应用程序架构指南
v2、“四人帮设计模式”、“CS4口袋参考”]
问题是什么?似乎实体框架在添加属性DataContract时包含属性
IsReference=true
因此,我建议您在项目中包含nuget包。
然后将代码修改为类似以下内容:
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);
var jsonString = Encoding.UTF8.GetString(data);
IList<string> bookNames = JArray.Parse(jsonString).ToObject<IList<string>>();
//Do something with the list
//foreach (string bookName in bookNames)
//{
//}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
WebClient proxy=新的WebClient();
字符串服务URL=
字符串格式(“http://localhost:53215/IBookService.svc/GetBooksNames");
字节[]数据=代理下载数据(serviceURL);
var jsonString=Encoding.UTF8.GetString(数据);
IList bookNames=JArray.Parse(jsonString.ToObject();
//对清单做点什么
//foreach(bookNames中的字符串bookName)
//{
//}
}
似乎实体框架在添加属性DataContract时包含属性IsReference=true
因此,我建议您在项目中包含nuget包。
然后将代码修改为类似以下内容:
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);
var jsonString = Encoding.UTF8.GetString(data);
IList<string> bookNames = JArray.Parse(jsonString).ToObject<IList<string>>();
//Do something with the list
//foreach (string bookName in bookNames)
//{
//}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
WebClient proxy=新的WebClient();
字符串服务URL=
字符串格式(“http://localhost:53215/IBookService.svc/GetBooksNames");
字节[]数据=代理下载数据(serviceURL);
var jsonString=Encoding.UTF8.GetString(数据);
IList bookNames=JArray.Parse(jsonString.ToObject();
//对清单做点什么
//foreach(bookNames中的字符串bookName)
//{
//}
}
您拥有这项服务吗?@PrestonGuillot在哪里?在客户端中?与中一样,您是否拥有所引用服务的定义?你能改变它的合同吗?@PrestonGuillot我不明白你拥有这项服务吗?@PrestonGuillot在哪里?在客户端中?与中一样,您是否拥有所引用服务的定义?你能更改它的合同吗?@PrestonGuillot我不明白默认情况下,IsReference
为false。我试图更改它。。。但我也犯了一个错误。“System.dll中发生了类型为'System.Net.WebException'的未处理异常。其他信息:远程服务器返回错误:(500)内部服务器错误。“IsReference
默认为false。我试图更改它。。。但我也犯了一个错误。“System.dll中发生未处理的“System.Net.WebException”类型的异常。其他信息:远程服务器返回错误:(500)内部服务器错误。”