mldisibio
mldisibio
CC#
Created by ScriptKidding on 3/24/2024 in #help
✅ Error while Dockerizing an ASP.NET project which references another project.
Paths inside the docker file are relatvie to the docker file itself, not to the location you are running the command from
13 replies
CC#
Created by Stefanidze on 3/22/2024 in #help
Can i use async/await in class constructors?
The static factory method suggested by @Pobiega is the way to go. But there is an alternative. You can fire a Task from the constructor and then get the result using await later in a non-constructor method.
public interface IDatabase{Task<int> GetId(string name);}

public class Foo
{
readonly string _someData;
readonly Task<int> _myId;

public Foo(string someData, IDatabase db)
{
_someData = someData;
_myId = db.GetId(someData);
}

public async Task DoStuff()
{
try
{
// only retrieved once and maintained in Result property on subsequent calls (maybe use ValueTask instead)
int id = await _myId;
Console.WriteLine($"Hello {id}");
}
catch { /* handle async error! */ }
}
}
public interface IDatabase{Task<int> GetId(string name);}

public class Foo
{
readonly string _someData;
readonly Task<int> _myId;

public Foo(string someData, IDatabase db)
{
_someData = someData;
_myId = db.GetId(someData);
}

public async Task DoStuff()
{
try
{
// only retrieved once and maintained in Result property on subsequent calls (maybe use ValueTask instead)
int id = await _myId;
Console.WriteLine($"Hello {id}");
}
catch { /* handle async error! */ }
}
}
6 replies
CC#
Created by sideburns_01 on 3/21/2024 in #help
Passing Lambda as a method parameter: Some issues
I'm curious though. Is this going against an RDBMS table, or is it Linq-to-Entity, or is it against a document database? If ClassB is a nested property of ClassA, does your query presume a join clause?
9 replies
CC#
Created by sideburns_01 on 3/21/2024 in #help
Passing Lambda as a method parameter: Some issues
I'm familiar enough with what you are doing to re-iterate that Albahari should be a gold-standard reference. But I gave up on this approach about ten years ago and went with explicit SQL and some extensions that wrapped simple Ado.Net operations with a fluent api. I won't be able to help you much with the Visitor pattern and expression tree builder needed here. 😕
9 replies
CC#
Created by sideburns_01 on 3/21/2024 in #help
Passing Lambda as a method parameter: Some issues
Ah, so you are really building the IQueryable! I got frustrated in my attempts years ago, but have a look at Joe Albahari's approach here: https://www.albahari.com/nutshell/linqkit.aspx
9 replies
CC#
Created by sideburns_01 on 3/21/2024 in #help
Passing Lambda as a method parameter: Some issues
If I understand you correctly, you want to pass in a Function that knows how to query your nested property, but you probably need a generic parameter to specify the expected outcome:
void Main()
{
Bar b = new Bar("abc", new Foo("Joe", 37));
Console.WriteLine(b.QueryProperty(b => b.Person.Age));
Console.WriteLine(b.QueryBarProperty(b => b.Person.Name));
}

public record Foo(string Name, int Age);
public record Bar(string Tag, Foo Person);
public static class Ext
{
// specific to Bar
public static TOut QueryBarProperty<TOut>(this Bar src, Func<Bar, TOut> query) => query(src);
// any class
public static TOut QueryProperty<TIn, TOut>(this TIn src, Func<TIn, TOut> query) => query(src);
}
void Main()
{
Bar b = new Bar("abc", new Foo("Joe", 37));
Console.WriteLine(b.QueryProperty(b => b.Person.Age));
Console.WriteLine(b.QueryBarProperty(b => b.Person.Name));
}

public record Foo(string Name, int Age);
public record Bar(string Tag, Foo Person);
public static class Ext
{
// specific to Bar
public static TOut QueryBarProperty<TOut>(this Bar src, Func<Bar, TOut> query) => query(src);
// any class
public static TOut QueryProperty<TIn, TOut>(this TIn src, Func<TIn, TOut> query) => query(src);
}
9 replies
CC#
Created by Alex on 3/21/2024 in #help
API structure
Exactly. I'm not an expert in the mobile/push/web-socket architectures, but once you can name it, it should be easy to find discussions and examples.
15 replies
CC#
Created by Alex on 3/21/2024 in #help
API structure
This is starting to sound like mobile phone map apps, and for sure that architecture is quite established and easy to find examples of out there
15 replies
CC#
Created by Alex on 3/21/2024 in #help
API structure
That's really an infrastructure question. What is your actual client base and how robust is your server, is it in the cloud and can it scale, etc. In today's world, 100 requests is child's play, and if the data is already cached in Redis, there is no compute, just network traffic. If it were really an issue, you could push the client requests onto a queue and respond to them one at time, but I would load test first and see if that is really a problem. There are reverse proxies and load balancers out there to solve that problem, so look into those before reinventing that wheel. Also, if you are considering a push architecture like SignalR, then heck, the client doesn't even need to send a request...just push the updated data to each client.
15 replies
CC#
Created by Alex on 3/21/2024 in #help
API structure
Couple of thoughts: 1. the backend that transforms the data and stored it in redis can do the compute to determine if the data changed. If it has changed, you can create a very light pub/sub interaction between Redis and your client and push a notification to your client that the data for a particular station has changed. You could abstract this pattern and do it with Signal R or some other push framework. 2. If you are using http calls, look into ETag variations that can be communicated via headers in the http messages, both request and response. Essentially eTags communicate if data has changed and if you treat your station data like a file or binary blob, you could send only the parts that have changed 3. Your idea of a second api that just tells you if the station data has changed is pretty simple and would work as well
15 replies
CC#
Created by whatsinaname on 3/20/2024 in #help
docker-angular-dotnet app
If you run the api in a second container, as @TeBeCo suggests, you can use a docker-compose file to bring them both up and either create an explicit network or let docker-compose do its magic and your api address will be the same name as the service under which the api is defined in the yaml file.
29 replies
CC#
Created by whatsinaname on 3/20/2024 in #help
docker-angular-dotnet app
localhost has a different meaning for the containerized angular app. It will look for the api inside the same container. It has no idea you are referring to the host running the container. To achieve what you want, docker has a few special variables, such as host.docker.internal and gateway.docker.internal. For docker run you would use the --add-host=host.docker.internal:my-api switch and then point your angular app at my-api instead of localhost. Alternatively you could hard code your local pc IP address, but it probably is dynamic, and you probably would need to configure the DNS resolver for the container to use. For more info simply google those keywords.
29 replies
CC#
Created by Whiteboy on 3/19/2024 in #help
Read process RAM usage in real time from docker container using c#
The System.Diagnostics functions work well from inside docker containers (they were a little shaky back in the dotnet 2.0 days). I have this polling memory usage every two seconds if enabled by an environment variable and logging only when the memory usage increases. I've compared the output with docker stats and it mirrors the container memory usage output.
2 replies
CC#
Created by whgrs1692 on 11/25/2023 in #help
I am trying to debug a webapi project in a docker container, but breakpoints are not being hit
is the container running on the same machine you are debugging from? I ask because debugging a container is ridiculously complex to get working and debugging a remote container is exponentially worse. I got debugging to work on a container running in my WSL2 using VS Code and the remote container extensions. Had to use System.Diagnostics.Debugger.Break(); to get the first breakpoint, then I could use the IDE to add additional breakpoints on the fly.
2 replies
CC#
Created by Кён Кёныч on 11/30/2023 in #help
Method for printing collections.
I don't recommend complex approaches with funky dependencies, but in your case, if you are looking to display any variety of collection or object graphs, this is well-known feature of LinqPad Dump() that is so popular people ask to incorporate it into Visual Studio or c# projects. Since LinqPad comes with a standalone executable, it's an option, albeit somewhat convoluted: https://stackoverflow.com/a/73541726/458354
18 replies