Skip to main content

Connot disposed object. Object Name 'System.serviceModelSecurity.TransportSecurityProtocol'


 I and working n ASP.NET WebApi application with some code that calls that Get method to get a OrganizationServiceProxy object, and then proceeds to call various methods on that OrganizationServiceProxy object. For example, part of the login process does this.

The way your Get method works is, each time it is called, it will first close the OrganizationServiceProxy object that it returned previously (regardless of whether or not it is still being used) then create and return a new one.
Imagine two users attempt to log in to your application at almost the same time - within milliseconds of each other. The thread handling the first user's login calls Get and gets its OrganizationServiceProxy object, then a millisecond later the thread handling the second user's login calls Get, which causes the first thread's OrganizationServiceProxy object to be closed. However, the first thread is still running, and now when it attempts to call a method on it's OrganizationServiceProxy object, it gets a Connot disposed object. Object Name 'System.serviceModelSecurity.TransportSecurityProtocol'
"As you can see, I am closing and reopening the connection each time."
The code currently in your Get method is not a good way to achieve this. You should instead make it the caller's responsibility to close (or dispose) the OrganizationServiceProxy object, and you should perhaps rename your Get method to Open or Create to suggest this. Callers should use a using statement to ensure that the object is closed/disposed, regardless of any exceptions that occur:



 using (proxyService = new OrganizationServiceProxy(organizationUri, null, credentials, null))
 {
  proxyService.EnableProxyTypes();
        _service = (IOrganizationService)proxyService;
 }

Comments

Post a Comment