revolt
revolt
CC#
Created by revolt on 9/30/2024 in #help
Partial classes and platform specific code
Hello. Trying to create platform specific code and only use/compile the code for correct platform upon release. My current issue is that regardless of platform my linux specific code is trying to run on windows. I seen examples of it using MAUI, however i'm just trying to make a simple console app. In my csproj:
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<DefineConstants>WINDOWS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<DefineConstants>LINUX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<DefineConstants>WINDOWS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<DefineConstants>LINUX</DefineConstants>
</PropertyGroup>
Base class:
public partial class Platform
{
public partial void SetWallpaper(string path);
}
public partial class Platform
{
public partial void SetWallpaper(string path);
}
used like this:
#if WINDOWS
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;

public partial class Platform
{
public partial void SetWallpaper(string path)
{
//implementation
}
}
#endif
#if WINDOWS
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;

public partial class Platform
{
public partial void SetWallpaper(string path)
{
//implementation
}
}
#endif
an identical implementation is done for linux and all code is in the same namespace and it's built using this command: dotnet publish wallpaper.csproj -r win-x64 -c Release
55 replies
CC#
Created by revolt on 12/21/2023 in #help
CancelKeyPress not being handled/interrupted correctly
The issue is that on ctrl+c press the application does call CancelKeyPress event, however I'm assuming it is not respecting e.Cancel = true correctly. Either that or the main program passes ctrl+c to the spawned process and does not respect e.Cancel = true. Code : Program.cs
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build();

builder.Services.Configure<SettingsConfig>(configuration.GetSection("Settings"));
builder.Services.AddSingleton<ServerHandler>();
builder.Services.AddSingleton<IHostLifetime, CustomLifetime>();
using IHost host = builder.Build();
ServerHandler _serverHandler = host.Services.GetRequiredService<ServerHandler>();
_serverHandler.Create();
host.Run();
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build();

builder.Services.Configure<SettingsConfig>(configuration.GetSection("Settings"));
builder.Services.AddSingleton<ServerHandler>();
builder.Services.AddSingleton<IHostLifetime, CustomLifetime>();
using IHost host = builder.Build();
ServerHandler _serverHandler = host.Services.GetRequiredService<ServerHandler>();
_serverHandler.Create();
host.Run();
ServerHandler.cs (Relevant code. _serverProcess is a reference to new spawned Process, not the current main process)
public void CtrlCPressed(object? sender, ConsoleCancelEventArgs e)
{
if (!shouldExitNext)
{
e.Cancel = true;
shouldExitNext = true;
Console.WriteLine("Ctrl+C pressed. Running a method...");
StopServerProcess();

}
else
{
Console.WriteLine("Ctrl+C pressed again. Exiting...");
e.Cancel = false;

}
}

public void StopServerProcess()
{
SendServerMessage("stop");
_serverProcess.WaitForExit();
}
public void CtrlCPressed(object? sender, ConsoleCancelEventArgs e)
{
if (!shouldExitNext)
{
e.Cancel = true;
shouldExitNext = true;
Console.WriteLine("Ctrl+C pressed. Running a method...");
StopServerProcess();

}
else
{
Console.WriteLine("Ctrl+C pressed again. Exiting...");
e.Cancel = false;

}
}

public void StopServerProcess()
{
SendServerMessage("stop");
_serverProcess.WaitForExit();
}
CustomLifetime.cs
public Task WaitForStartAsync(CancellationToken cancellationToken)
{
Console.CancelKeyPress += _serverHandler.CtrlCPressed;
return Task.CompletedTask;
}
public Task WaitForStartAsync(CancellationToken cancellationToken)
{
Console.CancelKeyPress += _serverHandler.CtrlCPressed;
return Task.CompletedTask;
}
2 replies
CC#
Created by revolt on 1/30/2023 in #help
❔ Properly calling "await login()" in web service constructor
Hello. Kinda noob, but I am looking for the best way to call a async login function for an external library in a web service constructor. I posted my code below. I want to use this service in DI singleton and make sure the _client.ConnectAsync() is called once correctly. AFAIK this approach would require me to call EnsureInitializedAsync(); everytime I use this service in my api controller. I just want to know if this is the best approach, or if there is something easier to use. Thank you!
namespace web.Services
{
public class SSService
{
private readonly Lazy<Task> _initialization;
private Client _client;

public SSService()
{
_initialization = new Lazy<Task>(InitializeAsync);
}

private async Task InitializeAsync()
{
_client = new Client();
await _client.ConnectAsync("test", "test");
}

public Task EnsureInitializedAsync() => _initialization.Value;
}
}
namespace web.Services
{
public class SSService
{
private readonly Lazy<Task> _initialization;
private Client _client;

public SSService()
{
_initialization = new Lazy<Task>(InitializeAsync);
}

private async Task InitializeAsync()
{
_client = new Client();
await _client.ConnectAsync("test", "test");
}

public Task EnsureInitializedAsync() => _initialization.Value;
}
}
16 replies