public override async Task <Kernel> CreateKernelAsync( MsSqlConnectionOptions options, KernelInvocationContext context) { var resolvedPackageReferences = ((ISupportNuget)context.HandlingKernel).ResolvedPackageReferences; // Walk through the packages looking for the package that endswith the name "Microsoft.SqlToolsService" // and grab the packageroot var runtimePackageIdSuffix = "native.Microsoft.SqlToolsService"; var root = resolvedPackageReferences.FirstOrDefault(p => p.PackageName.EndsWith(runtimePackageIdSuffix, StringComparison.OrdinalIgnoreCase)); string pathToService = ""; if (root != null) { // Extract the platform 'osx-x64' from the package name 'runtime.osx-x64.native.microsoft.sqltoolsservice' string[] packageNameSegments = root.PackageName.Split("."); if (packageNameSegments.Length > 2) { string platform = packageNameSegments[1]; // Build the path to the MicrosoftSqlToolsServiceLayer executable by reaching into the resolve nuget package // assuming a convention for native binaries. pathToService = Path.Combine( root.PackageRoot, "runtimes", platform, "native", "MicrosoftSqlToolsServiceLayer"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { pathToService += ".exe"; } } } var sqlClient = new MsSqlServiceClient(pathToService); var kernel = new MsSqlKernel( $"sql-{options.KernelName}", options.ConnectionString, sqlClient); await kernel.ConnectAsync(); if (options.CreateDbContext) { await InitializeDbContextAsync(options, context); } return(kernel); }
public MsSqlKernel(string pathToService, string name, string connectionString) : base(name) { var filePath = Path.GetTempFileName(); _tempFileUri = new Uri(filePath); _connectionString = connectionString; _serviceClient = new MsSqlServiceClient(pathToService); _serviceClient.Initialize(); _serviceClient.OnConnectionComplete += HandleConnectionComplete; _serviceClient.OnQueryComplete += HandleQueryComplete; _serviceClient.OnIntellisenseReady += HandleIntellisenseReady; _serviceClient.OnQueryMessage += HandleQueryMessage; RegisterForDisposal(() => { if (_connected) { Task.Run(() => _serviceClient.DisconnectAsync(_tempFileUri)).Wait(); } }); RegisterForDisposal(() => File.Delete(_tempFileUri.LocalPath)); }