robidd
robidd

Reputation: 53

wcf json-response Array/List

I am developing an wcf-webservice. The consumer is able to choose between an atom-response and a json-response.

My OperationContract looks like this:

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

The Result-Type contains some strings and an array of entries.

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

I marked the JsonEntries also as DataContract:

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

However, when i am trying to run this i get the error, that the metadata could not be called. When I am deleting the [DataMember] in front of the array, i get no error, but my response doesnt contain the array. I've seen it work like this on various exmaples. So what am i doing wrong?

thanks in advance.

robidd

Upvotes: 1

Views: 1900

Answers (1)

GSerjo
GSerjo

Reputation: 4778

Should work, here is my code sample

DataContracts

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

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

OperationContract

[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
        };
    }

my get call

    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;
        }
    }

Upvotes: 1

Related Questions