studio
2023.10
false
重要 :
このコンテンツの一部は機械翻訳によって処理されており、完全な翻訳を保証するものではありません。 新しいコンテンツの翻訳は、およそ 1 ~ 2 週間で公開されます。
UiPath logo, featuring letters U and I in white

Studio ガイド

最終更新日時 2026年2月18日

カスタム サービスを登録する

コード化されたオートメーションを強化するには、カスタム サービスの登録を検討してください。カスタム サービスを登録すると、プロジェクトでグローバルに使用できます。これにより、単一のコード化されたオートメーションだけでなく、プロジェクト内のすべてのコード化されたオートメーションでアクセスできます。

  1. コード ソース ファイルを作成し、作成するサービス名を指定してパブリック インターフェイスを定義します。このインターフェイスには、カスタム サービスで使用するメソッドが一覧表示されます。各メソッドは、インターフェイスを定義するのと同じファイルを継承する別のクラスに実装する必要があります。

この例では、インターフェイスに IMyService という名前を付けて、その中のメソッド DoMyMethod を呼び出します。

{
    public interface IMyService
    {
        void DoMyMethod();
    }
}
{
    public interface IMyService
    {
        void DoMyMethod();
    }
}
  1. 別のコード ソース ファイルに進み、以前に作成したインターフェイスからメソッドを実装します。このクラスは、パブリック インターフェイスが作成されたのと同じコード ソース ファイルから継承する必要があります。

この例では、変数の値を出力するための DoMyMethod メソッドを実装します。

public class MyService : IMyService
    {
        public void DoMyMethod()
        {
            var a = "hello world";
            Console.WriteLine(a);
        }
    }
}
public class MyService : IMyService
    {
        public void DoMyMethod()
        {
            var a = "hello world";
            Console.WriteLine(a);
        }
    }
}
  1. 部分クラスを構築してカスタム サービスをラップし、このクラスを継承するコード化されたワークフローからアクセスできるようにします。
    1. 別のコード ソース ファイルを作成し、Coded Workflow という名前のパブリック部分クラスを宣言します。このクラスに CodedWorkflowBase クラスを継承させます。
    2. 作成したカスタム サービスを取得するための専用のプロパティを作成し、サービス インターフェイスのインスタンスへのアクセスを許可します。

インスタンスは、serviceContainer.Resolve<IMyService>() メソッドを使用した依存関係のインジェクションによって取得されます。これにより、部分クラス内のカスタム サービスとの対話方法が提供されます。

  ```
  public IMyService myService { get => serviceContainer.Resolve<IMyService>()};
  ```
  ```
  public IMyService myService { get => serviceContainer.Resolve<IMyService>()};
  ```

3. クラスをさらに拡張するには、RegisterServices メソッドを追加します。このメソッドを呼び出すと、UiPath サービス コンテナー内にカスタム サービスが登録されます。

  ```
  protected override void RegisterServices(ICodedWorkflowsServiceLocator serviceLocator)
  		{
  			serviceLocator.RegisterType<IMyService, MyService>();
  		}
  ```
  ```
  protected override void RegisterServices(ICodedWorkflowsServiceLocator serviceLocator)
  		{
  			serviceLocator.RegisterType<IMyService, MyService>();
  		}
  ```

RegisterTypeの代わりに RegisterInstance を使用することもできます。2 つの実装とその署名方法について、次の違いを確認してください。 * RegisterType - RegisterTypeを使用する場合は、呼び出すたびにサービスの新しいコピーまたはインスタンスを作成するコンテナーを指定します。RegisterType 、毎回新しいサービスを構築するための基準となる青写真が示されています。+ TService: これは、サービスによって使用されるサービス インターフェイスの種類です。これは、サービス用に作成するインターフェイスです。この例では、 IMyServiceです。+ TServiceImplementation: TService インターフェイスを実装するクラスです。このクラスは、 TService 型のサービスが要求されたときに作成されます。+ registrationType:デフォルトは CodeServiceRegistrationType.Singletonです。サービス登録ポリシーを設定します。

    ```
    void RegisterType<TService, TServiceImplementation>(CodeServiceRegistrationType registrationType = CodeServiceRegistrationType.Singleton)
                where TService : class
                where TServiceImplementation : class, TService;
    ```
  * `RegisterInstance` - When you use `RegisterInstance`, you offer the container a ready-made instance of that service, that you already created before. Every time you call the service, the container returns this exact pre-made service. `RegisterInstance` returns an exact copy of the service that you created.
    + `TServiceImplementation`: This is the ready-made instance of a class that you have already created. This instance is returned whenever a service of this type needs to be resolved. For this example, `MySecondService`.
    + `serviceTypes`: This is an array of types that the instance will be available as, when calling it.

    ```
    void RegisterInstance<TServiceImplementation>(TServiceImplementation instance, params Type[] serviceTypes) 
                where TServiceImplementation : class;
    ```
    ```
    void RegisterType<TService, TServiceImplementation>(CodeServiceRegistrationType registrationType = CodeServiceRegistrationType.Singleton)
                where TService : class
                where TServiceImplementation : class, TService;
    ```
  * `RegisterInstance` - When you use `RegisterInstance`, you offer the container a ready-made instance of that service, that you already created before. Every time you call the service, the container returns this exact pre-made service. `RegisterInstance` returns an exact copy of the service that you created.
    + `TServiceImplementation`: This is the ready-made instance of a class that you have already created. This instance is returned whenever a service of this type needs to be resolved. For this example, `MySecondService`.
    + `serviceTypes`: This is an array of types that the instance will be available as, when calling it.

    ```
    void RegisterInstance<TServiceImplementation>(TServiceImplementation instance, params Type[] serviceTypes) 
                where TServiceImplementation : class;
    ```

以下のコード スニペットは、パブリック部分クラスの実装方法の例を示してします。

public partial class CodedWorkflow : CodedWorkflowBase
    {
		public IMyService myService { get => serviceContainer.Resolve<IMyService>() ; }
		
		protected override void RegisterServices(ICodedWorkflowsServiceLocator serviceLocator)
		{
                    // Implementation using 'RegisterType'
			serviceLocator.RegisterType<IMyService, MyService>();
                    // Implementation using 'RegisterInstance'
                    var secondService = new MySecondService();
                    serviceLocator.RegisterInstance<MySecondService>(secondService);
		}
public partial class CodedWorkflow : CodedWorkflowBase
    {
		public IMyService myService { get => serviceContainer.Resolve<IMyService>() ; }
		
		protected override void RegisterServices(ICodedWorkflowsServiceLocator serviceLocator)
		{
                    // Implementation using 'RegisterType'
			serviceLocator.RegisterType<IMyService, MyService>();
                    // Implementation using 'RegisterInstance'
                    var secondService = new MySecondService();
                    serviceLocator.RegisterInstance<MySecondService>(secondService);
		}

次のステップ

作成したカスタム サービスを使用するには、using ステートメントを使用して、作業中のファイル内でそのサービスの名前空間を参照します。カスタム サービスの名前空間には、プロジェクトの名前と .cs ファイルが存在する可能性のあるフォルダー/サブフォルダーの名前が含まれます。つまり、名前空間の形式は「<ProjectName>.<Folder>.<SubFolder>」のようになります。

たとえば、MyCustomService フォルダーにある GeneralCustomServices という名前のプロジェクト内にカスタム サービスを作成した場合、そのサービスの名前空間は GeneralCustomServices.MyCustomService になります。

  • 次のステップ

このページは役に立ちましたか?

サポートを受ける
RPA について学ぶ - オートメーション コース
UiPath コミュニティ フォーラム
Uipath Logo
信頼とセキュリティ
© 2005-2026 UiPath. All rights reserved.