Пример #1
0
        protected virtual void StopJob(bool success)
        {
            using (ILoggingOperation log = _logger.NormalOperation())
                using (ChannelFactory <ISchedulerService> ch = new ChannelFactory <ISchedulerService>(ClientUtilManager.Ask().EndpointConfigurationName))
                {
                    log.Wrap(() =>
                    {
                        ISchedulerService svc = ch.CreateChannel();

                        JobContextData data = GetContextData();
                        if (data == null)
                        {
                            log.Debug("No JobContextData found.");
                        }
                        else
                        {
                            svc.StopJob(data.JobId, success);
                        }
                    });
                }
        }
Пример #2
0
        protected override void RemoteCall(IJobExecutionContext context)
        {
            using (ILoggingOperation log = _logger.NormalOperation())
                using (TcpClient client = new TcpClient(Hostname, Convert.ToInt32(Port)))
                    using (NetworkStream stream = client.GetStream())
                    {
                        log.Wrap(() =>
                        {
                            stream.ReadTimeout  = _readTimeoutMillis;
                            stream.WriteTimeout = _writeTimeoutMillis;

                            byte[] tmp = Convert.FromBase64String(PayloadBase64);

                            StringBuilder result = new StringBuilder(tmp.Length + 200);
                            using (MemoryStream ms = new MemoryStream(tmp))
                                using (StreamReader sr = new StreamReader(ms))
                                {
                                    string line = null;
                                    while ((line = sr.ReadLine()) != null)
                                    {
                                        if (Regex.IsMatch(line, "Content-Length", RegexOptions.IgnoreCase))
                                        {
                                            result.AppendLine($"{JobContextData.JobDataHeaderName}:{JsonConvert.SerializeObject(new JobContextData() { JobGroup = context.JobDetail.Key.Group, JobId = context.JobDetail.Key.Name })}");
                                            log.Debug("added jobContextData");
                                        }
                                        result.AppendLine(line);
                                    }
                                    tmp = sr.CurrentEncoding.GetBytes(result.ToString());
                                }

                            log.Debug("calling service");
                            stream.Write(tmp, 0, tmp.Length);
                            log.Debug("request sent");

                            if (Convert.ToBoolean(FireAndForget) == false)
                            {
                                using (StreamReader sr = new StreamReader(stream))
                                {
                                    log.Debug("waiting response");

                                    StringBuilder b = new StringBuilder();
                                    string x        = null;
                                    int cntLength   = 0;
                                    while (x != "")
                                    {
                                        x       = sr.ReadLine();
                                        Match m = Regex.Match(x, "Content-Length:\\s*(\\d+)");
                                        if (m.Success)
                                        {
                                            cntLength = Convert.ToInt32(m.Groups[1].ToString());
                                        }
                                        b.AppendLine(x);
                                    }

                                    byte[] msg = new byte[cntLength];
                                    for (int i = 0; i < cntLength; i++)
                                    {
                                        msg[i] = (byte)sr.Read();
                                    }
                                    string res = b.ToString();

                                    log.Debug("response received");

                                    if (!Regex.IsMatch(res.Split('\n')[0], "20\\d"))
                                    {
                                        throw new Exception($"Error invoking service. {res}");
                                    }
                                }
                            }
                            else
                            {
                                log.Debug("FireAndForget set.");
                            }
                        });
                    }
        }
        protected override void RemoteCall(IJobExecutionContext context)
        {
            using (ILoggingOperation log = _logger.NormalOperation())
                using (HttpClient client = new HttpClient())
                {
                    log.Wrap(() =>
                    {
                        Dictionary <string, string> content = JsonConvert.DeserializeObject <Dictionary <string, string> >(ParametersJSON);
                        Dictionary <string, string> headers = JsonConvert.DeserializeObject <Dictionary <string, string> >(HeadersJSON);

                        headers.Add(JobContextData.JobDataHeaderName, JsonConvert.SerializeObject(new JobContextData()
                        {
                            JobGroup = context.JobDetail.Key.Group, JobId = context.JobDetail.Key.Name
                        }));
                        log.Debug("added jobContextData");

                        client.Timeout = TimeSpan.FromMilliseconds((int)_readTimeoutMillis + (int)_writeTimeoutMillis);

                        foreach (string hd in headers.Keys)
                        {
                            if (client.DefaultRequestHeaders.Contains(hd))
                            {
                                client.DefaultRequestHeaders.Remove(hd);
                            }
                            client.DefaultRequestHeaders.Add(hd, headers[hd]);
                        }

                        log.Debug("calling service");

                        Task <HttpResponseMessage> resp;
                        switch (Method.ToUpper())
                        {
                        case "GET":
                            resp = client.GetAsync(Destination);
                            break;

                        case "POST":
                            resp = client.PostAsync(Destination, new FormUrlEncodedContent(content));
                            break;

                        case "PUT":
                            resp = client.PutAsync(Destination, new FormUrlEncodedContent(content));
                            break;

                        default:
                            throw new ArgumentException($"Method {Method} not allowed");
                        }

                        log.Debug("request sent");

                        if (Convert.ToBoolean(FireAndForget) == false)
                        {
                            log.Debug("waiting response");
                            resp.Wait();
                            log.Debug("response received");
                            resp.Result.EnsureSuccessStatusCode();
                        }
                        else
                        {
                            log.Debug("FireAndForget set.");
                        }
                    });
                }
        }