Pregunta

I have an ASP.Net app that allows users to interact with Dynamics CRM 2011 data through early bound entities.

I am currently storing the ServiceContext and OrganizationServiceProxy in session, the reason for this is that I have to get objects of a specific type then get their related entities when a user requires it. Finally updating them when the user hits save.

I know the ServiceContext and OrganizationServiceProxy implement IDisposable and as such should be disposed. Currently I am doing this in session end in my Global.asax.

I'm in the process of testing my thinking but should I in reality instantiate and dispose of both the proxy and context whenever I get my entities or when I am done with the entire process?

All the MS guides show wrap the entire process in a using statement so that the objects are disposed but what if the process requires user interaction?

¿Fue útil?

Solución

Unless you are only using an OrganizationServiceProxy once per page request, I would create a common function that lazy loads an OrganizationServiceProxy only once per request, and then use the unload method to dispose of it if it was loaded(see Closing a conneciton in the "unload" method for the safety of this approach)

This would make sense in most of the situations. Remember, there isn't anything special about the Using statement, besides it virtually guaranteeing that the dispose method is going to get called. It may be worth the acceptable risk of a few edge cases causing dispose not to get called for the sake of cleaner code, and not opening up 5 different connections for a single request.

I think it's very dangerous for you to be storing the context and proxy in the session because people can go for a coffee or bathroom break, and you're stuck with lot's of unused open connections.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top