From 641f6e9bdcdd9bb0b232d430d2f2984d7e70c670 Mon Sep 17 00:00:00 2001 From: Jeroen Bobbeldijk Date: Tue, 19 May 2020 12:54:48 +0200 Subject: [PATCH] Make sure we can use the activity more than once --- .../Activities/ParseDocumentActivity.cs | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/KlippaOCRAPI/Activities/ParseDocumentActivity.cs b/KlippaOCRAPI/Activities/ParseDocumentActivity.cs index 1d6eca0..13d9585 100644 --- a/KlippaOCRAPI/Activities/ParseDocumentActivity.cs +++ b/KlippaOCRAPI/Activities/ParseDocumentActivity.cs @@ -81,15 +81,15 @@ public class ParseDocument: AsyncCodeActivity public OutArgument APIResponse { get; set; } private HttpClient httpClient; - private MultipartFormDataContent formData; - private FileStream fs; public ParseDocument() { BasePath = "https://custom-ocr.klippa.com/api/v1"; PDFTextExtraction = PDFTextExtractionType.Fast; - httpClient = new HttpClient(); - formData = new MultipartFormDataContent(); + httpClient = new HttpClient + { + Timeout = TimeSpan.FromSeconds(600) + }; } protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) @@ -145,23 +145,18 @@ protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResul } finally { - if (httpClient != null) - { - httpClient.Dispose(); - } - if (formData != null) + if (task.Result != null) { - formData.Dispose(); - } - if (fs != null) - { - fs.Dispose(); + task.Result.Dispose(); } } } Task ExecuteAsync(CodeActivityContext context) { + MultipartFormDataContent formData = new MultipartFormDataContent(); + FileStream fs = null; + var basePath = BasePath.Get(context); var request = new HttpRequestMessage() { @@ -225,8 +220,7 @@ Task ExecuteAsync(CodeActivityContext context) } request.Content = formData; - request.Properties["RequestTimeout"] = TimeSpan.FromSeconds(60); - httpClient.Timeout = TimeSpan.FromSeconds(60); + request.Properties["RequestTimeout"] = TimeSpan.FromSeconds(600); return httpClient.SendAsync(request); }