How to organize the return of data in a ListT?

5

I have a wcf that returns me some information, I would like to bring them organized, how could I do this?

I would like to return data like this:

Atthemomentitislikethis:

I'm doing this:

 public List<V_PRODUTOS> GetProdutos(string credencial)
        {
            if (credencial != ChaveCredencial)
            {
                throw new Exception("Erro: Usuário não autorizado");
            }

            try{

                using(SERRESTEEntities entites = new SERRESTEEntities())
                return entites.V_PRODUTOS.ToList();

            } 
            catch
            {
                throw new FaultException("Something went wrong");
            }

        }
    
asked by anonymous 08.06.2015 / 05:11

2 answers

1

itasousa, I particularly advise against formatting the return, this will only make the size of your response larger and will not bring you any benefit.

In any case, you can change the way WCF serializes the data, so you can use Json.NET instead of the default class.

For this you will need to implement these two interfaces:

public interface IDispatchMessageFormatter
{
    void DeserializeRequest(Message message, object[] parameters);
    Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result);
}

public interface IClientMessageFormatter
{
    Message SerializeRequest(MessageVersion messageVersion, object[] parameters);
    object DeserializeReply(Message message, object[] parameters);
}

And finally you will need to include the Formatter in some Behavior and associate this Behavior with some EndPoint.

16.08.2015 / 19:24
-1

I do not know if I understood your question well, but maybe if you put in the global.asax the following lines resolve.

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; 
    
08.06.2015 / 16:45