/// <summary>
        /// Get current repository readme file
        /// </summary>
        /// <param name="organization"></param>
        /// <param name="projectName"></param>
        /// <returns></returns>
        public static string GetRepositoryMainReadmeFileLink(string organization, string projectName)
        {
            var readmeAddress = "";

            try
            {
                if (WebRequest.Create($"https://api.github.com/repos/{organization}/{projectName}/readme") is HttpWebRequest request)
                {
                    request.UserAgent = "TestApp";

                    using (var response = request.GetResponse() as HttpWebResponse)
                    {
                        if (response != null)
                        {
                            var reader = new StreamReader(response.GetResponseStream());

                            var json    = reader.ReadToEnd();
                            var results = JsonConvert.DeserializeObject <GitReadmeRoot>(json);
                            readmeAddress = results.HtmlLink;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                OnExceptionEvent?.Invoke(ex);
            }

            return(readmeAddress);
        }
        public static string CompanyNamesDelimited()
        {
            string results = "";

            using (var cn = new SqlConnection {
                ConnectionString = _connectionString
            })
            {
                using (var cmd = new SqlCommand {
                    Connection = cn
                })
                {
                    cmd.CommandText =
                        "SELECT STRING_AGG(ISNULL(CompanyName, ''), ';') AS csv " +
                        "FROM CustomerDatabase.dbo.Customer " +
                        "WHERE dbo.Customer.CompanyName LIKE @LikeCondition;";

                    cmd.Parameters.AddWithValue("@LikeCondition", "c%");

                    try
                    {
                        cn.Open();

                        results = Convert.ToString(cmd.ExecuteScalar());
                    }
                    catch (Exception ex)
                    {
                        OnExceptionEvent?.Invoke(ex);
                    }
                }
            }

            return(results);
        }
Exemple #3
0
        public static List <Product> ExampleProductsList(List <string> pCategoryList)
        {
            _methodName = nameof(ExampleProductsList);

            SqlWhereInParameterBuilder.OnShowCommandStatementEvent += SqlWhereInParameterBuilder_OnShowCommandStatementEvent;
            var products = new List <Product>();

            // field which the WHERE IN will use
            const string parameterPrefix = "C.CategoryName";

            // Base SELECT Statement
            var selectStatement =
                "SELECT  P.CategoryID, C.CategoryName, P.ProductID, P.ProductName, P.QuantityPerUnit " +
                "FROM Products AS P INNER JOIN Categories AS C ON P.CategoryID = C.CategoryID " +
                $"WHERE {parameterPrefix} IN ({{0}}) ORDER BY P.ProductName";

            var commandText = SqlWhereInParameterBuilder.BuildWhereInClause(selectStatement, parameterPrefix, pCategoryList);

            using (var cn = new SqlConnection {
                ConnectionString = _connectionString
            })
            {
                using (var cmd = new SqlCommand {
                    Connection = cn
                })
                {
                    cmd.CommandText = commandText;
                    cmd.AddParamsToCommand(parameterPrefix, pCategoryList);

                    try
                    {
                        cn.Open();

                        var reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            products.Add(new Product()
                            {
                                CategoryID      = reader.GetInt32(0),
                                CategoryName    = reader.GetString(1),
                                ProductID       = reader.GetInt32(2),
                                ProductName     = reader.GetString(3),
                                QuantityPerUnit = reader.GetString(4)
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        OnExceptionEvent?.Invoke(ex);
                    }
                    finally
                    {
                        SqlWhereInParameterBuilder.OnShowCommandStatementEvent -= SqlWhereInParameterBuilder_OnShowCommandStatementEvent;
                    }
                }
            }

            return(products);
        }
Exemple #4
0
        public static DataTable ExampleCustomersDataTable1(List <string> pContactTitleList)
        {
            _methodName = nameof(ExampleCustomersDataTable1);

            SqlWhereInParameterBuilder.OnShowCommandStatementEvent += SqlWhereInParameterBuilder_OnShowCommandStatementEvent;

            var customersDataTable = new DataTable();

            // field which the WHERE IN will use
            const string parameterPrefix = "CT.ContactTitle";

            // Base SELECT Statement
            var selectStatement =
                "SELECT C.CustomerIdentifier , C.CompanyName , C.ContactName , C.ContactTypeIdentifier , " +
                "FORMAT(C.ModifiedDate, 'MM-dd-yyyy', 'en-US') AS ModifiedDate, CT.ContactTitle " +
                "FROM dbo.Customers AS C INNER JOIN dbo.ContactType AS CT ON C.ContactTypeIdentifier = CT.ContactTypeIdentifier " +
                $"WHERE {parameterPrefix} IN ({{0}}) ORDER BY C.CompanyName";

            // Builds the SELECT statement minus values
            var commandText = SqlWhereInParameterBuilder.BuildWhereInClause(selectStatement, parameterPrefix, pContactTitleList);

            using (var cn = new SqlConnection {
                ConnectionString = _connectionString
            })
            {
                using (var cmd = new SqlCommand {
                    Connection = cn
                })
                {
                    cmd.CommandText = commandText;

                    //
                    // Add values for command parameters
                    //
                    cmd.AddParamsToCommand(parameterPrefix, pContactTitleList);


                    try
                    {
                        cn.Open();
                        customersDataTable.Load(cmd.ExecuteReader());

                        if (DisplayFormattedSql)
                        {
                            Console.WriteLine(Scripter.Format(cmd.CommandText));
                        }
                    }
                    catch (Exception ex)
                    {
                        OnExceptionEvent?.Invoke(ex);
                    }
                    {
                        SqlWhereInParameterBuilder.OnShowCommandStatementEvent -= SqlWhereInParameterBuilder_OnShowCommandStatementEvent;
                    }
                }
            }

            return(customersDataTable);
        }
Exemple #5
0
 /// <summary>
 /// 强制结束线程
 /// </summary>
 public void AbortThread(string threadId)
 {
     if (_initDone)
     {
         _threadPool.AbortThread(threadId, (error) =>
         {
             if (OnExceptionEvent != null)
             {
                 OnExceptionEvent.Invoke(error);
             }
         });
     }
 }
        protected override async Task <bool> OnError(Exception e, TMessage message)
        {
            try
            {
                var errorEvent = new OnExceptionEvent(message.TenantId, Guid.NewGuid());
                await this._dispatcher.SendMessage(errorEvent, CancellationToken.None);

                return(true);
            }
            catch (Exception ex)
            {
                await base.OnError(ex, message);

                return(true);
            }
        }
Exemple #7
0
        /// <summary>
        /// 运行线程
        /// </summary>
        /// <param name="threadAct">线程方法</param>
        /// <returns>线程唯一标识Id</returns>
        public string RunThread(Action threadAct)
        {
            string id = string.Empty;

            if (_initDone)
            {
                id = _threadPool.CreateThread(threadAct, (error) =>
                {
                    if (OnExceptionEvent != null)
                    {
                        OnExceptionEvent.Invoke(error);
                    }
                });
            }
            return(id);
        }
Exemple #8
0
        public static List <Customer> GeCustomers()
        {
            try
            {
                var fileName = "customers.json";
                var json     = File.ReadAllText(fileName);
                return(JsonSerializer.Deserialize <List <Customer> >(json).ToList());
            }
            catch (Exception ex)
            {
                OnExceptionEvent?.Invoke(new DeserializeException()
                {
                    MethodName = $"{GetCurrentMethod().DeclaringType}.{GetCurrentMethod().Name}",
                    Exception  = ex
                });

                return(null);
            }
        }
Exemple #9
0
        public static List <Customer> GeTopCustomers(int count)
        {
            try
            {
                var json      = File.ReadAllText(FileName);
                var customers = JsonSerializer.Deserialize <List <Customer> >(json).Take(count).ToList();
                return(customers);
            }
            catch (Exception ex)
            {
                OnExceptionEvent?.Invoke(new DeserializeException()
                {
                    MethodName = $"{GetCurrentMethod().DeclaringType}.{GetCurrentMethod().Name}",
                    Exception  = ex
                });

                return(null);
            }
        }
 /// <summary>
 /// Example that will run freely and the app will be unresponsive which
 /// is how many developers approach reading folders and only try with a smaller
 /// folder structure but larger structures will freeze the app thus we
 /// need to consider an asynchronous method.
 /// </summary>
 /// <param name="path">Folder to work with</param>
 /// <param name="indentLevel">Indent level for display with Console.WriteLine</param>
 public static void RecursiveFolders(string path, int indentLevel)
 {
     try
     {
         if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
         {
             foreach (var folder in Directory.GetDirectories(path))
             {
                 OnTraverseEvent?.Invoke(folder);
                 Console.WriteLine($"{new string(' ', indentLevel)}{Path.GetFileName(folder)}");
                 RecursiveFolders(folder, indentLevel + 2);
             }
         }
     }
     catch (UnauthorizedAccessException unauthorized)
     {
         OnExceptionEvent?.Invoke(unauthorized);
     }
 }
Exemple #11
0
 /// <summary>
 /// 初始化
 /// </summary>
 public void Initialize(int maxCount = 25)
 {
     if (!_initDone)
     {
         _threadPool = new JMThreadPool(maxCount);
         _initDone   = true;
         if (OnInitSuccessEvent != null)
         {
             OnInitSuccessEvent.Invoke();
         }
     }
     else
     {
         if (OnExceptionEvent != null)
         {
             string arg = "## JM Error ## cls:JMThreadManager func:Initialize info:Has initialized";
             OnExceptionEvent.Invoke(arg);
         }
     }
 }
        /// <summary>
        /// Recursively remove an entire folder structure and files with events for monitoring and basic
        /// exception handling.
        /// </summary>
        /// <param name="currentDirectoryInfo"></param>
        public static void RecursiveDelete(DirectoryInfo currentDirectoryInfo)
        {
            if (!currentDirectoryInfo.Exists)
            {
                OnDeleteEvent?.Invoke("Nothing to process");
                return;
            }

            OnDeleteEvent?.Invoke(currentDirectoryInfo.Name);

            foreach (var directoryInfo in currentDirectoryInfo.EnumerateDirectories())
            {
                try
                {
                    RecursiveDelete(directoryInfo);
                }
                catch (Exception ex)
                {
                    OnExceptionEvent?.Invoke(ex);
                }
            }

            currentDirectoryInfo.Delete(true);
        }
Exemple #13
0
        public static async Task RecursiveFolders(DirectoryInfo directoryInfo, string[] excludeFileExtensions, CancellationToken ct)
        {
            if (!directoryInfo.Exists)
            {
                OnTraverseEvent?.Invoke("Nothing to process");

                return;
            }

            if (!excludeFileExtensions.Any(directoryInfo.FullName.Contains))
            {
                await Task.Delay(1, ct);

                OnTraverseEvent?.Invoke(directoryInfo.FullName);
            }
            else
            {
                OnTraverseExcludeFolderEvent?.Invoke(directoryInfo.FullName);
            }

            DirectoryInfo folder = null;

            try
            {
                await Task.Run(async() =>
                {
                    foreach (DirectoryInfo dir in directoryInfo.EnumerateDirectories())
                    {
                        folder = dir;

                        if ((folder.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden ||
                            (folder.Attributes & FileAttributes.System) == FileAttributes.System ||
                            (folder.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
                        {
                            OnTraverseExcludeFolderEvent?.Invoke($"* {folder.FullName}");

                            continue;
                        }

                        if (!Cancelled)
                        {
                            await Task.Delay(1);
                            await RecursiveFolders(folder, excludeFileExtensions, ct);
                        }
                        else
                        {
                            return;
                        }

                        if (ct.IsCancellationRequested)
                        {
                            ct.ThrowIfCancellationRequested();
                        }
                    }
                }, ct);
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException)
                {
                    Cancelled = true;
                }
                else if (ex is UnauthorizedAccessException)
                {
                    UnauthorizedAccessExceptionEvent?.Invoke($"Access denied '{ex.Message}'");
                }
                else
                {
                    OnExceptionEvent?.Invoke(ex);
                }
            }
        }
Exemple #14
0
        public static List <Orders> ExampleOrderDates(List <string> pDatesList)
        {
            _methodName = nameof(ExampleOrderDates);

            SqlWhereInParameterBuilder.OnShowCommandStatementEvent += SqlWhereInParameterBuilder_OnShowCommandStatementEvent;

            var orders = new List <Orders>();

            const string parameterPrefix = "O.OrderDate";

            var selectStatement =
                "SELECT O.OrderID, O.CustomerIdentifier, O.OrderDate, O.RequiredDate, O.ShipCity, O.ShipPostalCode  " +
                "FROM Orders AS O  " +
                $"WHERE {parameterPrefix} IN ({{0}})";

            var commandText = SqlWhereInParameterBuilder.BuildWhereInClause(selectStatement, parameterPrefix, pDatesList);

            using (var cn = new SqlConnection {
                ConnectionString = _connectionString
            })
            {
                using (var cmd = new SqlCommand {
                    Connection = cn
                })
                {
                    cmd.CommandText = commandText;
                    cmd.AddParamsToCommand(parameterPrefix, pDatesList);


                    try
                    {
                        cn.Open();

                        var reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            orders.Add(new Orders()
                            {
                                OrderID = reader.GetInt32(0), OrderDate = reader.GetNullableDateTime("OrderDate")
                            });
                        }

                        if (DisplayFormattedSql)
                        {
                            OnShowFormattedStatementEvent?.Invoke(Scripter.Format(cmd.CommandText));
                        }
                    }
                    catch (Exception ex)
                    {
                        OnExceptionEvent?.Invoke(ex);
                    }
                    finally
                    {
                        SqlWhereInParameterBuilder.OnShowCommandStatementEvent -= SqlWhereInParameterBuilder_OnShowCommandStatementEvent;
                    }
                }
            }


            return(orders);
        }
Exemple #15
0
        public static List <Customer> ExampleCustomersList1(List <string> pContactTitleList)
        {
            _methodName = nameof(ExampleCustomersList1);

            SqlWhereInParameterBuilder.OnShowCommandStatementEvent += SqlWhereInParameterBuilder_OnShowCommandStatementEvent;

            var customers = new List <Customer>();

            // field which the WHERE IN will use
            const string parameterPrefix = "CT.ContactTitle";

            // Base SELECT Statement
            var selectStatement =
                "SELECT C.CustomerIdentifier , C.CompanyName , C.ContactName , C.ContactTypeIdentifier , " +
                "FORMAT(C.ModifiedDate, 'MM-dd-yyyy', 'en-US') AS ModifiedDate, CT.ContactTitle " +
                "FROM dbo.Customers AS C INNER JOIN dbo.ContactType AS CT ON C.ContactTypeIdentifier = CT.ContactTypeIdentifier " +
                $"WHERE {parameterPrefix} IN ({{0}}) ORDER BY C.CompanyName";

            var commandText = SqlWhereInParameterBuilder.BuildWhereInClause(selectStatement, parameterPrefix, pContactTitleList);

            using (var cn = new SqlConnection {
                ConnectionString = _connectionString
            })
            {
                using (var cmd = new SqlCommand {
                    Connection = cn
                })
                {
                    cmd.CommandText = commandText;
                    cmd.AddParamsToCommand(parameterPrefix, pContactTitleList);

                    try
                    {
                        cn.Open();

                        var reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            customers.Add(new Customer()
                            {
                                CustomerIdentifier    = reader.GetInt32(0),
                                CompanyName           = reader.GetString(1),
                                ContactName           = reader.GetString(2),
                                ContactTypeIdentifier = reader.GetInt32(3),
                                ModifiedDate          = reader.GetString(4),
                                ContactTitle          = reader.GetString(5)
                            });
                        }

                        if (DisplayFormattedSql)
                        {
                            Console.WriteLine(Scripter.Format(cmd.CommandText));
                        }
                    }
                    catch (Exception ex)
                    {
                        OnExceptionEvent?.Invoke(ex);
                    }
                    {
                        SqlWhereInParameterBuilder.OnShowCommandStatementEvent -= SqlWhereInParameterBuilder_OnShowCommandStatementEvent;
                    }
                }
            }

            return(customers);
        }
Exemple #16
0
 /// <summary>
 /// 触发爬虫失败的事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void OnException(object sender, OnExcetionEventArgs e)
 {
     OnExceptionEvent?.Invoke(sender, e);
 }
Exemple #17
0
 private void SetOnException(string Message)
 {
     OnExceptionEvent?.Invoke(Message);
 }
Exemple #18
0
        public static List <ResultContainer> Work(string zipFileName)
        {
            var containers = new List <ResultContainer>();
            var newItem    = false;

            if (!File.Exists(zipFileName))
            {
                OnExceptionEvent?.Invoke(new FileNotFoundException(zipFileName));
                return(null);
            }

            Dictionary <string, byte[]> dictionary = new Dictionary <string, byte[]>();

            try
            {
                dictionary = ZipHelpers.GetFiles(File.ReadAllBytes(zipFileName));
            }
            catch (Exception e)
            {
                OnExceptionEvent?.Invoke(e);
            }

            var currentContainer = new ResultContainer();

            foreach (var keyValuePair in dictionary)
            {
                var folderName = Path.GetDirectoryName(keyValuePair.Key);

                if (string.IsNullOrWhiteSpace(folderName))
                {
                    folderName = "(root)";
                }

                currentContainer = containers.FirstOrDefault((item) => item.FolderName == folderName);

                if (currentContainer == null)
                {
                    currentContainer = new ResultContainer {
                        FolderName = folderName
                    };
                    newItem = true;
                }
                else
                {
                    newItem = false;
                }


                if (!string.IsNullOrWhiteSpace(Path.GetFileName(keyValuePair.Key)))
                {
                    currentContainer.List.Add(new FileContainer()
                    {
                        Name          = Path.GetFileName(keyValuePair.Key),
                        Size          = keyValuePair.Value.Length,
                        SizeFormatted = ZipHelpers.SizeSuffix(keyValuePair.Value.Length)
                    });
                }

                if (newItem)
                {
                    containers.Add(currentContainer);
                }
            }

            return(containers);
        }