|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2026, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using BH.Engine.Base; |
| 24 | +using BH.oM.Base; |
| 25 | +using BH.oM.Base.Attributes; |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.ComponentModel; |
| 29 | +using System.IO; |
| 30 | +using System.Linq; |
| 31 | +using System.Reflection; |
| 32 | +using System.Text; |
| 33 | +using System.Threading.Tasks; |
| 34 | + |
| 35 | +namespace BH.Engine.SQL |
| 36 | +{ |
| 37 | + public static partial class Compute |
| 38 | + { |
| 39 | + /***************************************************/ |
| 40 | + /**** Public Methods ****/ |
| 41 | + /***************************************************/ |
| 42 | + |
| 43 | + [Description("Make sure the correct version of Microsoft.Data.SqlClient before Rhino 8 has a chance to load its stub dll. Temporary solution while wating for a fix from McNeel.")] |
| 44 | + [Output("success", "return true if the code was executed without error.")] |
| 45 | + public static bool PreloadSqlClient() |
| 46 | + { |
| 47 | + // Only relevant on .NET 5+ (CoreCLR / Rhino 8). |
| 48 | + // On .NET Framework 4.7.2 (Rhino 7) the System.Data.SqlClient inbox provider is used |
| 49 | + // and Microsoft.Data.SqlClient is not involved. |
| 50 | + if (Environment.Version.Major < 5) |
| 51 | + return true; |
| 52 | + |
| 53 | + // Assembly path for the version of the SqlCleint we want to load |
| 54 | + string assemblyPath = Path.Combine( |
| 55 | + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), |
| 56 | + "BHoM", "Assemblies", "net7.0", "Microsoft.Data.SqlClient.dll"); |
| 57 | + |
| 58 | + // Guard: if SqlClient is already in the Default ALC we are too late. |
| 59 | + // Check whether what is loaded is the real implementation or Rhino's stub. |
| 60 | + Assembly already = AppDomain.CurrentDomain.GetAssemblies() |
| 61 | + .FirstOrDefault(a => a.GetName().Name == "Microsoft.Data.SqlClient"); |
| 62 | + |
| 63 | + if (already != null) |
| 64 | + { |
| 65 | + if (!already.Location.Contains("BHoM")) |
| 66 | + { |
| 67 | + BH.Engine.Base.Compute.RecordWarning( |
| 68 | + "Microsoft.Data.SqlClient was already loaded from Rhino's directory before the SQL Toolkit " + |
| 69 | + $"pre-load could run: {already.Location}. The SQL Adapter will not work correctly in Rhino 8. " + |
| 70 | + "This indicates that a Rhino plugin loaded the stub at Rhino startup. " + |
| 71 | + "Consider switching to the ALC-isolation approach described in 14_OptionA_ALC_Implementation.md."); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Already loaded from BHoM's own directory — nothing to do. |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + if (!File.Exists(assemblyPath)) |
| 80 | + { |
| 81 | + BH.Engine.Base.Compute.RecordWarning( |
| 82 | + $"Microsoft.Data.SqlClient.dll was not found at the expected path: {assemblyPath}. " + |
| 83 | + "The SQL Adapter may not work correctly in Rhino 8. " + |
| 84 | + "Ensure SQL_Adapter has been built with the net7.0 target framework."); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + try |
| 89 | + { |
| 90 | + Assembly.LoadFrom(assemblyPath); |
| 91 | + return true; |
| 92 | + } |
| 93 | + catch (Exception ex) |
| 94 | + { |
| 95 | + BH.Engine.Base.Compute.RecordError(ex, "Failed to pre-load Microsoft.Data.SqlClient from the BHoM Assemblies folder."); |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /***************************************************/ |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments