V
V
Victor P.2022-04-07 10:04:30
C++ / C#
Victor P., 2022-04-07 10:04:30

How to give the Program class the public modifier in c# 9.0?

Hello everyone, Top-level instructions
appeared in Nome Sharp instead of such an entry

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Now it is written like this:
using System;

Console.WriteLine("Hello World!");

I need to put down the public access modifier in the new syntax, but I can't find it anywhere in the documentation and in Google. Help me please.

This is necessary because I do integration tests and define the launch point through the WebApplicationFactory
internal class FunctionalTestsApiApplication : WebApplicationFactory<Program>
{
    protected override IHost CreateHost(IHostBuilder builder)
    {
...


I can then use this factory inside the test methods like this:
[Fact]
    public async Task Login()
    {
        // Arrange
        await using var application = new FunctionalTestsApiApplication();
        var client = application.CreateClient();
...

But I need to pass the factory through the DI container, for example, through the constructor, I do it according to the instructions like this:
public class Auth: IClassFixture<WebApplicationFactory<Program>>
{
    private readonly WebApplicationFactory<Program> _factory;
    private readonly IUserArrangeService _userService;
    
    public Auth(WebApplicationFactory<Program> factory, IUserArrangeService userService)
    {
        _factory = factory;
        _userService = userService;
    }

But in the constructor, the word Program is underlined, of course, because this class has less accesibility than the constructor. And the constructor must be public for DI to work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor P., 2022-04-07
@Jeer

They suggested that I rewrote the Program class in the old format:

namespace Api
{
    public class Program
    {
        static void Main(string[] args)
        {

And it started, although it was somehow not obvious to me that this could be done.
Thanks to all!
UPD They
also suggested that in the new syntax you can write at the very bottom through partial and it will also work:
using...
... 
app.Run();

public partial class Program { /* Expose the Program class for use with WebApplicationFactory<T> */ }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question