My netstandard 2.0 code generator being referenced from a core 5.0 console application.
The csproj looks like this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Func" Version="0.2.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>
<!-- Generator dependencies -->
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<!--<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Security_Principal_Windows)\runtimes\win\lib\netstandard1.3\System.Security.Principal.Windows.dll" IncludeRuntimeDependency="false" />-->
<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Data_SqlClient)\runtimes\win\lib\netstandard2.0\Microsoft.Data.SqlClient.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>
</Project>
and references Microsoft.Data.SqlClient.
when attempting to create a new SqlConnection like this
_dbConnection = new SqlConnection();
_dbConnection.ConnectionString = @"validconnectionstring";
_dbConnection.Open();
On the open of the connection an Exception is thrown with message:
Could not load file or assembly 'System.Security.Principal.Windows, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source:
Microsoft.Data.SqlClient
Stacktrace:
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrentNative()
at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory)
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at CSharp.Data.Sql.Schema.Provider.SqlServer.DbConnectionAsync..ctor() in C:\Dev\CSharpSQLProvider\SQLProvider\CSharp.Data.Sql\Schema\Provider\SqlServer\DbConnectionAsync.cs:line 18
at CSharp.Data.Sql.Generator.DataContextGenerator.Execute(GeneratorExecutionContext context) in C:\Dev\CSharpSQLProvider\SQLProvider\CSharp.Data.Sql\Generator\DataContextGenerator.cs:line 57
Innerexception is null.
To be clear, I am able to create an instance of SqlConnection just when setting the connection string this error is thrown. I also tried the System.Data.SqlClient, however I wasn't able to create a SqlConnection instance but the same exception was thrown just with a different assembly name.
I have also tried adding the dependency into the csproj in the same way as I have for Microsoft.Data.SqlClient in the example given to no avail, also attempted to load the required assemblies into the app domain, and also tried adding the assembly into the context compilation.
var assembly = Assembly.LoadFrom(@"C:\Users\{user}\.nuget\packages\system.security.principal.windows\4.4.1\runtimes\win\lib\netcoreapp2.0\System.Security.Principal.Windows.dll");
var refe = MetadataReference.CreateFromFile(assembly.Location);
var foo = context.Compilation.ExternalReferences.Add(refe);
var d = context.Compilation.AddReferences(new[] { refe });
I suspect I'm doing something wrong and any help would be appreciated. Thank you.
I should also mention that the missing assembly is apart of the returned assemblies in AppDomain.CurrentDomain.GetAssemblies() within the execute method