...
Блок кода |
---|
language | c# |
---|
theme | RDark |
---|
title | Пример IDisposableService |
---|
linenumbers | true |
---|
collapse | true |
---|
|
using System;
using System.Collections.Generic;
using Robin.ActionSDK;
using Robin.Engine.Services.Interfaces;
using Robin.Types;
namespace Robin.SdkExamples
{
public class OpenFile : BaseRobinAction
{
private readonly IDisposeService _disposeService;
public OpenFile(IActionLogger logger, IDisposeService disposeService) : base(logger)
{
_disposeService = disposeService;
}
public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
{
var filePath = ((RobinFilePath)parameters["filePath"]).Value;
var file = new File(filePath);
file.Open();
_disposeService.RegisterResource(file);
return new Dictionary<string, object> { { "file", file } };
}
}
internal class File : IDisposable
{
private readonly string _filePath;
private bool _isDisposed;
public File(string filePath)
{
_filePath = filePath;
}
public void Open() { }
public bool TryReadNextLine(out string line) { }
public void Close() { }
public void Dispose()
{
if (_isDisposed) return;
Close();
_isDisposed = true;
}
}
} |
ArtifactsFolderService
Сервис для получения полного пути до папки с артефактами робота.
Методы
Блок кода |
---|
|
string GetArtifactsFolderPath(); |
Примеры использования
ResourcesFolderService
Сервис для получения полного пути до папки с ресурсами робота, в этой папке находятся файлы, которые были добавлены к роботу в студии.
Методы
Блок кода |
---|
|
string GetResourcesFolderPath(); |
Примеры использования
Блок кода |
---|
|
using System.Collections.Generic;
using System.IO;
using Robin.ActionSDK;
using Robin.ActionSDK.Exceptions;
using Robin.Engine.Services.Interfaces;
using Robin.Types;
namespace Robin.SdkExamples
{
public class ReadFile : BaseRobinAction
{
private readonly IResourcesFolderService _resourcesFolderService;
public ReadFile(IActionLogger logger, IResourcesFolderService resourcesFolderService) : base(logger)
{
_resourcesFolderService = resourcesFolderService;
}
public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
{
var fileName = ((RobinFilePath)parameters["fileName"]).Value;
if (!TryResolveFilePath(fileName, out var filePath))
throw new RobinException($"Файл не найден {fileName}", "Robin.Exception.FileNotFound");
var text = File.ReadAllText(filePath);
return new Dictionary<string, object>
{
{"text", text}
};
}
private bool TryResolveFilePath(string fileName, out string filePath)
{
filePath = fileName;
if (File.Exists(fileName))
return true;
filePath = Path.Combine(_resourcesFolderService.GetResourcesFolderPath(), fileName);
return File.Exists(filePath);
}
}
} |
RobinDeserializeService