2009-07-01 19 views
1

Ich habe diesen Code, der in einem Komponententest funktioniert, aber nicht funktioniert, wenn im Kontext eines Plugins ausgeführt. Was der Code tut, ist versuchen, einen Lead zu erstellen, indem Sie den crm4 Webservice aufrufen.Fehler 401 Aufruf crm4 Webservice

Wenn das Plugin ausführt ich die folgende Ausnahme erhalten: „HTTP-Status 401: Unauthorized“

Dies ist der Code, der eine Instanz des Webservice initialisiert

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

jemand Rat haben auf, was ich kann als nächstes versuchen? Der Lead wird erstellt, wenn der Test ausgeführt wird, und die Konfigurationsinformationen sind im Komponententest identisch, wie wenn die App das Plug-in ausführt.

+0

Ok dumm mich. Jemand anders hat die Konfiguration im Plugin-Runner geändert, um ein älteres Plugin anstelle des neueren zu laden. Dieser Code funktioniert tatsächlich. –

Antwort

0

Anstatt die CrmService selbst instanziiert wird, alternativ können Sie die CrmService erhalten, indem die Referenz des IPluginExecutionContext zu erhalten und rufen Sie das CreateCrmService Methode

dieser link entnehmen Sie bitte Bezug auf die CrmService von IPluginExecutionContext Schaffung

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

Grüße,

hadi

+0

Danke Hadi. Wenn nur mein Code/das/Art von Plugin wäre. Das Plugin, auf das ich mich bezog, wird im Kontext eines Nachrichtenbusses ausgeführt. Jemand hatte die Konfiguration des MB zurückgerollt, so dass es begann, eine ältere Cache-Version meines Plugins zu laden - die zufällig auf eine ältere Instanz von crm zeigte. Ich mache dich aber :) –