1

我正在开发一个 wcf-webservice。消费者可以在 atom-response 和 json-response 之间进行选择。

我的 OperationContract 如下所示:

[OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/json")]
    Result GetData();

Result-Type 包含一些字符串和一组条目。

 [DataContract]
public class Result
{
    [DataMember]
    public string baseUrl;
    [DataMember]
    public string url;
    [DataMember]
    public string title;
    [DataMember]
    public int totalResults;
    [DataMember]
    public JsonEntries[] resources;
}

我也将 JsonEntries 标记为 DataContract:

  [DataContract]
public class JsonEntries
{
    [DataMember]
    public string updated;
    [DataMember]
    public string key;
    [DataMember]
    public string title;
    [DataMember]
    public Salary salarie;
}

但是,当我尝试运行它时,我收到错误,即无法调用元数据。当我删除[DataMember]数组前面的时,我没有收到错误,但我的响应不包含数组。我已经在各种示例中看到它像这样工作。那我做错了什么?

提前致谢。

罗比德

4

1 回答 1

1

应该可以,这是我的代码示例

数据合同

[DataContract]
public class Result
{
    [DataMember]
    public int totalResults;
    [DataMember]
    public JsonEntries[] resources;
}

[DataContract]
public class JsonEntries
{
    [DataMember]
    public string title;
}

运营合同

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getdata")]
Result GetData();

    public Result GetData()
    {
        var jsonEntries = new List<JsonEntries>
            {
                new JsonEntries {title = "1"},
                new JsonEntries {title = "2"},
                new JsonEntries {title = "3"}
            }.ToArray();
        return new Result
        {
            resources = jsonEntries,
            totalResults = 1
        };
    }

我接电话

    private Bag<T> GetData<T>(string uri)
    {
        try
        {
            var request = (HttpWebRequest) WebRequest.Create(uri);
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.Headers[HttpRequestHeader.Authorization] = _authorizationData;
            var response = (HttpWebResponse) request.GetResponse();
            Stream stream = response.GetResponseStream();
            var localStream = new MemoryStream();
            CopyStream(stream, localStream);
            stream.Close();
            var result = JsonContractExtensions.Create<T>(localStream);
            return new Bag<T>(result);
        }
        catch (WebException ex)
        {
            _log.Debug(ex);
            return Bag<T>.Empty;
        }
        catch (Exception ex)
        {
            _log.Debug(ex);
            return Bag<T>.Empty;
        }
    }
于 2012-07-30T22:21:28.123 回答