Skip to content

Instantly share code, notes, and snippets.

View cheoalfredo's full-sized avatar
🔖
Learning Vue!!

El profe cheoalfredo

🔖
Learning Vue!!
View GitHub Profile
@cheoalfredo
cheoalfredo / Program.cs
Created March 13, 2024 22:27
Webapi example of SignalR usage
using Microsoft.AspNetCore.SignalR;
using WebsocketTest;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
var app = builder.Build();
app.UseHttpsRedirection();
@cheoalfredo
cheoalfredo / WSTestHub.cs
Last active March 13, 2024 22:20
SignalR Hub implementation
using Microsoft.AspNetCore.SignalR;
namespace WebsocketTest;
public class WSTestHub : Hub
{
public async Task DifundirMensaje(string message)
{
await Clients.All.SendAsync("MensajeRecibido", message);
}
}
@cheoalfredo
cheoalfredo / WeatherForecastController.cs
Created June 16, 2023 17:00
Acquire a lock with retries
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> Get()
{
await using var _lock = await _LockFactory.CreateLockAsync(
typeof(WeatherForecast).Name,
TimeSpan.FromSeconds(10), // need a lock for 10 seconds
TimeSpan.FromSeconds(3), // wait 3 seconds to attempt and acquire a lock
TimeSpan.FromSeconds(1) // retry every 1 second
);|
@cheoalfredo
cheoalfredo / Worker.cs
Created March 24, 2022 13:22
Extract propagated context from message headers
private IEnumerable<string> ExtractHeaders(IBasicProperties prop, string key)
{
try
{
if (prop.Headers.TryGetValue(key, out var value))
{
var bytes = value as byte[] ?? throw new Exception("no value");
return new[] { System.Text.Encoding.UTF8.GetString(bytes) };
}
}
@cheoalfredo
cheoalfredo / Worker.cs
Created March 24, 2022 13:19
Background process consuming messages and adding spans to trace
private static readonly ActivitySource ActivitySource = new ActivitySource("Worker");
private static readonly TextMapPropagator Propagator = new TraceContextPropagator();
const string ACTIVITY_NAME = "Launching handler to process request";
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
stoppingToken.ThrowIfCancellationRequested();
_model.QueueDeclare(_queueName, false, false, false, null);
var consumer = new EventingBasicConsumer(_model);
@cheoalfredo
cheoalfredo / Program.cs
Created March 24, 2022 13:04
Worker setup for OTel
var serviceName = "Worker";
services.AddOpenTelemetryTracing((builder) =>
{
builder.AddSource(serviceName)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))
.AddHttpClientInstrumentation()
.AddZipkinExporter(zipkinOptions =>
{
zipkinOptions.Endpoint = new Uri("http://localhost:9411/api/v2/spans");
@cheoalfredo
cheoalfredo / RabbitMessaging.cs
Last active January 19, 2022 20:27
Activity Creation and context propagation thru RabbitMQ headers in IBasicProperties
public async Task SendMessage(string Message, string Queue)
{
await Task.Run(() =>
{
using (var activity = _activitySource.StartActivity("Queue user creation request", ActivityKind.Producer))
{
using (var channel = _conn.CreateModel())
{
channel.QueueDeclare(Queue, false, false, false, null);
ActivityContext contextToInject = default;
@cheoalfredo
cheoalfredo / Program.cs
Created January 19, 2022 01:59
TraceProvider to subscribe to activities of type ApiEntryPoint
var serviceName = "ApiEntryPoint";
builder.Services.AddOpenTelemetryTracing((builder) =>
{
builder.AddSource(serviceName)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))
.AddAspNetCoreInstrumentation()
.AddZipkinExporter(zipkinOptions =>
{
zipkinOptions.Endpoint = new Uri("http://localhost:9411/api/v2/spans");
})
@cheoalfredo
cheoalfredo / app-routing.module.ts
Created January 14, 2022 16:14
Adding userinfo to available routes
const routes: Routes = [
{ path: 'userinfo', component: UserInfoComponent}
];
@cheoalfredo
cheoalfredo / app.component.ts
Created January 14, 2022 03:23
Login, Logout and consume jwt protected http endpoint
public login() {
this.oauthSvc.initLoginFlow()
}
public logoff() {
this.oauthSvc.logOut();
}
hitBackend(): void {
this.client.get("http://localhost:5000/api/numero").subscribe({