Asked by:
Cache Connection MS CRM 2011 for developer extensions

Question
-
My question is specific to the blog post highlighted below. 'm using the CrmConnection class to create my connection. My question s, how do I cache this connection and reuse the cached connection to get the org service. Could anyone provide code samples/links on caching connections. Thanks
http://develop1.net/public/post/MicrosoftXrmClient-Part-2.aspx
Please note that I am not using the CRMSVCUtil to generate the early bound entities.
Here is my code:
OrganizationService proxy=null;
var connection = CrmConnection.Parse("Url=xxx; Username=xxx; Password=xxx;");
using (proxy = new OrganizationService(connection))
{
var request = new WhoAmIRequest();
var response = (WhoAmIResponse)proxy.Execute(request);
}
My question: How to "consume" a cached-connection in all my queries? What is the code snippet / syntax to re-use a connection ? What is the design pattern I should use to re-use connections when creating applications with late-bound entities?
- Edited by IamaNovice Sunday, November 17, 2013 10:36 AM
Sunday, November 17, 2013 8:33 AM
All replies
-
Hi,
The easiest way is to store the connection string in the app/web.config as described by http://www.develop1.net/public/post/Microsoft-Xrm-Client-(Part-3b)-Configuration-via-appwebconfig.aspx.
The connection management is actually the same if you are using late-bound as it would be if you were using early bound.
hth,
Scott
Scott Durow
Blog www.develop1.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"Monday, November 18, 2013 6:27 AMAnswerer -
Hi Scott, Thank you.
I'm not storing the connection string in the 'connection string' tag of the config file, but am storing in the app-settings of teh web.config file. This will be accessed by the data access layer. Hence, I use CrmConnection.Parse(app-setting).
This is how I consume the org-service, what is wrong with this design pattern?
DB Connection Class
public class DBConnection { private OrganizationService _organizationService; public OrganizationService OrgService { get { if (_organizationService == null) { _organizationService = GetOrganizationService(); } return _organizationService; } } public OrganizationService GetOrganizationService() { try { OrganizationService proxy=null; var connection = CrmConnection.Parse("Url=xxx; Username=xxx; Password=xxx;"); using (proxy = new CachedOrganizationService(connection)) { var request = new WhoAmIRequest(); var response = (WhoAmIResponse) proxy.Execute(request); } return proxy; } catch (Exception e) { //handle throw; } } #endregion GetCRMOrganizationService }
Consumption Code
public class helper { private DBConnection connection public helper() { if (connection == null) connection = new DBConnection(); } public EntityCollection GetContacts() { collection = Service.OrgService.RetrieveMultiple(queryExpression); return collection; } }
My questions:
1) I'm using this pattern in all my repository classes where there are n-number of queries/retrievals from MS CRM. Is this the right way to re-use the OrgService without recreating conenctions everytime?
2) If you see,I'm using the CachedOrganizationService. What is the disadvantage of using this as against the OrganizationService?
3) Also, in response to the link you posted, I'm not using the OrganizationServiceContext anywhere in may app, as I think it's used for ealy-bound entities. Correct me if I'm wrong.
Thank you so much!
- Edited by IamaNovice Monday, November 18, 2013 7:20 PM
Monday, November 18, 2013 7:10 PM