web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Connection is not persistent in C#

(0) ShareShare
ReportReport
Posted on by 61

I have been working on task to connect to Dynamics CRM in C#.Net. I am quite new with CRM environment and not aware of what its capable of doing completely.

Although my connection is successful, recently I have started noticing the connection does not remain persistent. Sometime it connects, sometime it is not connecting.

I have been trying in haphazard manner to resolve this issue.

I am here to share few alternatives I have thought about and response to resolve this situation.

1. Is there any retry mechanism available to connect to CRM Dynamics?

2. I have applied "RequireNewInstance=true" in connection string, but still no luck.

3. If I close the browser and run the app again after initial failure, it works. (with fluke !)

I get below error:

Unable to Login to Dynamics CRM. OrganizationServiceProxy is null.

Any suggestions are welcomed.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Joseph McGregor Macdonald Profile Picture
    597 on at

    Hi binoyk

    I also experienced this problem with an always on web service integration connecting to Dynamics. this was a few years ago and my issue was the token would expire within the connection after a period of time. The IOrganisationService instance would then effectively fail and I would need to refresh the connection

    My solution to this problem was rather than coding directly against the SDK IOrganisationService type, I created a wrapper class around the IOrganisationService instance which called all messages through the IOrganisationService.Execute method. This created a single point of calling the Dynamics Web Service for all code. If the connection failed when calling this Execute method I would recreate the IOrganisationService instance within the wrapper object and retry the Execute method

    In case you are not aware all operations through the IOrganisationService can be done through the Execute method including Create, Update, Retreive, RetreiveMultiple etc. by using the relevant request class type (e.g. RetreiveMultipleRequest)

    This solution worked well and I have not encountered the same issue since

    Hope this helps

  • Suggested answer
    binoyk Profile Picture
    61 on at

    Hi Joseph,

    I appreciate your response. I found work around though.

    My current code looks like below as I use FetchXML in C#.

    Connection String:

    <add name="CRMConnectionString"

            connectionString="Url=MyDynamicsUrl.com/MyCRM;Domain=DomainName;Username=Domain\MyUsername;Password=testpwd;AuthType=IFD;RequireNewInstance=True" />

    I access above connection string in C# code as below.

                string fetchXml = string.Empty;
                try
                {
                    using (CrmServiceClient crmSvcClient = new CrmServiceClient(DynamicsServiceProxy.ServiceClient))
                    {
                        if (crmSvcClient.IsReady)
                        {
                            fetchXml = @"<fetch top='1'>
                                                      ………
                                                   </fetch>";
                            EntityCollection ec = crmSvcClient.RetrieveMultiple(new FetchExpression(fetchXml));
                            if (ec.Entities.Count > 0)
                            {
                                     /* additional code */
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception : {ex}", ex.Message);
                }
    Here in above code, "DynamicsServiceProxy.ServiceClient" contains my connection string.
    One thing I found for my work around is, earlier I was using AuthType=AD in my connection string. I changed it to using "IFD" as I am working on On-Premise environment. (Although AD should work. Not sure why not !)
    After switching to IFD and deploying my application again on server, it works without any issue.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans