UiPath Documentation
studio
2023.10
false
重要 :
新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

Studio 用户指南

上次更新日期 2026年4月28日

注册自定义服务

要增强编码自动化,请考虑注册自定义服务。通过注册自定义服务,您便可在项目中全局使用该服务。这样,它不仅可以在单个编码自动化中访问,还可以在项目中的所有编码自动化中访问。

  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>();
  		}
  ```

您也可以使用 RegisterInstance,而不是使用 RegisterType。 查看两个实现及其签名方法之间的以下差异:* 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

  • 后续步骤

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新