c# – Microsoft Dynamics CRM SDK CRMServiceClient连接字符串缓存错误

最后一个Dynamics SDK的缓存行为让我发疯.

首先,如果要使用CrmServiceClient访问不同的环境,则必须使用参数RequireNewInstance = True;在连接字符串中.如果没有,即使您创建实例并将实例部署到不同的环境,CrmServiceClient的每个实例都将使用相同的连接.

现在,即使你使用RequireNewInstance = True;在连接字符串中,我发现在某些情况下仍然会出现缓存.

var client1 = new CrmServiceClient("RequireNewInstance=True;
Url=https://myCompany.crm.dynamics.com;
Username=myUser@myCompany.onmicrosoft.com; Password=myPassowrd;
AuthType=Office365");

//Now, client 2 points to a url that doesn’t exists:
var client2 = new CrmServiceClient("RequireNewInstance=True; 
Url=https://xxx.crm.dynamics.com; Username=myUser@myCompany.onmicrosoft.com; 
Password=myPassowrd; AuthType=Office365");

client2继续使用第一个连接字符串,因此您无法确定新连接字符串是否正确.

有关如何在我的asp.net应用程序中正确测试Dynamics Crm连接字符串的任何想法?

最佳答案 我同意如果你不包括RequireNewInstance = true就选择重用现有连接似乎反直觉,但我无法重现你所看到的.如果我从LinqPad尝试以下内容,crmSvcClient2将打印出错误,然后在Execute调用(SDK的8.2.0.2版本)上抛出一个空引用.使用此版本的SDK,您需要在连接后始终检查LastCrmError以查看连接是否失败.

var connectionString = @"AuthType=Office365;Url=https://REAL.crm.dynamics.com;Username=USERNAME;Password=PASSWORD;RequireNewInstance=True;";
var connectionString2 = @"AuthType=Office365;Url=https://FAKE.crm.dynamics.com;Username=USERNAME;Password=PASSWORD;RequireNewInstance=True;";   

using (var crmSvcClient = new CrmServiceClient(connectionString))
{
    "crmSvcClient".Dump();
    crmSvcClient.LastCrmError.Dump();
    ((WhoAmIResponse)crmSvcClient.Execute(new WhoAmIRequest())).OrganizationId.Dump();
    crmSvcClient.ConnectedOrgFriendlyName.Dump();


}
using (var crmSvcClient2 = new CrmServiceClient(connectionString2))
{
    "crmSvcClient2".Dump();
    crmSvcClient2.LastCrmError.Dump();
    ((WhoAmIResponse)crmSvcClient2.Execute(new WhoAmIRequest())).OrganizationId.Dump();
    crmSvcClient2.ConnectedOrgFriendlyName.Dump();
}

《c# – Microsoft Dynamics CRM SDK CRMServiceClient连接字符串缓存错误》

点赞