2009-07-01 49 views
1

我有這個代碼在單元測試中工作,但在插件的上下文中執行時不起作用。代碼所做的就是嘗試通過調用crm4 webservice來創建潛在客戶。錯誤401調用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")); 

任何人有意見就我所能代碼下次嘗試?測試運行時會創建潛在客戶,並且單元測試中的配置信息與應用執行插件時的配置信息相同。

+0

好愚蠢的我。其他人更改了插件運行器上的配置,以加載較舊的插件而不是較新的插件。這段代碼實際上有效。 – 2009-07-01 12:41:59

回答

0

不是自己實例化CrmService,或者你可以通過獲得IPluginExecutionContext的基準獲得CrmService並調用CreateCrmService方法

請參閱本link關於從IPluginExecutionContext

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

問候,

哈迪

+0

謝謝哈迪。如果只有我的代碼是/那/插件。我所指的插件是在messagebus的上下文中執行的。有人回滾了MB的配置,因此它開始加載我的插件的舊緩存版本 - 這恰好指向了一個較老的CRM實例。我會讓你起來,雖然:) – 2009-07-02 07:06:08