错误401调用crm4 webservice

时间:2009-07-01 10:01:19

标签: web-services dynamics-crm crm dynamics-crm-4

我有这个代码在单元测试中工作,但在插件的上下文中执行时不起作用。代码所做的是尝试通过调用crm4 webservice来创建一个潜在客户。

当插件执行时,我得到以下异常:“HTTP状态401:未授权”

这是初始化webservice实例的代码

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"));

任何人都有关于我接下来可以尝试的建议吗?测试运行时会创建负责人,并且单元测试中的配置信息与应用程序执行插件时的配置信息相同。

1 个答案:

答案 0 :(得分:0)

您可以通过获取IPluginExecutionContext的引用并调用CreateCrmService方法来获取CrmService,而不是自己实例化CrmService

关于从IPluginExecutionContext创建CrmService,请参阅此link

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"));
}

此致

哈迪