Skip to content

Instantly share code, notes, and snippets.

View JsAndDotNet's full-sized avatar

Justin JsAndDotNet

View GitHub Profile
@JsAndDotNet
JsAndDotNet / Files.md
Created June 28, 2023 10:15
.NET Core - Files - Get Exec Assembly Location

.NET Core - Files - Get Exec Assembly Location

private string GetBasePath()
{
    var path = System.Diagnostics.Process.GetCurrentProcess()!.MainModule!.FileName;
    FileInfo fi = new FileInfo(path);

    // If you'd set something in a 'resources' folder and made the content copy always.
 var basePath = fi.Directory + @"\resources\";
From solution level:
containerRegistryName = The name of the container registry you create in Azure (acr)
acrRepositoryName = The individual repository within acr (e.g. my-microservice-app)
# Note build is from solution level. You might just need './Dockerfile .'
docker build -t {acrRepositoryName}:latest -f ./VsProjectName/Dockerfile .
az login
az acr login --name {containerRegistryName}
docker tag {acrRepositoryName}:latest {containerRegistryName}.azurecr.io/{acrRepositoryName}:latest;
docker push {containerRegistryName}.azurecr.io/{acrRepositoryName}:latest
@JsAndDotNet
JsAndDotNet / GetTaskResult-Program.cs
Last active April 3, 2023 11:18
Run a List of Task And Get Results
// See https://aka.ms/new-console-template for more information
using DoTaskAndGetResult;
Console.WriteLine("Hello, World!");
TestClass tc = new TestClass();
List<Task<TestData>> taskList = new List<Task<TestData>>();
for(int i = 0; i < 1000; i++)
@JsAndDotNet
JsAndDotNet / app-insights-microservices-program.cs
Created October 7, 2022 10:27
App Insights Set Cloud Role Name - Multiple Microservices off one App Insights (one workspace too)
// 0. Create Application insights (workspace version - You could set up a 'log analytics workspace' first)
// 1. In web app, Nuget -> Microsoft.ApplicationInsights.AspNetCore
// 2. Config (can either pass in app insights key, or use whole connection string in config)
// 3. (if using connectionstring, appsettings.json looks like)
//"ApplicationInsights": {
// "ConnectionString": "InstrumentationKey=...;IngestionEndpoint=https://...-0.in.applicati...ights.azure.com/;LiveEndpoint=https://........livediagnostics.monitor.azure.com/"
// },
@JsAndDotNet
JsAndDotNet / Akka-Net-Programs.cs
Created May 23, 2022 06:50
AKKA.NET - Very Basic Example with Tell and Ask
using Akka.Actor;
using AkkaHelloWorld;
Console.WriteLine("Enter a string to update the actor, or 'ask' to get its value");
var system = ActorSystem.Create("MySystem");
var greeter = system.ActorOf<SimpleMessageActor>("simplemsg");
greeter.Tell(new SimpleMessage("Starting"));
@JsAndDotNet
JsAndDotNet / api-with-problem.cs
Created May 3, 2022 12:49
Example API Problem Return
using Microsoft.AspNetCore.Mvc;
namespace TestForAPIM.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
@JsAndDotNet
JsAndDotNet / get-spid.ps1
Created April 8, 2022 09:31
Get Service Principle ObjectID from Azure
$webAppObjectId = (Get-AzADServicePrincipal -DisplayName $webAppName).Id
@JsAndDotNet
JsAndDotNet / CacheHelper.cs
Created March 29, 2022 08:49
Caching in .NET6+ Using IMemoryCache - Console Example
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Primitives;
using System;
namespace CacheTest
{
public class CacheHelper
{
private IMemoryCache _memoryCache { get; } = new MemoryCache(
@JsAndDotNet
JsAndDotNet / HttpClientApiCall.cs
Last active December 8, 2021 10:24
HttpClient API Call
public void SendData(string authToken, MyDataClass myData)
{
try
{
var url = $"https://www.something.com/api/something/post";
HttpClient client = new HttpClient();
//client.BaseAddress = new Uri(url);
// Add an Accept header for JSON format.
@JsAndDotNet
JsAndDotNet / delete-all-but-master.ps1
Created September 9, 2021 08:59
Delete All Branches Except Master Windows
#The command to delete all branches except master on linux is:
#git branch | grep -v "master" | xargs git branch -D
#To use the same command in Windows use powershell and CD to your repo
#Or, WINDOWS - we can use PowerShell command that do the same thing that above command do:
# Tried this and it worked nicely...
git branch | %{ $_.Trim() } | ?{ $_ -ne 'master' } | %{ git branch -D $_ }
#or WINDOWS -
#git branch -D @(git branch | select-string -NotMatch "master" | Foreach {$_.Line.Trim()})