C# RabbitMQ模擬Producer與Consumer

最近系統考量可靠性問題,在中間加上一層 Message Queue(MQ)的架構,採用的是目前最多人使用的RabbitMQ當作server服務。順便直接以百萬筆資料為單位塞入MQ來測試一下效能。

Producer程式

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
            sw.Reset();//碼表歸零
            sw.Start();//碼表開始計時
            //
            string queue = "info";
            try
            {
                var factory = new RabbitMQ.Client.ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "guest";
                factory.Password = "guest";

                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        bool durable = true;
                        channel.QueueDeclare(queue, durable, false, false, null);
                        RabbitMQ.Client.IBasicProperties properties = channel.CreateBasicProperties();
                        properties.DeliveryMode = 2;
                        for (int i = 1; i <= 100; i++)
                        {
                            LogInfo info = new LogInfo();
                            info.SYS_ID = "系統";
                            info.COMPANY_ID = "公司";
                            info.STORE_ID = "店別";
                            info.Content = "訊息";
                            string m1 = Newtonsoft.Json.JsonConvert.SerializeObject(info);
                            var body = Encoding.UTF8.GetBytes(m1);
                            channel.BasicPublish("", queue, properties, body);
                        }

                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            sw.Stop();
            string result1 = sw.Elapsed.TotalMilliseconds.ToString();
            Console.WriteLine("共花費:" + result1 + "毫秒");
            Console.ReadLine();

Consumer端程式

    string queue = "info";
            try
            {
                var factory = new ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "guest";
                factory.Password = "guest";
                var connection = factory.CreateConnection();
                var channel = connection.CreateModel();
                channel.QueueDeclare(queue, true, false, false, null); // 定義處理那一個queue
                channel.BasicQos(0, 1, false);  // 每次處理1則

                var consumer = new RabbitMQ.Client.Events.EventingBasicConsumer(channel); 
                // 定義收到queue的內容處理方式
                consumer.Received += (sender, e) =>
                {
                    byte[] body = e.Body.ToArray();
                    string message1 = Encoding.UTF8.GetString(body); 
                    LogInfo log = JsonConvert.DeserializeObject<LogInfo>(message1);  // 將queue中的json轉回物件
                    // 以下可以更改為自己要處理的事項
                    Console.WriteLine(log.DateTime.ToString("yyyy/MM/dd HH:mm:ss:FFF")+" "+log.Content );  // 先顯示畫面上
                    //
                    channel.BasicAck(e.DeliveryTag, false); // 處理完手動回應    

                };
                channel.BasicConsume(queue, false, consumer);  // 開始處理
                Console.ReadLine();
                connection.Close();
                channel.Close();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

自然人憑證作為公文系統、簽核系統簽章用憑證

最近要用.net c#去開發憑證簽證、加解密的功能,於是下午跑去申請了自然人憑證。然後瞭解一下卡片中的資料~繼續努力中,似乎自然人憑證與未來的新式晶片身份證都會當做很重要的憑證以及加解密的依據。

內政部憑證 網址: https://moica.nat.gov.tw/index.html

電子簽章法 https://law.moj.gov.tw/LawClass/LawAll.aspx?pcode=J0080037

跨平台網頁元件下載 網址: https://moica.nat.gov.tw/rac_plugin.html

HiPKI Local Server範例 網址: https://gpkiapi.nat.gov.tw/PKCS7Verify/

http://localhost:61161

HiPKI Local Server (version:1.3.4.103339) at 127.0.0.1:61161

http://127.0.0.1:61161/pkcs11info


http://127.0.0.1:61161/sign

http://127.0.0.1:61161/decrypt

.net 6 preview加上對http/3支援

http3協定是架構在以往被認為最不穩定的UDP基礎上,而為了消除UDP的不確定性,加上了QUIC協定。

透過QUIC來替代TCP對於可靠以及流量的控制,使得http3可以有效可靠的進行傳輸。

在.net 6的preview版本中,微軟也加上了對http/3的支援,也許它會是http協定的一估新的未來

C# ASP.net執行APPcmd/DNScmd實踐網站自動平台化

最近在測試,如果透過web申請帳號後,可不可以直接在DNS上一增子網域以及在IIS上增加站台的方式,直接申請後就有站台,在Windows 的網站主機上,似乎要透過appcmd與dnscmd達到。後來弄出了一個可以執行的方式,主要重點在於要有較高權限的帳密透過process執行cmd模式再去下指令。分享一下

/// <summary>

        /// 新增IIS站台

        /// </summary>

        /// <param name=”subdomain”>網站名稱</param>

        /// <param name=”domain”>網域</param>

        /// <param name=”webpath”>網頁位置</param>

        /// <returns></returns>

        public bool AddSite(string subdomain, string domain, string webpath)

        {

            using (Process p = new Process())

            {

                p.StartInfo.FileName = @”cmd.exe”;

                p.StartInfo.UseShellExecute = false;

                p.StartInfo.CreateNoWindow = true;

                p.StartInfo.RedirectStandardError = true;

                p.StartInfo.RedirectStandardInput = true;

                p.StartInfo.RedirectStandardOutput = true;

                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                p.StartInfo.UserName = “高權限帳號”;

                string pw = “高權限密碼”;

                SecureString ss = new SecureString();   

                foreach (char c in pw)

                {

                    ss.AppendChar(c);

                }

                p.StartInfo.Password = ss;

                //

                p.StartInfo.Arguments = “/c appcmd add site /name:”+subdomain+” /bindings:\”http/*:80:”+subdomain+”.”+domain+”,https/*:443:”+subdomain+”.”+domain+”\” /physicalPath:\””+webpath+”\””;

                p.Start();

                p.WaitForExit();

                StreamReader sr = p.StandardOutput;

                p.Close();

                string message = sr.ReadToEnd().Replace(“\n”, “<br />”);

                if (message.Contains(“ERROR”))

                {

                    return false;

                }

                else

                {

                    return true;

                }

            }

        }

        /// <summary>

        /// 透過指令新增子網域(如: levin.ksi.com.tw)

        /// </summary>

        /// <param name=”subdomain”>子網域名稱</param>

        /// <param name=”domain”>主網域</param>

        /// <param name=”ip”>主機IP</param>

        /// <returns></returns>

        public bool AddDNS(string subdomain,string domain,string ip)

        {

            using (Process p = new Process())

            {

                p.StartInfo.FileName = @”cmd.exe”;

                p.StartInfo.UseShellExecute = false;

                p.StartInfo.CreateNoWindow = true;

                p.StartInfo.RedirectStandardError = true;

                p.StartInfo.RedirectStandardInput = true;

                p.StartInfo.RedirectStandardOutput = true;

                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                p.StartInfo.UserName = “高權限帳號”;

                string pw = “高權限密碼”;

                SecureString ss = new SecureString();

                foreach (char c in pw)

                {

                    ss.AppendChar(c);

                }

                p.StartInfo.Password = ss;

                p.StartInfo.Arguments = @”/c dnscmd /recordadd ” + domain + ” ” + subdomain + ” A ” + ip;

                p.Start();

                p.WaitForExit();

                StreamReader sr = p.StandardOutput;

                p.Close();

                string message = sr.ReadToEnd().Replace(“\n”, “<br />”);

                if (message.Contains(“成功”)){

                    return true;

                }

                else

                {

                    return false;

                }

            }

        }

C# ASP.net Core開發line bot使用ngrok出現307 Temporary Redirect

使用asp.net core 開發LINE BOT時,使用ngrok做本機的服務器時,驗證Webhook出現錯誤。ngrok出現 307 Temporary Redirect的錯誤!

解決方式:

Starup.cs中有一句app.UseHttpsRedirection(); 前面加上 // 註解掉就可以了

參考:

ngrok http [port] -host-header="localhost:[port]"

C# AES加解密

最近常遇到需要使用AES加解密的需求,無論是網路傳輸/API相關的部份。
分享一下
Github原始碼下載
https://github.com/superlevin/CSharpAES

using System.Security.Cryptography;
public static string SHAEncrypt(string str)
{
var crypt = new System.Security.Cryptography.SHA256Managed();
var hash = new System.Text.StringBuilder();
byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(str));
foreach (byte theByte in crypto)
{
hash.Append(theByte.ToString("x2"));
// x小寫 X 大寫 x2 補0
}
return hash.ToString();
}
public static string AESEncrypt(string str, string strkey, string strivKey, bool isVasEncrypt = false)
{

var aesCipher = new AesManaged
{
KeySize = 128,
BlockSize = 128,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};

var keyStr = KeyGenerator(strkey);
var key = Convert.FromBase64String(keyStr);
var ivKey = Encoding.UTF8.GetBytes(strivKey);
var input = str;
var ivStr = Convert.ToBase64String(ivKey);

aesCipher.Key = key;
aesCipher.IV = ivKey;
byte[] b = System.Text.Encoding.UTF8.GetBytes(str); // plain text to be encrypted
ICryptoTransform encryptTransform = aesCipher.CreateEncryptor();
byte[] cipherText = encryptTransform.TransformFinalBlock(b, 0, b.Length);
return Convert.ToBase64String(cipherText);
}
public static string AESDecrypt(string encstring, string strkey, string strivKey)
{
var aesCipher = new AesManaged
{
KeySize = 128,
BlockSize = 128,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
var keyStr = KeyGenerator(strkey);
var key = Convert.FromBase64String(keyStr);
aesCipher.Key = key;
aesCipher.IV = Encoding.UTF8.GetBytes(strivKey);

var encryptBytes = Convert.FromBase64String(encstring);
ICryptoTransform decryptTransform = aesCipher.CreateDecryptor();
byte[] plainText = decryptTransform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length);

return System.Text.Encoding.UTF8.GetString(plainText);
}

FastReport新版測試

[C#]
DataSet FDataSet;
// 建立資料集
FDataSet = new DataSet();
DataTable table = new DataTable();
table.TableName = “Employees”;
FDataSet.Tables.Add(table);
table.Columns.Add(“ID”, typeof(int));
table.Columns.Add(“Name”, typeof(string));
table.Rows.Add(1, “Andrew Fuller”);
table.Rows.Add(2, “Nancy Davolio”);
table.Rows.Add(3, “Margaret Peacock”);
// 建立報表
Report report = new Report();
// 載入報表
report.Load(@”..\..\report.frx”);

// 註冊資料集
report.RegisterData(FDataSet);
report.GetDataSource(“Employees”).Enabled = true;
// 1 進入設計模式
 report.Design();
// 2 顯示報表
report.Show();
// 3 匯出成pdf
report.Prepare();
PDFExport export = new PDFExport();
report.Export(export, “result.pdf”);

report.Dispose();

[/c#]