Thursday, June 11, 2015

MS CRM performance improvement with metadata caching when creating SDK connection.

MS CRM performance improvement with metadata caching when creating SDK connection.

I was dealing with performance issues in my WCF services which were exposed to various clients. Clients consume WCF method to connect to MSCRM to do operation in MSCRM.

Solution 1 – Making OrganizationServiceProxy Object as Static or Cache it:

I created a class variable of Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy as static and every time the request come it is shared between multiple requests.

I also tried caching the service proxy.

private static Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy _serviceProxy;

public static Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy OrganizationServiceProxy

{

get

{

if (_serviceProxy == null)

{

string crmUrl = CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["Url"];

 

ClientCredentials creds = new ClientCredentials();

creds.Windows.ClientCredential = new System.Net.NetworkCredential(

CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["Username"],

CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["password"],

CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["Domain"]);

 

Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy serviceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(new Uri(crmUrl), null, creds, null);

serviceProxy.EnableProxyTypes();

 

return serviceProxy;

}

}

}

 

This particular solution really help me gaining huge performance in my WCF solution.

But this solution resulted in poisoning of OrganizationProxy.

Let me explain with an example.

Consider a request come from USER-1 and organization proxy created under USER-1 credentials. The request is still under pipeline and when it reached to actually create any entity record in MS CRM, another request comes in from USER-2 and organization proxy gets changed under USER-2 credentials. The record which needs to be created by USER-1 now gets created with USER-2.

So, I was back to square one and started to find more other solution to get performance around my WCF service.

 

Solution-2: Make only Metadata Cache or as Static and used in while creating OrganizationServiceProxy.

Let me provide code for what above statement says.

First I have declare a variable for Organization Service Management.

[ThreadStatic]

private static IServiceManagement<IOrganizationService> _orgServiceManagement;

Next step is to define the get method for this variable

public static Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy OrganizationServiceProxy

{

get

{

string crmUrl = CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["Url"];

 

if (_orgServiceManagement == null)

{

IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(crmUrl));

 

_orgServiceManagement = orgServiceManagement;

}

ClientCredentials creds = new ClientCredentials();

creds.Windows.ClientCredential = new System.Net.NetworkCredential(

CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["Username"],

CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["password"],

CrmConfigurationManager.CreateConnectionDictionary(ConfigurationManager.ConnectionStrings["CRMConnectionString"])["Domain"]);

Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy serviceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(_orgServiceManagement, creds);

serviceProxy.EnableProxyTypes();

return serviceProxy;

}

}

I hope the code says it all.

The OrganizationServiceProxy Method is overloaded and can accept metadata around your CRM organization as in parameter.

So we just need to cache Management Instance of it.

This can really help you gain a lot of performance with no major issues around it.

 

Try this solution in your environment and let me know if it work and how much performance you gain.

Happy CRMing.

 

 

Wednesday, June 10, 2015

I am getting error as - There are too many active security negotiations or secure conversations at the service

I have deployed CRM in cluster environment along with multiple WCF service hosted on other website.

I am performing a load test on my WCF service which are internally referring CRM Sdk and making a call to MS CRM.

The scenario for a load test
Create Account with 100 user load
Create Cases with 100 user load
Create Quotes with 100 user load

Now when I am introducing another load test as
Search Account with 100 user load

I am getting <b> active security negotiations exception</b> from organization.svc of MS CRM as a fault with server too busy.