- リリース ノート
- 基本情報
- セットアップと構成
- オートメーション プロジェクト
- 依存関係
- ワークフローの種類
- ファイルの比較
- オートメーションのベスト プラクティス
- ソース管理との連携
- デバッグ
- 診断ツール
- ワークフロー アナライザー
- 変数
- 引数
- インポートされた名前空間
- コード化されたオートメーション
- はじめに
- カスタム サービスを登録する
- Before および After コンテキスト
- トリガーベースの有人オートメーション
- 制御フロー
- オブジェクト リポジトリ
- ログ
- ScreenScrapeJavaSupport ツール
- Studio でのテスト
- 拡張機能
- トラブルシューティング

Studio ガイド
カスタム サービスを登録する
コード化されたオートメーションを強化するには、カスタム サービスの登録を検討してください。カスタム サービスを登録すると、プロジェクトでグローバルに使用できます。これにより、単一のコード化されたオートメーションだけでなく、プロジェクト内のすべてのコード化されたオートメーションでアクセスできます。
- コード ソース ファイルを作成し、作成するサービス名を指定してパブリック インターフェイスを定義します。このインターフェイスには、カスタム サービスで使用するメソッドが一覧表示されます。各メソッドは、インターフェイスを定義するのと同じファイルを継承する別のクラスに実装する必要があります。
この例では、インターフェイスに IMyService という名前を付けて、その中のメソッド DoMyMethod を呼び出します。
{
public interface IMyService
{
void DoMyMethod();
}
}
{
public interface IMyService
{
void DoMyMethod();
}
}
- 別のコード ソース ファイルに進み、以前に作成したインターフェイスからメソッドを実装します。このクラスは、パブリック インターフェイスが作成されたのと同じコード ソース ファイルから継承する必要があります。
この例では、変数の値を出力するための 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);
}
}
}
- 部分クラスを構築してカスタム サービスをラップし、このクラスを継承するコード化されたワークフローからアクセスできるようにします。
- 別のコード ソース ファイルを作成し、
Coded Workflowという名前のパブリック部分クラスを宣言します。このクラスにCodedWorkflowBaseクラスを継承させます。 - 作成したカスタム サービスを取得するための専用のプロパティを作成し、サービス インターフェイスのインスタンスへのアクセスを許可します。
- 別のコード ソース ファイルを作成し、
インスタンスは、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 になります。