Robert
Robert
CC#
Created by Robert on 1/16/2025 in #help
Task.Run() in .net framework
Hi, I'm maintaining an older legacy .net Framework application in which I have a void start() method (that must be void, however now this is intertwined with async await)
public async Task ConnectToSomeService() { ... }

public void StartService()
{
// what's the right way to call this here
Task.Run(ConnectToSomeService);
Task.Run( async () => await ConnectToSomeService());
ConnectToSomeService(); // and disable the warning ;)
}
public async Task ConnectToSomeService() { ... }

public void StartService()
{
// what's the right way to call this here
Task.Run(ConnectToSomeService);
Task.Run( async () => await ConnectToSomeService());
ConnectToSomeService(); // and disable the warning ;)
}
as far as i understand async () => await is slower but i don't quite grasp the difference.
4 replies
CC#
Created by Robert on 12/9/2024 in #help
Adding middleware
Hi! I face an issue whilst using the azure serverless signalr with azure function as bindings. When Signalr is running using websockets the authorization header is null and i must use the query to retrieve the token. How can I register it in my function? this is my Program.cs
var builder = FunctionsApplication.CreateBuilder(args);

builder.Services.AddAuthentication().AddBearerToken();
builder.Services.AddAuthorization();
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
builder.Services.AddServerlessHub<Functions>();
builder.Build().Run();
var builder = FunctionsApplication.CreateBuilder(args);

builder.Services.AddAuthentication().AddBearerToken();
builder.Services.AddAuthorization();
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
builder.Services.AddServerlessHub<Functions>();
builder.Build().Run();
essentially i'd somehow need to register this, but make it work for the function
public class QueryStringAuthenticationMiddleware
{
private readonly RequestDelegate _next;

public QueryStringAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Headers["Authorization"].Count == 0)
{
var token = context.Request.Query["auth_token"];
if (!string.IsNullOrEmpty(token))
{
context.Request.Headers["Authorization"] = $"Bearer {token}";
}
}

await _next(context);
}
}
public class QueryStringAuthenticationMiddleware
{
private readonly RequestDelegate _next;

public QueryStringAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Headers["Authorization"].Count == 0)
{
var token = context.Request.Query["auth_token"];
if (!string.IsNullOrEmpty(token))
{
context.Request.Headers["Authorization"] = $"Bearer {token}";
}
}

await _next(context);
}
}
2 replies
CC#
Created by Robert on 11/20/2024 in #help
Signalr
Hi! For work I have to connect the middleware with the backend in a fast way so I ended up deciding to use signalr. I have a serverless signalr instance running in azure and an azure function for the bindings I am start with a small poc to fully understand how it's working The azure function is uploaded as a file as it is too big
5 replies
CC#
Created by Robert on 8/8/2024 in #help
Skill issue serializing tasks
Hi! I need to serialize a list of tasks to only begin when the previous task is fully completed, let's take this snippet for example
async Task TestAsync(string log)
{
Console.WriteLine($"delay start {log}");
await Task.Delay(5000);
Console.WriteLine($"delay end {log}");
}

[TestMethod]
public async Task TestTasks()
{
List<Task> tasks = new()
{
TestAsync("1"),
TestAsync("2"),
TestAsync("3"),
};

/*
How do I achieve this?
delay start 1
delay end 1
delay start 2
delay end 2
delay start 3
delay end 3
*/
}
async Task TestAsync(string log)
{
Console.WriteLine($"delay start {log}");
await Task.Delay(5000);
Console.WriteLine($"delay end {log}");
}

[TestMethod]
public async Task TestTasks()
{
List<Task> tasks = new()
{
TestAsync("1"),
TestAsync("2"),
TestAsync("3"),
};

/*
How do I achieve this?
delay start 1
delay end 1
delay start 2
delay end 2
delay start 3
delay end 3
*/
}
I've tried using Task.WaitAll, await Task.WhenAll, looping and awaiting them, continue with (with and without await), but the closest i've been to succes is delay start 1 delay start 2 delay start 3 delay end 3 delay end 2 delay end 1 Thank you!
46 replies