Beispiel #1
0
        public void LogPluginFeedback(LogClass log, Microsoft.Xrm.Sdk.ITracingService trace)
        {
            try
            {
                WebRequestData             wrData       = new WebRequestData();
                MemoryStream               memoryStream = new MemoryStream();
                DataContractJsonSerializer serializer   = new DataContractJsonSerializer(typeof(LogClass));
                serializer.WriteObject(memoryStream, log);
                var jsonObject = Encoding.Default.GetString(memoryStream.ToArray());
                memoryStream.Dispose();
                wrData.InputData     = jsonObject;
                wrData.ContentType   = "application/json";
                wrData.Authorization = "Bearer " + Configuration.TokenForWebAPI;
                wrData.Url           = AboxServices.CrmWebAPILog;

                try
                {
                    using (WebClient client = new WebClient())
                    {
                        var webClient = new WebClient();
                        if (wrData.ContentType != "")
                        {
                            webClient.Headers[HttpRequestHeader.ContentType] = wrData.ContentType;
                        }
                        if (wrData.Authorization != "")
                        {
                            webClient.Headers[HttpRequestHeader.Authorization] = wrData.Authorization;
                        }

                        // var code = "key";
                        string serviceUrl = wrData.Url;
                        webClient.UploadString(serviceUrl, wrData.InputData);

                        trace.Trace("Url:" + wrData.Url + " | Data:" + wrData.InputData);
                    }
                }
                catch (WebException wex)
                {
                    string error      = "";
                    string statusCode = "";
                    if (wex.Response != null)
                    {
                        using (var errorResponse = (HttpWebResponse)wex.Response)
                        {
                            using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                            {
                                error = reader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                trace.Trace($"MethodName: {new System.Diagnostics.StackTrace(ex).GetFrame(0).GetMethod().Name}|--|Exception: " + ex.ToString());
                throw;
            }
        }
Beispiel #2
0
        public bool IsValidLastname(string input, Microsoft.Xrm.Sdk.ITracingService trace)
        {
            try
            {
                string pattern = Constants.RegexValidLastname;
                Match  m       = Regex.Match(@input, pattern);
                return(m.Success);
            }
            catch (Exception ex)
            {
                this.LogPluginFeedback(new LogClass
                {
                    Exception  = ex.ToString(),
                    Level      = "error",
                    ClassName  = this.GetType().ToString(),
                    MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name,
                    Message    = $"",
                    ProcessId  = ""
                }, trace);

                trace.Trace($"MethodName: {new System.Diagnostics.StackTrace(ex).GetFrame(0).GetMethod().Name}|--|Exception: " + ex.ToString());
                throw;
            }
        }
Beispiel #3
0
        public WebRequestResponse DoGetRequest(WebRequestData requestData, Microsoft.Xrm.Sdk.ITracingService trace)
        {
            try
            {
                #region debug log

                this.LogPluginFeedback(new LogClass
                {
                    Exception  = "",
                    Level      = "debug",
                    ClassName  = this.GetType().ToString(),
                    MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name,
                    Message    = $"GETing Url:{requestData.Url}",
                    ProcessId  = ""
                }, trace);

                #endregion debug log
            }
            catch (Exception exc)
            {
            }

            WebRequestResponse wrResponse = new WebRequestResponse();
            try
            {
                try
                {
                    using (WebClient client = new WebClient())
                    {
                        var webClient = new WebClient();
                        if (requestData.ContentType != "")
                        {
                            webClient.Headers[HttpRequestHeader.ContentType] = requestData.ContentType;
                        }

                        if (requestData.Authorization != "")
                        {
                            webClient.Headers[HttpRequestHeader.Authorization] = requestData.Authorization;
                        }

                        // var code = "key";
                        string serviceUrl = requestData.Url;
                        wrResponse.Data = webClient.DownloadString(serviceUrl);

                        if (wrResponse.Data != "")
                        {
                            wrResponse.IsSuccessful = true;

                            //TODO:ELiminar para Produccion
                            try
                            {
                                this.LogPluginFeedback(new LogClass
                                {
                                    Exception  = "",
                                    Level      = "debug",
                                    ClassName  = this.GetType().ToString(),
                                    MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name,
                                    Message    = $"Url:{requestData.Url} ResponseFromRequest:{wrResponse.Data}",
                                    ProcessId  = ""
                                }, trace);
                            }
                            catch (Exception e)
                            {
                            }
                        }
                    }
                }
                catch (WebException wex)
                {
                    string error      = "";
                    string statusCode = "";
                    if (wex.Response != null)
                    {
                        using (var errorResponse = (HttpWebResponse)wex.Response)
                        {
                            using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                            {
                                error = reader.ReadToEnd();
                            }
                        }
                    }

                    #region error log

                    this.LogPluginFeedback(new LogClass
                    {
                        Exception  = wex.ToString(),
                        Level      = "error",
                        ClassName  = this.GetType().ToString(),
                        MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name,
                        Message    = $"{error}",
                        ProcessId  = ""
                    }, trace);

                    #endregion error log

                    wrResponse.Data         = null;
                    wrResponse.ErrorMessage = wex.ToString();
                    wrResponse.IsSuccessful = false;
                }
            }
            catch (Exception ex)
            {
                this.LogPluginFeedback(new LogClass
                {
                    Exception  = ex.ToString(),
                    Level      = "error",
                    ClassName  = this.GetType().ToString(),
                    MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name,
                    Message    = $"Excepción realizando el POST request",
                    ProcessId  = ""
                }, trace);

                trace.Trace($"MethodName: {new System.Diagnostics.StackTrace(ex).GetFrame(0).GetMethod().Name}|--|Exception: " + ex.ToString());
                wrResponse.Data         = null;
                wrResponse.IsSuccessful = false;
                wrResponse.ErrorMessage = ex.ToString();
            }

            return(wrResponse);
        }