C# WIX: Registrieren meiner ASP.NET Core -App als Windows -Dienst mit Wix InstallerC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 C# WIX: Registrieren meiner ASP.NET Core -App als Windows -Dienst mit Wix Installer

Post by Anonymous »

Ich versuche, mein Wix -Installationsprogramm meine ASP.NET -Core -App als Windows.msc -Dienst während der Installation zu haben.
Dies ist Teil meines Programms.

Code: Select all










Aber während der Installation erhalte ich diesen Fehler:
< /p>
fragt mich natürlich während der Installation für Admin Prev. Und ich klicke auf "als Administrator ausführen", aber das scheint nicht zu helfen. CS: < /p>

Code: Select all

var builder = WebApplication.CreateBuilder(args);
int freePort = 0;

// Load default settings
builder.Configuration
.AddJsonFile("local.settings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();

builder.Host.UseWindowsService(options =>
{
options.ServiceName = "Nav.LocalService"; // MUST MATCH WiX ServiceInstall Name
});

// Determine user-specific settings file path in AppData
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var userSettingsFilePath = Path.Combine(appDataPath, "LocalService", "local.settings.json");

if (File.Exists(userSettingsFilePath))
{
// if we have a local file for each user, take this instead and leave the other one as backup
builder.Configuration.AddJsonFile(userSettingsFilePath, optional: true, reloadOnChange: true);
}
else
{
// copy local file if it doenst already exist

// Ensure the directory exists
var directoryPath = Path.GetDirectoryName(userSettingsFilePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
var sourceFilePath = Path.Combine(Directory.GetCurrentDirectory(), "local.settings.json");

var fileContent = File.ReadAllText(sourceFilePath); // Read the content of the existing file

File.WriteAllText(userSettingsFilePath, fileContent);

builder.Configuration.AddJsonFile(userSettingsFilePath, optional: true, reloadOnChange: true);

Thread.Sleep(100);

}

var getLastUsedPort = builder.Configuration["Values:Application:FunctionAppPort"];

builder.Services
.AddHttpClient()
.AddTransient()
.AddTransient()
.AddTransient()
.AddTransient()
.AddSingleton()
.AddTransient()
.AddSingleton()
.AddHostedService()
.AddHostedService()
.AddControllers();

var app = builder.Build();

if (String.IsNullOrEmpty(getLastUsedPort))
{
// no old port available get a new one
freePort = Converters.GetFreePort();
var updater = new ConfigUpdater();
updater.UpdateConfig("Application:FunctionAppPort", freePort.ToString());

}
else
{
// we had a port already, check if its free
var portStillFree = Converters.IsPortAvailable(int.Parse(getLastUsedPort));

if (!portStillFree)
{
// port is no longer free, retrieve new one, write to config and update on BC
freePort = Converters.GetFreePort();
var updater = new ConfigUpdater();
updater.UpdateConfig("Application:FunctionAppPort", freePort.ToString());

//Update new port on BC
using (var scope = app.Services.CreateScope())
{
var ntlmRegisterSender = scope.ServiceProvider.GetRequiredService();
await ntlmRegisterSender.UpdateFieldValue("Port", freePort.ToString());
}
}
else
{
// do nothing, leave everythign as it is
}
}

var logger = app.Services.GetRequiredService();

// Hide the console window
HideConsoleWindow();

app.UseRouting();
app.MapControllers();

Thread.Sleep(1000);

string uripath = builder.Configuration["Values:Application:FunctionAppUrl"] ?? "http://localhost:";

uripath += builder.Configuration["Values:Application:FunctionAppPort"] ?? "7072";

string trayAppPath = Converters.GetPathToWPFApp(builder.Configuration["TrayAppPath"]);
try
{
Process.Start(trayAppPath, uripath);
logger.LogInformation("Successfully started the tray app.");
}
catch (Exception ex)
{
logger.LogWarning(ex, "Could not start the tray app.  Check local.settings.json for the correct path.");
}

try
{
app.Run(uripath);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to start server: {ex.Message}");
}

void HideConsoleWindow()
{
IntPtr handle = GetConsoleWindow();
if (handle != IntPtr.Zero)
{
ShowWindow(handle, 0);
}
}

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post