---
title: "保留労働者を作成する"
slug: "create-a-deferred-worker"
abstract: "既知の連絡先を作るためにデフレードワーカーを作成する方法。"
---
# 保留労働者を作成する

以下のサンプル [の繰延労働者](https://doc.sitecore.com/xp/ja/developers/100/sitecore-experience-platform/workers.html "Workers") は、xConnectクライアントAPIを使って既知の連絡先を作成します。ワーカーはカスタムのワーカーオプション辞書(SampleDeferredWorkerOptionsDictionary)を受け入れ、以下を定義します:

-   連絡先に使用する識別子と識別子のソース
-   作業員の完全資格名

## 保留労働者オプション辞書を作成する

DistributedWorkerOptionsDictionaryクラスを使ってカスタムの繰延労働者のタスクを登録できます。ただし、労働者の完全な資格名と各辞書項目の名前とフォーマットを把握している必要があります。誤りのリスクを減らすために、すべての繰延労働者向けにカスタムの労働者オプション辞書を作成することをお勧めします。労働者オプション辞書を作成するには:

-   SampleDeferredWorkerOptionsDictionary というクラスを作成し、以下のようにDeferredWorkerOptionsDictionaryを継承します。

using System.Collections.Generic;
using Newtonsoft.Json;
using Sitecore.Processing.Engine.Abstractions;

namespace Sitecore.Documentation.Examples
{
 public class SampleDeferredWorkerOptionsDictionary : DeferredWorkerOptionsDictionary
 {
 public SampleDeferredWorkerOptionsDictionary(string identifier, string identifierSource) : base("Sitecore.Documentation.Examples.SampleDeferredWorker, Sitecore.Documentation.Examples", CreateDictionary(identifier, identifierSource))
 {
 }

 JsonConstructor
 protected SampleDeferredWorkerOptionsDictionary(IDictionary<string, string> dictionary) : base(dictionary)
 {
 }

 public static SampleDeferredWorkerOptionsDictionary Parse(IReadOnlyDictionary<string, string> options)
 {
 return new SampleDeferredWorkerOptionsDictionary(options"Identifier", options"IdentifierSource");
 }

 private static IDictionary<string, string> CreateDictionary(string identifier, string identifierSource)
 {
 var dictionary = new Dictionary<string, string>
 {
 { "Identifier", identifier },
 { "IdentifierSource", identifierSource }
 };

 return dictionary;
 }
 }
}

先延べ労働者オプション辞書を作成する際:

-   IDictionary<string, string>パラメータを受け入れるコンストラクタを含める必要があります。コンストラクタにはJsonConstructor属性で装飾されなければなりません。このコンストラクタがない場合、処理エンジンはワーカーを実行しようとした際に次のエラーを出します:「System.FormatException: Could not deserialize JSON text.」

## 繰延労働者階級の創設

先延べ労働者(deferred worker)を作成する方法:

-   SampleDeferredWorkerというクラスを作成し、示されたようにIDeferredWorkerを実装します。
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Sitecore.Framework.Conditions;
    using Sitecore.Processing.Engine.Abstractions;
    using Sitecore.XConnect;
    
    namespace Sitecore.Documentation.Examples
    {
     public class SampleDeferredWorker : IDeferredWorker
     {
     private readonly ILogger \_logger;
     private readonly SampleDeferredWorkerOptionsDictionary \_options;
     private readonly IServiceProvider \_serviceProvider;
    
     public SampleDeferredWorker(ILogger logger, SampleDeferredWorkerOptionsDictionary options, IServiceProvider serviceProvider)
     {
     \_logger = logger;
     \_serviceProvider = serviceProvider;
     \_options = options;
     }
    
     public SampleDeferredWorker(ILogger logger, IReadOnlyDictionary<string, string> options, IServiceProvider serviceProvider) : this(logger, SampleDeferredWorkerOptionsDictionary.Parse(options), serviceProvider)
     {
     }
    
     public async Task RunAsync(CancellationToken token)
     {
     \_logger.LogInformation($"Run Worker: {GetType().Name}");
    
     using (IServiceScope scope = \_serviceProvider.CreateScope())
     {
     using (var xdbContext = scope.ServiceProvider.GetRequiredService<IXdbContext>())
     {
     var testContact = new Contact(
     new ContactIdentifier(\_options"IdentifierSource", \_options"Identifier", ContactIdentifierType.Known));
    
     xdbContext.AddContact(testContact);
    
     await xdbContext.SubmitAsync(CancellationToken.None);
    
     // We need to execute search operation to make sure that contacts are indexed
     List<Contact> contacts = await xdbContext.Contacts.Where(c => c.Identifiers.Any(i => i.Source == \_options"Identifier")).ToList(token);
     Condition.Ensures(contacts, nameof(contacts)).IsNotEmpty();
     }
     }
     }
    
     public void Dispose()
     {
     }
     }
    }
    

先延べ労働者を作成する場合:

-   IReadOnlyDictionary<string, string>パラメータを受け入れるコンストラクタが必要です。このコンストラクタを含めないと、処理エンジンは起動せず、次のエラーが表示されます:「System.InvalidOperationException: 型に適したコンストラクタが見つかりませんでした。」

## 設定中のレジスタワーカー

Cortex処理エンジンでワーカーを登録するには:

1.  C:\\<PathToxConnect>\\App\_data\\jobs\\continuous\\ProcessingEngine\\App\_Data\\Config\\Global\\Processing\\sc.Processing.Documentation.Examples.xmlというファイルを作成し、以下の構成を追加します:
    
    <Settings>
     <Sitecore>
     <Processing>
     <Services>
     <TaskServicesFactory>
     <Options>
     <SampleDeferredWorker>
     <Type>Sitecore.Documentation.Examples.SampleDeferredWorker, Sitecore.Documentation.Examples</Type>
     </SampleDeferredWorker>
     </Options>
     </TaskServicesFactory>
     </Services>
     </Processing>
     </Sitecore>
    </Settings>
    
2.  Cortex処理エンジンを再起動してください。これは構成の変更を登録するために必要です。
    

## カスタムの繰延労働者を使います

カスタムワーカーをコアロール環境(Content Managementなど)で使用するには:

-   示すようにSampleDeferredWorkerOptionsDictionaryクラスを使って遅延タスクを登録します:
    
    using System;
    using Microsoft.Extensions.DependencyInjection;
    using Sitecore.DependencyInjection;
    using Sitecore.Diagnostics;
    using Sitecore.Processing.Engine.Abstractions;
    
    namespace Sitecore.Documentation.Examples
    {
     public class Example
     {
     public async void AsyncExamples()
     {
     var \_taskManager = ServiceLocator.ServiceProvider.GetRequiredService<ITaskManager>();
    
     Guid taskId = await \_taskManager.RegisterDeferredTaskAsync(
     new SampleDeferredWorkerOptionsDictionary("MyrtleMcSitecore", "SampleIdentifierSource"),
     null,
     TimeSpan.FromHours(1))
     .ConfigureAwait(false);
     }
     }
    }