ShowProgramCode

2022年12月15日 星期四

C# .net Core6 主控台使用強行別讀取 AppSetting.json

在主控台讀取設定檔有兩種格式:
  1.  .Xml
  2.  .Json
現在我要記錄的是第二種.json的格式該如何在主控台的專案使用強型別讀取。
首先,在專案的最上層新增一個appsetting.json檔案
接著,新增一個物件檔,處理設定檔的讀取。
internal class ConfigHelp
{
    private readonly IConfigurationRoot config;
    public ConfigHelp()
    {
    	config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsetting.json", true)
        .Build();
    }
    
    public void Get<T>(string configName, ref T resp)
    {
    	config.GetSection(configName).Bind(resp);
    }
    
    public string? Get(string configName)
    {
    	return config[configName];
    }
}
appsetting.json檔案
{
  "Setting": {
    "MinThreads": 2,
    "MaxThreads": 3,
    "TestTimes": 300
  }
}
設定檔的DTO
public class Setting
{
    public int MinThreads { get; set; }
    public int MaxThreads { get; set; }
    public int TestTimes { get; set; }
}
實際主控台程式讀取。
internal class Program
{
    private static Setting setting = new Setting();
    static void Main(string[] args)
    {
    	ConfigHelp config = new ConfigHelp();
        config.Get("Setting", ref setting);
    }
}

2022年9月15日 星期四

c# 無須遞迴取得資料夾下包含子目錄的所有檔案

之前我知道取得特定資料夾所有檔案的方式:

//*.*或*代表所有檔案,如果有特定檔名或檔案格式可在此設定
//ex:*.txt
List< string> files = System.IO.Directory.GetFiles(dirPath, "*.*").ToList();

但之後我需要將整個資料夾內所有檔案加密,正在尋找如何取得子目錄的檔案,甚至在考慮如何寫遞迴函式,才找到原來GetFiles就可以取得。

//加入System.IO.SearchOption.AllDirectories即可取得資料夾包含子目錄的檔案
List< string> files = System.IO.Directory.GetFiles(dirPath, "*.*", System.IO.SearchOption.AllDirectories).ToList();

特別記錄下來,以免日後需要時忘記。

2022年9月7日 星期三

c# 讀取中文檔案亂碼

簡言之,使用File.ReadAllLines開啟內容有中文的檔案,內容產生亂碼的處理方式。

原始程式碼:

List< string> csvDiffs = File.ReadAllLines(path).ToList();

更新程式碼:

//Encoding.GetEncoding(950)因.net core簡化會產生錯誤,必須加入此行
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//將編碼轉為big5
List< string> csvDiffs = File.ReadAllLines(path, Encoding.GetEncoding(950)).ToList();

參考網站:

http://dog0416.blogspot.com/2019/11/c-read-text-file-with-encoding.html

2022年8月5日 星期五

C# 連結Golang的Grpc Server

Grpc Client專案要如何設定,請參考之前的文章
這裡主要說明的是如何使用C# .NetCore作為Client連結Golang的Grpc Server。

Golang Grpc Server proto調整

syntax = "proto3";  // 定義要使用的 protocol buffer 版本

option csharp_namespace = "GrpcTestApi";

package grpcServer;  // for name space
//option go_package = "./;grpcServer";  // generated code 的 full Go import path

message SumRequest {
  repeated int64 input = 1 [packed=true];
}

message SumResponse {
  int64 result = 1;
}

message RemainderRequest{
  int64 a = 1;
  int64 b = 2;
}

message RemainderResponse {
  int64 result = 1;
}

service grpcService {
  rpc Sum(SumRequest) returns (SumResponse) {};
  rpc Remainder(RemainderRequest) returns (RemainderResponse) {};
}

連線Grpc Client Help物件

public class GrpcServerConnectHelp
{
    private string serverUrl;
    private grpcService.grpcServiceClient client;

    public string ServerUrl { get { return serverUrl; } }

    public GrpcServerConnectHelp()
    {
        Init(System.Configuration.ConfigurationManager.AppSettings.Get("GrpcServerUrl"));
    }

    public void Init(string url = null)
    {
        if (string.IsNullOrEmpty(url))
            return;

        serverUrl = url;
        //.NetCore 3.*版本必須加入此行,否則會錯誤。
        AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
        var channel = GrpcChannel.ForAddress(serverUrl);
        client = new grpcService.grpcServiceClient(channel);
    }

    public string CommonApi(string method, string input)
    {
        string resp = "查無此函式";

        if (method.Equals("Sum"))
        {
            return Sum(input);
        }

        if(method.Equals("Remainder"))
        {
            return Remainder(input);
        }

        return resp;
    }

    private string Sum(string input)
    {
        dynamic dyn = JsonConvert.DeserializeObject(input);
        SumRequest request = new SumRequest();

        foreach(var value in dyn.Input)
        {
            long temp = Convert.ToInt64(value);
            request.Input.Add(temp);
        }
        var reply = client.Sum(request);
        return reply.Result.ToString();
    }

    private string Remainder(string input)
    {
        dynamic dyn = JsonConvert.DeserializeObject(input);
        RemainderRequest request = new RemainderRequest();
        request.A = dyn.A;
        request.B = dyn.B;

        var reply = client.Remainder(request);
        return reply.Result.ToString();
    }
}

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

重點在於.Net Core 3.*的版本再傳入URL前,必須加入上面的語法,否則會產生錯誤

C# Grpc Server&Client範例

使用Grpc的預設專案,建立一個C#的GrpcServer+Client在同一個專案下。

 所以大致有以下三個動作:

  1. 建立範例Grpc Server專案
  2. 在同一個方案內建立Grpc Client專案
  3. 將共用的.proto檔案copy到Client中
  4. 測試專案

建立Grpc Server

  • 建立Grpc服務專案
  • 設定專案名稱
  • 選擇 .Net Core 3.1版本
  • Server的方案總管畫面
  • 更名Server專案名稱
  • 更改資料夾名稱
  • 編輯方案檔
  • 改寫Server專案路徑
GrpcDemo\Grpc.Server.csproj => Grpc.Server\Grpc.Server.cspro
  • 加入第三方套件參考
Server設定完成,執行沒有問題。

建立Grpc Client

  • 在同一方案中加入新專案(Client)
  • 專案類型選擇主控台
  • 設定專案名稱
  • 同樣選擇 .Net Core 3.1版本
  • 專案建置完成的方案總管截圖
  • 將Proto檔Copy到此
  • 改寫 .proto檔案內容
Server端:改寫option內容
GrpcDemo => Grpc.Server
Server端:加入參考
Client端:改寫option內容
GrpcDemo => Grpc.Client
  • 調整Grpc.Client.csproj
  • 重建方案
  • 修改Client程式碼
static async Task Main(string[] args)
{
await Task.Delay(3000);
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
Console.WriteLine("請輸入你的名字...");
string name = Console.ReadLine();
var reply = client.SayHello(new HelloRequest { Name = name });
Console.WriteLine("問候語 : " + reply.Message);
await channel.ShutdownAsync();
Console.WriteLine("按任何一個鍵退出...");
Console.ReadKey();
}
  • 從方案屬性調整起始專案
  • 開始測試
測試成功!!!

2022年7月8日 星期五

goLang grpc 安裝問題proto或protoc-gen-go不是可執行的程式

最近被要求寫一個grpc base的server範例,之後可能需要用到。

基本上範例所有動作都是參考下面網址:

https://pjchender.dev/golang/grpc-getting-started/


但是針對這個網址的說明,對我而言不明確的部分,加上紀錄。以防止下一次需要用到相關功能時,發生相同的問題。

一、 'protoc' 不是內部或外部命令、可執行的程式或批次檔。

首先剛開始時我沒有找到上面的網址,所以我先執行了go get -u google.golang.org/grpc、go get github.com/golang/protobuf/protoc-gen-go。執行go get github.com/golang/protobuf/protoc-gen-go時還發生另一個錯誤,go get github.com/golang/protobuf/protoc-gen-go已經不使用了,要求我改用google.golang.org/protobuf。

然後我照了另外一篇文章的說明,先寫proto檔案,在檔案路徑下執行protoc *.proto --go_out=plugins=grpc:. --go_opt=paths=source_relative,就出現了上面的錯誤: 'protoc' 不是內部或外部命令、可執行的程式或批次檔。

這個比較簡單,因為我沒有安裝protoc,另外最上方網址的文章提供的指令並非window使用的,我參考了下面網址,先解壓縮資料夾後,放入我慣用的路徑,最後在去環境變數中設定PATH,執行protoc --version就正常可以使用了。

https://hoohoo.top/blog/mac-linux-windows-install-protocol-buffer-protobuf-compiler-way/


 二、'protoc-gen-go' 不是內部或外部命令、可執行的程式或批次檔。

這個就比較麻煩了,當protoc安裝完畢,我再次在檔案路徑下執行protoc *.proto --go_out=plugins=grpc:. --go_opt=paths=source_relative,就出現了上面的錯誤:'protoc-gen-go' 不是內部或外部命令、可執行的程式或批次檔。

我花了不少時間查詢,包含改變環境參數中的PATH、GOPATH...,都沒有成功執行指令,最終我查到了下面的網址,將GOPATH下bin的檔案copy一份到GOROOT的bin底下就成功了(汗)@@

https://icode.best/i/35480546158182

但有個地方和上方網址的說明不同,protoc-gen-go.exe檔案有,protoc-gen-go-grpc.exe檔案是沒有的,可能因為我安裝的並非github.com/golang/protobuf/protoc-gen-go版本。

不過目前為止,我的測試範本的Server功能可正常執行,之後如果有問題將再繼續補充。

2022年6月24日 星期五

C# 靜態物件在不同執行緒產生的問題

 工作中遇到了一個問題,為了加快速度,我將某個基礎函式定義為靜態物件,這個靜態物件中會送POST到某個機器上,並在裡面夾帶Cookie作為認證機制。

例如:

Public static class Test
{
private string cookie;
public void Send(string url, string content )
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
WebHeaderCollection headerCollection = request.Headers;
headerCollection.Add("Cookie", cookie);
request.ContentType = "text/xml";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(content);
streamWriter.Flush();
streamWriter.Close();
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
resp.httpStatus = response.StatusCode;
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
resp.xmlRespone = sr.ReadToEnd();
}

if (!string.IsNullOrEmpty(response.GetResponseHeader("Set-Cookie")))
{
cookie = response.Headers.GetValues("Set-Cookie").ToList().FirstOrDefault();
}
}
response.Close();
}
}
}


TestController:
建構式:
private Test test = new Test();
其他呼叫函式TestSend():
test .Send(<<傳送機台網址>>,"sendContent");

一般情況是測不出問題的,可是某天客戶突然說出現了401回覆。

去看了Log才發現,當頻繁的呼叫這個函式(TestSend()),傳送機台那邊回覆401(沒有權限)。也就是雖然我有設定cookie值作為驗證,但是機器不認得這個驗證值,或者認定這個驗證值不屬於我,因此我沒有權限將資料送到機台處理。

為什麼呢?我百思不得其解,甚至還在考慮是否客戶的網路結構跟公司不同,在分流IP時產生了問題,造成去回不同路?

看了N個log,我才驚愕地發現一個事實。

我的驗證cookie在不同執行緒下,被蓋掉了!!!

從來不知道原來靜態物件是這樣設定的,在Controller中呼叫靜態物件後,它會占用同樣的儲存位置,因此不同執行緒執行時,靜態物件內部的設定值,即使是私有變數也可能會被蓋掉。

果然,當我取消靜態物件的設定,即使呼叫方式沒有改變,也不再出現驗證失敗401的錯誤了。

為防自己忘記,特別寫一下。並非設定私有變數就一定沒有問題,靜態物件的使用、呼叫必須謹慎。以上就是這次的教訓了。