Domanda

Ho questo codice che funziona in una prova di unità, ma non funziona quando viene eseguito nel contesto di un plugin. Quello che il codice fa è cercare di creare un vantaggio chiamando il webservice crm4.

Quando il plugin esegue ottengo la seguente eccezione: "stato HTTP 401: non autorizzato"

Questo è il codice che inizializza un'istanza del 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"));

Qualcuno ha consigli su cosa posso provare dopo? Il piombo viene creato quando il test viene eseguito, e le informazioni di configurazione è la stessa nel test di unità come è quando l'applicazione è in esecuzione il plugin.

È stato utile?

Soluzione

Piuttosto che istanziare il CrmService da soli, in alternativa, è possibile ottenere la CrmService ottenendo il riferimento del IPluginExecutionContext e invocare il metodo CreateCrmService

Si prega di fare riferimento a questo link quanto riguarda la creazione del CrmService da 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"));
}

Saluti,

Hadi

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top