gpt4 book ai didi

asp.net - 在 WCF 中将对象数组序列化为 JSON 以符合 OpenSearch

转载 作者:行者123 更新时间:2023-12-02 12:16:16 27 4
gpt4 key购买 nike

我正在尝试编写一个符合 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]。请发布完整的[数据契约(Contract)]。还向我们展示您返回 DataContract 时返回的 JSON。

<小时/>

数据契约(Contract)不应包含行为。尝试以下操作(我还没有机会测试它,并且需要伪造数据才能做到这一点):

[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}",
BodyStyle = WebMessageBodyStyle.Bare,
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 - 在 WCF 中将对象数组序列化为 JSON 以符合 OpenSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1134371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com