If you are using Microsoft.Xrm.Tooling.Connector.CrmServiceClient and relying on nuget to reference the assembly in your project (as you should) then you need to be aware of what seems to be an odd behavior but is, in fact, a bug fix.
Run the following code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| var s1 = $ @"Url=https://org1.crm.dynamics.com; AuthType=Office365; UserName=user@org1.onmicrosoft.com; Password=password org1" ; var s2 = $ @"Url=https://org2.crm.dynamics.com; AuthType=Office365; UserName=user@org2.onmicrosoft.com; Password=password org2" ; using ( var crmSvc = new CrmServiceClient(s1)) { Console.WriteLine($ "Ready={crmSvc.IsReady}" ); Console.WriteLine($ "User={crmSvc.GetMyCrmUserId()}" ); Console.WriteLine( $ "Org={crmSvc.ConnectedOrgFriendlyName}" ); } using ( var crmSvc = new CrmServiceClient(s2)) { Console.WriteLine($ "Ready={crmSvc.IsReady}" ); Console.WriteLine($ "User={crmSvc.GetMyCrmUserId()}" ); Console.WriteLine( $ "Org={crmSvc.ConnectedOrgFriendlyName}" ); } |
And get:
1
2
3
4
5
6
| Ready=True User=8fa1eeae-dead-beef-dead-b6b5240774e4 Org=org1 Ready=True User=8fa1eeae-dead-beef-dead-b6b5240774e4 Org= org1 |
Same user and org? Despite the use of the disposable object, connection didn’t change at all?!
Yes, this is correct and, if you were relying on similar code before, you were riding on top of the unintended buggy behavior. Correct and documented behavior is for the connection to be cached and reused unless you specify RequireNewInstance flag either as part of the connection string:
1
2
3
4
5
| var s2 = $ @"RequireNewInstance=True; Url=https://barbaz.crm.dynamics.com; AuthType=Office365; UserName=user@barbaz.onmicrosoft.com; Password=password" ; |
or as part of a constructor (useUniqueInstance parameter):
1
2
3
4
5
6
| CrmServiceClient crmSvc = new CrmServiceClient( "user@barbaz.onmicrosoft.com" , CrmServiceClient.MakeSecureString( "password" ), "NorthAmerica" , "barbaz" , useUniqueInstance: true , useSsl: true , isOffice365: true ); |
RequireNewInstance=True will now properly create a unique connection instance, RequireNewInstance=false, will now properly reuse the cached instance of the connection, default is ‘false’
If you are connecting to more than one instance in your code or, if you’d like to maintain two separate connections with the separate instances of metadata, cache, etc, make sure that you use this flag.
No comments:
Post a Comment