002 VSCode - CarrieKroutil/Reactivities GitHub Wiki
-
Turn on ‘Auto Save’ under File main menu.
-
In *.csproj file:
- The TartgetFramework is specified,
net7.0
- Setting for
<ImplicitUsings>enable</ImplicitUsings>
- Which allows for global using statements to live ./{projectName}/obj/debug/net7.0/API.GlobalUsings.g.cs
- The TartgetFramework is specified,
-
Under main menu: Code → Settings → Settings → search for “exclude” and under Files: Exclude
- Click “Add Pattern” and save
**/bin
and**/obj
.
- Click “Add Pattern” and save
- Remember under main menu: Code → Settings → "Keyboard Shortcuts" or type [cmd + k + cmd + s].
Handy shortcuts to remember:
- [command + p] = type file or folder name to go to, used for quick navigation
- [control + `] = opens terminal
- [shift + opt + down] = copy line down
- [shift + command + p] = opens command pallet.
- Then type: “Shell command: Install 'code' command in PATH”
- Type
dotnet new list
to see available templates from the CLI. - Create new console app:
dotnet new console -o MyApp -f net7.0
- Switch to new directory:
cd MyApp
- Run code:
dotnet run
→ Displays “Hello, World!”
Using terminal and dotnet CLI:
- For Http use:
dotnet add package RestSharp
- To work with Amazon S3 buckets:
dotnet add package AWSSDK.S3
Using command pallet, add Nuget Gallery
- [shift + command + p] = opens command pallet.
- Then type: “... Nuget...” to add and then have available as tab next to terminal. E.g.
- Then type: “... Nuget...” to add and then have available as tab next to terminal. E.g.
Using the RestSharp package added in above step. In Console App's program.cs file:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using RestSharp;
// TODO: Replace with more secure storage
string ApiKey = "api_key_here";
string BaseUrl = "https://api.some-domain.com";
async Task<List<string>> GetSupportedLanguages()
{
var client = new RestClient(BaseUrl);
var request = new RestRequest("/v2/video_translate/target_languages", Method.Get);
request.AddHeader("accept", "application/json");
request.AddHeader("x-api-key", ApiKey);
var response = await client.ExecuteAsync(request);
if (response != null && response.IsSuccessful)
{
var json = JsonSerializer.Deserialize<JsonElement>(response.Content);
List<string> languages = new List<string>();
foreach (var lang in json.GetProperty("data").GetProperty("languages").EnumerateArray())
{
languages.Add(lang.GetString());
}
return languages;
}
else
{
WriteLog("Failed to fetch supported languages from API.");
return new List<string>();
}
}
Add a Debug Configuration
- Open the Run and Debug panel (
Cmd + Shift + D
). - Click "create a launch.json file" if you don't have one.
- Select .NET (Core).
- A
launch.json
file will be created under.vscode/launch.json
.
Configure launch.json
- update to ensure it works for your console app:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Console App",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net9.0/YourAppName.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"stopAtEntry": false
}
]
}
Note: Change YourAppName.dll to match your app's name inside bin/Debug/net6.0/.
Before debugging, build your project: dotnet build
Then, press F5 (or click "Run and Debug" in VS Code) to start debugging.
Set Breakpoints
- Open any
.cs
file in VS Code. - Click in the left margin next to a line number to set a breakpoint.
- Start debugging (
F5
). - Execution will pause at breakpoints, allowing you to inspect variables.
To set environment variables for debugging:
- Open
launch.json
. - Add an
"env"
section:
"env":
{
"API_KEY": "your_api_key_here",
"S3_BUCKET_BASE_PATH": "your_s3_bucket_path/",
"S3_BUCKET_NAME": "your_s3_bucket_name",
"S3_REGION": "your_aws_region"
}
string LogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "process.log");
void WriteLog(string message)
{
string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}\n";
File.AppendAllText(LogFilePath, logMessage);
}
The 'AppendAllText' method in C# is...
... part of the System.IO namespace and is used to append text to a file. If the specified file does not exist, this method creates the file and then writes the specified text to it. If the file already exists, the method opens the file, appends the specified text to the end of the file, and then closes the file.
This method is particularly useful when you need to add new content to an existing file without overwriting the current content. It simplifies the process of opening a file, seeking to the end, writing the new content, and then closing the file, all in a single method call.
Since your application writes logs to process.log, you can tail the log file while debugging: tail -f process.log
This will display log updates in real-time.
Per here: https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/dotnetv3/README.md#Prerequisites -
We rely on dotnet-format to keep this code consistently formatted and styled. To contribute .NET code to this project, please refer to the following installation and usage steps.
Using dotnet-format
We run dotnet-format using a custom configuration file against any changed file or directory. See the .NET Github Action workflow for details.
To invoke dotnet-format yourself, first install it with: dotnet tool install -g dotnet-format
.
Next, run the dotnet-format command in the directory of your solution or project: dotnet format
.