7.4Using in File-Based Apps Applications
Starting with the .NET 10 SDK, .NET provides File-Based Apps: a single .cs source file can be built, run and published — no project file (.csproj) or solution required. Dependencies and settings are declared with #:-prefixed directives at the top of the file (the #:include, #:package, #:project, #:property and #:sdk directives are supported).
For the complete directive reference and more usage details of
File-Based Apps, see the official Microsoft documentation "File-based apps".
Since a File-Based App has no project file, you cannot use the dotnet add package command. Instead, reference the HttpAgent package with the #:package directive. HTTP remote requests have fully supported the File-Based Apps application type since version 1.53, so use that version or later (@* means the latest version, currently 2.1.5):
1. Write the app.cs file
#:package HttpAgent@*using HttpAgent;var result = await HttpRemoteClient.Service.GetAsStringAsync("https://furion.net/");Console.WriteLine(result);Note: omitting the version after
#:packagecurrently only works when using central package management (Directory.Packages.props), so@*is used here to reference the latest version; you can also pin an explicit version such as#:package HttpAgent@2.1.5.
2. Run the application
Run the following command in the terminal:
dotnet run app.csYou can also use the --file option (dotnet run --file app.cs) or the shorthand syntax (dotnet app.cs). To pass arguments to the application, place them after --: dotnet run app.cs -- arg1 arg2.
3. Enable the Profiler (optional)
To print the full request/response traffic, call HttpRemoteClient.Configure in the file to configure the default HttpClient:
#:package HttpAgent@*using HttpAgent;HttpRemoteClient.Configure(services =>{ services.AddHttpClient(string.Empty).AddProfilerDelegatingHandler();});var result = await HttpRemoteClient.Service.GetAsStringAsync("https://furion.net/");Console.WriteLine(result);