示例#1
0
        public override void ExecuteWorkflow()
        {
            tracingService.Trace($"Starting custom workflow {className}");

            string body    = string.Empty;
            var    content = new StringContent(body, Encoding.UTF8, "application/json");
            Task <HttpResponseMessage> response;
            RequestMethodEnum          requestMethod;
            StringBuilder delimitedHeaders = new StringBuilder();

            try
            {
                // Check if there is a body to be sent
                if (!string.IsNullOrEmpty(RequestBody.Get <string>(context)))
                {
                    body = RequestBody.Get <string>(context);
                }

                tracingService.Trace($"body {body}");

                using (var client = new HttpClient())
                {
                    requestMethod = GetRequestMethod(RequestMethod.Get <string>(context));

                    tracingService.Trace($"requestMethod {requestMethod}");
                    tracingService.Trace($"Calling SendRequest...");

                    response = SendRequest(context, client, requestMethod, content);

                    tracingService.Trace($"SendRequest was called");

                    // Check if reponse was a success, otherwise thrown an exception
                    response.Result.EnsureSuccessStatusCode();

                    tracingService.Trace($"HTTP call was called successfully");

                    // Add a delimiter (;) for every header row returned
                    foreach (var header in response.Result.Headers)
                    {
                        if (delimitedHeaders.Length > 0)
                        {
                            delimitedHeaders.Append(";");
                        }

                        delimitedHeaders.Append($"{header.Key}:{header.Value}");
                    }

                    // Set Response Header output parameter
                    ResponseHeaders.Set(context, delimitedHeaders.ToString());

                    tracingService.Trace($"ResponseHeaders {ResponseHeaders.Get<string>(context)}");

                    // Set Response Body output parameter
                    var responseString = response.Result.Content.ReadAsStringAsync();
                    ResponseBody.Set(context, responseString.Result);

                    tracingService.Trace($"ResponseBody {ResponseBody.Get<string>(context)}");
                }
            }
            catch (Exception ex)
            {
                tracingService.Trace($"Exception: {ex.Message}");
                tracingService.Trace($"StackTrace: {ex.StackTrace}");
                tracingService.Trace($"InnerException: {ex.InnerException}");

                // Set Response Header output parameter
                ResponseHeaders.Set(context, "HTTP call was failed");

                // Instead of throwing an error, sending back the response header with a texting indicating an error
                //throw new InvalidPluginExecutionException(ex.Message);
            }
            finally
            {
                tracingService.Trace($"ResponseHeaders {ResponseHeaders.Get<string>(context)}");
                tracingService.Trace($"ResponseBody {ResponseBody.Get<string>(context)}");
                tracingService.Trace($"Finished custom workflow {className}");
            }
        }