1

我有这段代码可以在单元测试中使用,但在插件上下文中执行时不起作用。代码所做的是尝试通过调用 crm4 网络服务来创建潜在客户。

当插件执行时,我得到以下异常:“HTTP status 401: Unauthorized”

这是初始化 web 服务实例的代码

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = GetConfig("crm.organisation_name");
_crmService = new CrmService(GetConfig("webservice.crm"));

_crmService.CrmAuthenticationTokenValue = token;
_crmService.UseDefaultCredentials = false;                  
_crmService.PreAuthenticate = false;
_crmService.Credentials = new NetworkCredential(GetConfig("crm.user_username"),
                                                GetConfig("crm.user_password"), 
                                                GetConfig("crm.user_domain"));

有人对我下一步可以尝试什么有建议吗?Lead 是在测试运行时创建的,单元测试中的配置信息与应用程序执行插件时的配置信息相同。

4

1 回答 1

0

与其自己实例化CrmService,也可以通过获取IPluginExecutionContext的引用并调用CreateCrmService方法来获取CrmService

有关从 IPluginExecutionContext 创建 CrmService,请参阅此链接

Here is some code snippet

public void Execute(IPluginExecutionContext context)
{
  // the below code means, the CrmService will be created 
  // by referring to the user account who is registered to 
  // run the CRM Application Pool
  ICrmService crmService = context.CreateCrmService(false);

  // the below code means, the CrmService will be created
  // by taking account the user account who login and run
  // the current plugin
  ICrmService crmService = context.CreateCrmService(true);

  // the below code means, the CrmService will be created
  // by impersonating a valid user
  ICrmService crmService = context.CreateCrmService(new Guid("3F2504E0-4F89-11D3-9A0C-0305E82C3301"));
}

问候,

哈迪

于 2009-07-01T12:49:12.670 回答