Httpselfhost Serialize daughter entities + Session nhibernate

Asked

Viewed 37 times

1

I have the following method:

    // GET: api/Pedido/5
    [ResponseType(typeof(Orcamento))]
    public IHttpActionResult Get(int id)
    {
        using (OrcamentoRepository or = new OrcamentoRepository())
        {
            var orcamento = or.PesquisarPedidoAbertoPorNumeroComanda(null, id);
            //var teste = JsonConvert.SerializeObject(orcamento, Parametros.JsonConfig.JsonSerializerSettings);

            return Ok(orcamento);
        }
    }

and the following configuration:

    public static JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings()
    {
        Formatting = Formatting.Indented, 
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore, 
        PreserveReferencesHandling = PreserveReferencesHandling.All, 
        ContractResolver = new DefaultContractResolver()
        {
            IgnoreSerializableAttribute = true 
        }
    };

The normal call for this method does not return the object, because the Session closes in the Return even before serializing the object...

But if I remove the comment in the Jsonconvert.Serializeobject() line, I simply serialize for a test variable that will not be used, the method will return normally. (at that time he carries the daughter lists that are set up as Lazyload in nhibernate)

I wonder if there are any settings that I can change to close the using’s Session only after serializing and sending, or I’ll have to do the conversion manually and send the json in string format.

I implemented the Repository standard in Apicontroller, it is functional but I do not know if it is correct to do this

public class ApiControllerRepository<R> : ApiController
{
    protected R Repositorio;

    public ApiControllerRepository()
    {
        Repositorio = Activator.CreateInstance<R>();
    }

    protected override void Dispose(bool disposing)
    {
        ((IDisposable)Repositorio).Dispose();
        base.Dispose(disposing);
    }
}
  • But when is Session being closed? It’s in the method Dispose repository?

  • Yes, the repository implements Idisposable just so it doesn’t get opened. Only the controller is closing before generating the answer

  • Or for the web I should always leave Session open? that would be a Session per controller, I believe I would waste a lot of memory

  • It’s just that it’s kind of poorly planned. The pipeline itself won’t end up in controller return. Either you will have to change the time to close Session, or you will need to load the entities you want to return. I see no exit out of this.

  • I’m starting with web, I don’t understand much, could explain better why it doesn’t end in return?

  • by chance the method PesquisarPedidoAbertoPorNumeroComanda() is not asynchronous, is?

Show 1 more comment
No answers

Browser other questions tagged

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