asp.net – 在WCF中将对象数组序列化为JSON以符合OpenSearch
|
我正在尝试编写符合OpenSearch规范的OpenSearch建议服务.
http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions 此规范要求服务返回JSON数组,第一个元素是字符串,以下元素是字符串数组.我可以通过返回一个字符串数组(string [] [])并让WCF将其序列化为JSON来获得它.但是,为了符合规范,我试图返回一个对象数组(object []),第一个是字符串,其余的是字符串数组(string []). 每当我尝试返回对象数组时,它都不起作用,例如: 来自服务: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SuggestionService : ISuggestionService
{
public object[] Search(string searchTerms)
{
SearchSuggestions = new object[4];
SearchText = searchTerms;
SearchSuggestions[0] = SearchText;
Text = new string[10];
Urls = new string[10];
Descriptions = new string[10];
// Removed irrelevant ADO.NET code
while (searchResultReader.Read() && index < 10)
{
Text[index] = searchResultReader["Company"].ToString();
Descriptions[index] = searchResultReader["Company"].ToString();
Urls[index] = "http://dev.localhost/Customers/EditCustomer.aspx?id=" +
searchResultReader["idCustomer"];
index++;
}
SearchSuggestions[1] = Text;
SearchSuggestions[2] = Descriptions;
SearchSuggestions[3] = Urls;
return SearchSuggestions;
}
[DataMember]
public string SearchText { get; set; }
[DataMember]
public string[] Text { get; set; }
[DataMember]
public string[] Descriptions { get; set; }
[DataMember]
public string[] Urls { get; set; }
[DataMember]
public object[] SearchSuggestions { get; set; }
}
这是整个界面: [ServiceContract]
public interface ISuggestionService
{
[OperationContract]
[WebGet(UriTemplate = "/Search?q={searchTerms}",BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json)]
object[] Search(string searchTerms);
}
这会导致服务返回“错误324(net :: ERR_EMPTY_RESPONSE):未知错误.”这是我能得到的唯一错误. 我无法使用一组对象来存储一个字符串和三个数组?为了使用WCF返回符合此规范的正确JSON,我还能做些什么呢? 编辑:添加了更多的代码 解决方法
你发布了一堆[DataMember]的.请发布整个[DataContract].还向我们展示了返回DataContract时返回的JSON.
数据合同不应包含行为.尝试以下(我没有机会测试它,并且需要假装数据来执行此操作): [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SuggestionService : ISuggestionService
{
public SearchResults Search(string searchTerms)
{
var results = new SearchResults
{
SearchText = searchTerms,Text = new string[10],Urls = new string[10],Descriptions = new string[10]
};
// Removed irrelevant ADO.NET code
int index = 0;
while (searchResultReader.Read() && index < 10)
{
results.Text[index] = searchResultReader["Company"].ToString();
results.Descriptions[index] = searchResultReader["Company"].ToString();
results.Urls[index] = "http://dev.localhost/Customers/EditCustomer.aspx?id=" +
searchResultReader["idCustomer"];
index++;
}
return results;
}
}
[DataContract]
public class SearchResults
{
[DataMember]
public string SearchText { get; set; }
[DataMember]
public string[] Text { get; set; }
[DataMember]
public string[] Descriptions { get; set; }
[DataMember]
public string[] Urls { get; set; }
}
好吧,我已经阅读了足够的规范和JSON规范,以说服自己确实需要返回一个对象数组,而不是一个包含对象数组的类的实例.当然,这并不是很有效.这是你需要的: [ServiceContract]
[ServiceKnownType(typeof(string))]
[ServiceKnownType(typeof(string[]))]
public interface ISuggestionService
{
[OperationContract]
[WebGet(UriTemplate = "/Search?q={searchTerms}",ResponseFormat = WebMessageFormat.Json)]
object[] Search(string searchTerms);
}
我只是尝试过它,它起作用了.这是JSON(添加缩进): [
"abc",["Company1","Company2","Company3",...],["Company1 Description","Company2 Description","Company3 Description",["http://dev.localhost/Customers/EditCustomer.aspx?id=1","http://dev.localhost/Customers/EditCustomer.aspx?id=2","http://dev.localhost/Customers/EditCustomer.aspx?id=3",...]
]
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.Net下拉列表始终返回按钮单击事件的第一个值
- asp.net-mvc-4 – 当IIS在不同的系统上运行时,SignalR WPF客
- asp.net-mvc-3 – 为什么抛出NULL值异常?
- ASP.NET async /等待第2部分
- 在ASP.NET中,什么是’ASP’代码?
- asp.net-mvc – 如何从视图中检索传递给我的自定义EditorFo
- asp.net – SignalR跨域不在IE10以外的浏览器上工作
- 使用模块化设计组织良好的ASP.NET应用程序的最佳方法
- 如何防止ASP.NET站点的图像热链接?
- asp.net-mvc – ASP.NET MVC:Mock controller.Url.Action
- ASP.Net Web应用程序安全性不适用于IIS 7?
- ASP.NET 使用System.Drawing 绘制随机验证码
- asp.net – RegisterStartupScript不适用于Scrip
- asp.net – AngularJs 2与ASP.NET 4.5.1
- asp.net-mvc-routing – ASP.NET Core 1.0中的属
- .NET 3.5 / VS 2008上的ASP.NET Web Services的自
- 为ASP.Net Core Weblistener安装SSL证书
- asp.net-mvc – 在MVC Controller中访问GET参数
- asp.net – 具有默认值的TextBox
- 在Textbox asp.net上使用JQuery DatePicker
