我有这样的代码,在一个单元测试工作,但是当在一个插件的上下文中执行不工作。什么代码所做的是试图通过调用webservice的crm4创造领先优势。

当该插件执行I出现以下情况例外: “HTTP状态401:未经授权”

这是初始化该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"));

任何人有什么我可以尝试下建议?创建引线的测试运行时,和设定信息是在单元测试一样,它当应用程序正在执行的插件。

有帮助吗?

解决方案

而不是自行实例化CrmService,或者可以通过获得IPluginExecutionContext的基准获得CrmService并调用CreateCrmService方法

请参阅此链接

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

此致

哈迪

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top