foundry sdk can't start service

This commit is contained in:
vanzue
2025-11-04 14:47:35 +08:00
parent a878208053
commit bbb6f3ca63
3 changed files with 139 additions and 5 deletions

View File

@@ -48,7 +48,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.9" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.8" />
<PackageVersion Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.8" />
<PackageVersion Include="Microsoft.AI.Foundry.Local" Version="0.1.0" />
<PackageVersion Include="Microsoft.AI.Foundry.Local" Version="0.3.0" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.66.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.66.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.Amazon" Version="1.66.0-alpha" />
@@ -106,7 +106,7 @@
<PackageVersion Include="System.Diagnostics.EventLog" Version="9.0.8" />
<!-- Package System.Diagnostics.PerformanceCounter added as a hack for being able to exclude the runtime assets so they don't conflict with 8.0.11. -->
<PackageVersion Include="System.Diagnostics.PerformanceCounter" Version="9.0.8" />
<PackageVersion Include="System.ClientModel" Version="1.7.0" />
<PackageVersion Include="System.ClientModel" Version="1.7.0" />
<PackageVersion Include="System.Drawing.Common" Version="9.0.8" />
<PackageVersion Include="System.IO.Abstractions" Version="22.0.13" />
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="22.0.13" />
@@ -139,4 +139,4 @@
<PackageVersion Include="Microsoft.VariantAssignment.Client" Version="2.4.17140001" />
<PackageVersion Include="Microsoft.VariantAssignment.Contract" Version="3.0.16990001" />
</ItemGroup>
</Project>
</Project>

View File

@@ -14,12 +14,18 @@ internal sealed class FoundryClient
try
{
Logger.LogInfo("[FoundryClient] Creating Foundry Local client");
// Workaround for SDK issue: FoundryLocalManager.StartServiceAsync() uses UseShellExecute=false
// which cannot handle Windows App Execution Aliases (foundry.exe in WindowsApps)
// Pre-start the service using UseShellExecute=true
await EnsureFoundryServiceStarted().ConfigureAwait(false);
var manager = new FoundryLocalManager();
// Ensure service is running
// Check if service is running
if (!manager.IsServiceRunning)
{
Logger.LogInfo("[FoundryClient] Starting Foundry Local service");
Logger.LogInfo("[FoundryClient] Service not running, attempting to start via SDK");
await manager.StartServiceAsync().ConfigureAwait(false);
if (!manager.IsServiceRunning)
@@ -35,10 +41,42 @@ internal sealed class FoundryClient
catch (Exception ex)
{
Logger.LogError($"[FoundryClient] Error creating client: {ex.Message}");
if (ex.InnerException != null)
{
Logger.LogError($"[FoundryClient] Inner exception: {ex.InnerException.Message}");
}
return null;
}
}
private static async Task EnsureFoundryServiceStarted()
{
try
{
Logger.LogInfo("[FoundryClient] Pre-starting foundry service with UseShellExecute=true");
using var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "foundry";
process.StartInfo.Arguments = "service start";
process.StartInfo.UseShellExecute = true; // Critical: allows App Execution Alias to work
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
// Give the service a moment to start, but don't wait for completion
// as the service runs in the background
await Task.Delay(2000).ConfigureAwait(false);
Logger.LogInfo("[FoundryClient] Foundry service start command completed");
}
catch (Exception ex)
{
Logger.LogWarning($"[FoundryClient] Failed to pre-start foundry service: {ex.Message}");
}
}
private readonly FoundryLocalManager _foundryManager;
private readonly List<FoundryCatalogModel> _catalogModels = [];

View File

@@ -0,0 +1,96 @@
using System;
using System.Diagnostics;
using System.IO;
class TestFoundryProcess
{
static void Main(string[] args)
{
Console.WriteLine("=== Testing foundry.exe Process.Start ===");
Console.WriteLine();
Console.WriteLine($"Current Directory: {Environment.CurrentDirectory}");
Console.WriteLine();
// Test 1: UseShellExecute = false (like SDK does)
Console.WriteLine("Test 1: UseShellExecute = false");
try
{
using var process = new Process();
process.StartInfo.FileName = "foundry";
process.StartInfo.Arguments = "--version";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
Console.WriteLine($" Attempting to start foundry...");
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
Console.WriteLine($" SUCCESS!");
Console.WriteLine($" Output: {output}");
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine($" Error: {error}");
}
}
catch (Exception ex)
{
Console.WriteLine($" FAILED: {ex.Message}");
Console.WriteLine($" Type: {ex.GetType().Name}");
}
Console.WriteLine();
// Test 2: Change directory to parent
Console.WriteLine("Test 2: Change to AppData\\Local and retry");
string originalDir = Environment.CurrentDirectory;
try
{
string testDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PowerToys");
if (Directory.Exists(testDir))
{
Environment.CurrentDirectory = testDir;
Console.WriteLine($" Changed directory to: {Environment.CurrentDirectory}");
using var process = new Process();
process.StartInfo.FileName = "foundry";
process.StartInfo.Arguments = "--version";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
Console.WriteLine($" Attempting to start foundry...");
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine($" SUCCESS!");
Console.WriteLine($" Output: {output}");
}
else
{
Console.WriteLine($" Directory doesn't exist: {testDir}");
}
}
catch (Exception ex)
{
Console.WriteLine($" FAILED: {ex.Message}");
Console.WriteLine($" Type: {ex.GetType().Name}");
}
finally
{
Environment.CurrentDirectory = originalDir;
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}