How to organize the return of data in a List<T>?

Asked

Viewed 543 times

5

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

I’d like to return data like this: inserir a descrição da imagem aqui

At the moment it’s like this: inserir a descrição da imagem aqui

I’m doing like 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");
            }

        }

2 answers

1

itasousa, I particularly advise against you formatting the return, this will only leave the size of your Sponse larger and will not bring you any benefits.

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

for such 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 to some Endpoint.

Note that this process is not so simple, I even see some benefits in replacing the standard serialization class with Json.NET, but if the purpose is just to create JSON, I ask you to consider the following alternative:

Install any of the following plugins in your preferred browser:

-1

I’m not sure I understand your question, but maybe if you put the following lines on the global.asax solution.

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; 
  • No global.asax and a WCF Rest webservice

  • Sorry, I mistook it for Webapi

Browser other questions tagged

You are not signed in. Login or sign up in order to post.