C#

C

C#

We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.

Join

Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?

Hello everyone, I'm working on implementing a factory pattern for payment services in a .NET application and I'm unsure about properly managing the lifecycle of services. Here's the relevant part of my code: ```public class ProviderServiceFactory { private readonly IServiceProvider _serviceProvider;...

Blazor Isolated CSS not working

I have a Blazor app in InteractiveAuto mode and isolated css stopped working. I am not sure when it stopped working or what I changed. The attributes of the affected html elements are generated correctly. The generated css file contains the following:
@import 'ChemStore.Web.Client.bundle.scp.css';
@import 'ChemStore.Web.Client.bundle.scp.css';
I don't believe that the isolated css file is relevant since I tried removing all css I added myself, but I will provide it anyway: ```css #blazor-error-ui {...

Dependency Injection question

I was learning about coupling/decoupling and one thing that was confusing to me was this: Basically, people tell you to leave the instances for the calling of the constructor method, and instead just use an Interface that implements that class when using it on your code. The reason people will say this is useful includes (while not limited to) "Making so that if the signature of that class changes, the code using that class won't be affected, since it's using an abstraction of it, an Interface. Making it easier to change the class without breaking the code"...

C# backend vs Java backend

Hello, I will enter a long bootcamp and I am very undecided about which one to go with, there are 2 options in front of me, Java and C#. I could not decide even after my research on the internet. Can you please answer objectively, which way should I go?

problem with docker installation for .Net

Can someone please help me with solving this, the code has no error but the docker won’t install in the project
No description

creating a nuget for a custom msbuild task

I'm trying to follow the custom msbuild task tutorial here: https://learn.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-code-generation?view=vs-2022 My problem is that when using dotnet pack the resulting nupkg file doesnt contain any binaries. All required binaries are present in the ./bin/Release/netstandard2.0/ folder, they are just not packed into the nupkg. When inspecting the nupkg, there is no lib folder inside, i can see my .props file in the build folder though. Any ideas why that may happen?...

✅ Exception handling..

```cs namespace Calculator.Model { internal class Division {...

.MESH FILE CONVERSION

Hello, how would I go about converting Roblox .mesh files into Autodesk FBX Files with the use of Assimp via C# code?!

✅ SQL code

does anyone know why this keeps happening? i have tried changing the name to multiple things but it still says it is invalid
No description

calculated value in Start() function seems to be 0 when trying to use it.

Hi, i'm an amateur trying to learn c#. I'm confronted with a problem I can't seem to solve: I'm trying to use the "objectLength" value in the Start() function of my SpawnManager. I calculate "objectSize" in a different script called objectMovement. Now I know that it calculates the value correctly, but when I try to use it in the Start() function it is 0. (My Debug.Log(objectLength) outputs 0.) It must be a stupid error but I can't solve it. Thanks for your kind help!...
No description

✅ Different output on same code.

return arithmetic.Calculate(Convert.ToDecimal(left, CultureInfo.InvariantCulture), token, Convert.ToDecimal(right, CultureInfo.InvariantCulture));
return arithmetic.Calculate(Convert.ToDecimal(left, CultureInfo.InvariantCulture), token, Convert.ToDecimal(right, CultureInfo.InvariantCulture));
...
No description

✅ System.FormatException

Me and another are writing a RPN calculator. I get this error message when trying to calculate 3 4.8 5.7 + * 2.5 * but when he runs it he does not get an exception on it. We have the exact same files and code. Why?...
No description

✅ vs code community don't start my code

does anyone know how to fix visual studio code community that won't open my project? When I click on the run button it tells me everything was successful but in reality nothing opens, i am doing a Windows forms app and i am new with c# and vs code community

Manipulate Json

Anyone know how to add things to a deserialized json?

How do y'all get the ideas for your side projects?

Title. Do you have a random blink in your mind? Do you find them online? Is it something that helps you without your day? hmm...

✅ Fedora 40, dotnet 8 sdk, can’t use debugging tools in vscode.

Hi, has anyone else had a similar issue with dotnet on Fedora 40 or in general?


Could not execute because the specified command or file was not found.
Possible reasons for this include:
• You misspelled a built-in dotnet command.
• You intended to execute a .NET program, but dotnet-/home/username/GodotEngine/Godot_v4.3-stable_mono_linux_x86_64/Godot_v4.3-stable_mono_linux.x86_64 does not exist.
• You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.”
Could not execute because the specified command or file was not found.
Possible reasons for this include:
• You misspelled a built-in dotnet command.
• You intended to execute a .NET program, but dotnet-/home/username/GodotEngine/Godot_v4.3-stable_mono_linux_x86_64/Godot_v4.3-stable_mono_linux.x86_64 does not exist.
• You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.”


Haven’t found much online about this problem. I was only able to find solutions related to dotnet-ef which didn’t resolve my issue. I can’t start debugging my C# code due to this error. Path to executable is correct and when I run dotnet —version command in terminal, it outputs sdk’s version which is 8.0.108.
This happens both in vscode and when just running through the terminal....

✅ Why is this try/catch block preventing lines after it from firing when an exception is caught?

I have this foreach block with a try/catch inside it. Can someone please help me understand why the logger line for initialization completing isn't executing when an exception occurs inside the foreach try/catch? I know it's bad form to control flow with exceptions. This is a call to a dependency service which unfortunately doesn't give me a good way to tell if the ConnectAsync method succeeds or fails, so I don't really have a choice. (Also I'd want to catch exceptions regardless.) ```c#...

✅ Is it possible to ask check for a keypress without pausing the whole console?

Kinda new to c# so i dont rlly know alot, but i started a project and i literally have no clue how to ask for a keypress w out pausing and waiting for one, ive seen examples on stack like
Task.Factory.StartNew(() => Console.ReadKey(true)).Wait(TimeSpan.FromSeconds(0.2)); but i cant assign that to var key to use as controls etc, if anyone wants to check through my code more i can send screenshtos or try open a git...

How to add an image tag to an Aspire's ProjectResource?

so I add my project like this: ```csharp var apiService = builder .AddProject<Projects.Messenger_ApiService>("apiService") .WithEnvironment("ASPNETCORE_URLS", "http://*:8080;https://*:8443");...

Unit Testing ASP.NET async api service

Okay. I am trying to make a unit test for my Customer Service. I tried starting with the Read operation which looks like this in my service: ```csharp public async Task<Customer> Read(int customerId) {...