エラー401 crm4のWebサービスを呼び出します
-
21-08-2019 - |
質問
私はユニットテストで動作しますが、プラグインのコンテキストで実行された場合には動作しません。このコードを持っています。どのようなコードがないことcrm4 Webサービスを呼び出すことにより、リードを作成しようとしている。
プラグインは、私は次の例外を取得実行すると:「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メソッドを呼び出しますIPluginExecutionContextからCrmServiceの作成に関するこのリンクするを参照してください
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"));
}
よろしく、
ハディ
所属していません StackOverflow