当前位置: 首页>>代码示例>>C#>>正文


C# IJobExecutionContext.GetType方法代码示例

本文整理汇总了C#中IJobExecutionContext.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IJobExecutionContext.GetType方法的具体用法?C# IJobExecutionContext.GetType怎么用?C# IJobExecutionContext.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IJobExecutionContext的用法示例。


在下文中一共展示了IJobExecutionContext.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Execute

        public void Execute(IJobExecutionContext context)
        {
            JobDataMap data = context.MergedJobDataMap;
            var jobName = context.JobDetail.Key.Name;

            string actionType = GetRequiredParameter(data, ActionType, jobName);
            string method = GetRequiredParameter(data, Method, jobName);
            string uri = GetRequiredParameter(data, Uri, jobName);
            string body = GetOptionalParameter(data, Body) ?? string.Empty;
            string contentType = GetOptionalParameter(data, ContentType) ?? "text/plain";

            // Parse tokens in uri. This allows passing context property values, such as FireInstanceId, in the query string
            var r = new Regex(Regex.Escape("{$") + "(.*?)" + Regex.Escape("}"));
            MatchCollection matches = r.Matches(uri);
            foreach (object match in matches)
            {
                var token = match.ToString();
                var tokenValue = token.Replace("{$", "").Replace("}", "");

                PropertyInfo prop = context.GetType().GetProperty(tokenValue, BindingFlags.Public | BindingFlags.Instance);
                if (null != prop && prop.CanRead)
                {
                    var val = prop.GetValue(context);

                    if (null != val)
                    {
                        uri = uri.Replace(token, val.ToString());
                    }
                }
            }

            if (uri.ToLower().StartsWith("http"))
            {
                uri = uri.Replace("http://", "");
                uri = uri.Replace("https://", "");
                uri = uri.Replace("HTTP://", "");
                uri = uri.Replace("HTTPS://", "");
            }

            if (!actionType.Contains("://"))
            {
                actionType += "://";
            }

            // Execute WebRequest
            System.Net.WebRequest request = System.Net.WebRequest.Create(actionType + uri);
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            request.Method = method;

            Stream dataStream;

            // Create POST/PUT data and convert it to a byte array.
            if (method.ToUpper() == "PUT" || method.ToUpper() == "POST")
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(body);
                request.ContentType = contentType;
                request.ContentLength = byteArray.Length;

                dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
            }

            // Get the response.
            try
            {
                WebResponse response = request.GetResponse();
                Logger.InfoFormat("WebRequestJob server response status: {0}", ((HttpWebResponse) response).StatusDescription);

                // Get the stream containing content returned by the server.
                using (dataStream = response.GetResponseStream())
                {
                    if (dataStream != null)
                    {
                        var reader = new StreamReader(dataStream);
                        string responseFromServer = reader.ReadToEnd();
                        context.Result = responseFromServer;
                        Logger.InfoFormat("WebRequestJob server response content: {0}", responseFromServer);

                        reader.Close();
                    }
                }
                response.Close();
            }
            catch (WebException ex)
            {
                Logger.Error(string.Format("Error in WebRequestJob ({0}):", jobName), ex);
                context.Result = ex.Message;

                if (ex.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse) ex.Response)
                    {
                        var stream = errorResponse.GetResponseStream();
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                string error = reader.ReadToEnd();
                                Logger.ErrorFormat("Error in WebRequestJob ({0}): {1}", jobName, error);
//.........这里部分代码省略.........
开发者ID:R-Suite,项目名称:R.Scheduler,代码行数:101,代码来源:WebRequestJob.cs


注:本文中的IJobExecutionContext.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。