júlio
júlio
CC#
Created by júlio on 5/31/2023 in #help
❔ IHealthCheck and Swagger
19 replies
CC#
Created by júlio on 5/31/2023 in #help
❔ CookieContainer and IHttpClientFactory
Hello, I'm currently migrating some old code base (.Net Core 3.0), and the code itself has a bunch of code smells, so I'm trying to improve it a bit (a bunch). It was a big Web Service and I'm also splitting it into smaller Web Services. I'm looking to take advantage of the IHtppClientFactory. I understand the concept, it reuses a HttpClient object for the connections, but I have a issue. The old code did something like this in one of the Controllers:
public class myClass
{
private HttpClient _client;
private CookieContainer _cookieContainer;
private HttpClientHandler _httpClientHandler;

public myClass()
{
_cookieContainer = new CookieContainer();
_httpClientHandler= new HttpClientHandler
{
AllowAutoRedirect = false,
CookieContainer = _cookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
};

_client= new HttpClient(_httpClientHandler);
_client.Timeout = TimeSpan.FromSeconds(30);
ConfigureDefaultHeaders();
}

private void ConfigureDefaultHeaders()
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36");
}
// ...
}
public class myClass
{
private HttpClient _client;
private CookieContainer _cookieContainer;
private HttpClientHandler _httpClientHandler;

public myClass()
{
_cookieContainer = new CookieContainer();
_httpClientHandler= new HttpClientHandler
{
AllowAutoRedirect = false,
CookieContainer = _cookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
};

_client= new HttpClient(_httpClientHandler);
_client.Timeout = TimeSpan.FromSeconds(30);
ConfigureDefaultHeaders();
}

private void ConfigureDefaultHeaders()
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36");
}
// ...
}
75 replies
CC#
Created by júlio on 4/27/2023 in #help
❔ C# Web Service - endpoint request redirection
Hello, I have multiple Web Services running. There is a service that is going to be discontinued but we need to keep it since older versions depend on the endpoints of that service. I want now to change the code in the deprecated service to redirect the request to the services where the code from this deprecated service was moved. I have 2 questons: Is there a better way to keep the old endpoints? For example is there a way for 2 endpoints to refer to the same thing? Because this would solve all the issues I'm having, and if not, how can I pass the request onto another service?
9 replies
CC#
Created by júlio on 4/19/2023 in #help
❔ Docker and Visual Studio - ASP.NET Core projects
Hello. I've recently been assigned the task to split a very big Web Service into smaller services, so that I can containerize them and orchestrate them with K8s. I've successfully split them and now what I have is a solution, and each project in that solution Is a Web Service that does a specific task. I've been experimenting with Docker and Visual Studio a bit, read some documentation, and gathered some questions that hopefully someone could help me understand better. So the first question is about how I should organize the projects. My main concert is that, if there is going to be any problem with having a single repository in Git that contains the solution and all the projects inside, of if I should split them by solutions and have one repository for each. I understand that when I have the whole thing running, if I want to change the code of a service I only need a Docker Image so that K8s can deploy newer versions, but maybe someone knows better and how I currently have things organized could be an issue. The second question is, I'm having trouble interacting with the services without them being run using Visual Studio. If I manually build a Docker Image (using the Dockerfile the Visual Studio generated), and then create and run a container using that image I created, yes it runs but I'm never able to interact with the service. When I run the docker ps or docker containers ls --all command, and put 2 containers running side by side, one that I created and the other created by Visual Studio in either Debug or Release mode, The ones VS creates in the PORT column get mapped to a port on my local machine, where the one I created only shows the ports the container has exposed (80 and 443 in my case). So this is something I don't yet understand why is happening or how can I talk with the container. Third question is regarding the containers Visual Studio creates.
11 replies
CC#
Created by júlio on 3/21/2023 in #help
❔ Understanding project structure - Visual Studio ASP.NET Core
4652 replies
CC#
Created by júlio on 3/20/2023 in #help
❔ Web Services - Beginner
Hello, I'm in the process of learning Web Services. I have successfully followed the tutorial (https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-7.0&tabs=visual-studio) and had it running, and everything works fine. I would now like to explore a bit more with Web Services. My first question is the following:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();
This is the default code that comes with the Program.cs file when I create a new ASP.NET Core Empty Project. If in my browser I do localhost.../ I will get Hello World!. My goal is to be able to pass and interpret parameters now. What changes to the code would I have to make, If I wanted to check if the parameter, for example name was passed, so that the code now does Hello <name>!?
373 replies
CC#
Created by júlio on 3/17/2023 in #help
❔ Windows BackgroundService
Hello. So I'm a beginner at C# and a beginner and making services, so I'm not sure how stupid some one the questions I'm about to ask might sound. Regardless I would really appreciate if someone could either explain me things or link to useful documentation. I have the following questions regarding Windows Services in C#: 1- From my understanding, the flow of the Service goes as follows (I'll be referring to BackgroundServices, since that is what the documentation advice me to use, apparently it's the modern approach to making a Service in Windows): In the Program.cs file, we have a builder, we give the builder information about the classes that are going to do tasks (?) and then we tell it to .Run(). So I assume this is the code that will run when in the shell I do sc.exe create <service_name.exe>. Please correct me If I'm wrong. Then I need to start the service so that it starts doing the thing I tell it to do, and I would stop it if I wanted to stop the service. To kill the service I would need to delete it. This would all be done using the sc.exe. 2- Still regarding Program.cs, what is a HostApplicationBuilder, and why do I need one? The documentation that I followed (https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service) used a bunch of Host stuff in the Program.cs file but didn't really explained what was going on, so this is my biggest question mark so far, is what is going on with the Program.cs file.
227 replies
CC#
Created by júlio on 3/14/2023 in #help
❔ C# methods
Why do I need to have a class or struct, so that I can have functions? In other words, why do I need to put functions inside classes or structures?
41 replies
CC#
Created by júlio on 3/14/2023 in #help
❔ Delegates
56 replies