c# - netstandard 代码生成器 Microsoft.Data.SqlClient 抛出 FileNotFound System.Security.Principal.Windows,Version=4.1.1.0

标签 c# code-generation roslyn c#-9.0

我的 netstandard 2.0 代码生成器被核心 5.0 控制台应用程序引用。
csproj 看起来像这样

<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>

并引用 Microsoft.Data.SqlClient。
尝试创建新 SqlConnection 时像这样
_dbConnection = new SqlConnection();           
_dbConnection.ConnectionString = @"validconnectionstring";
_dbConnection.Open();
在连接打开时,会抛出一个带有消息的异常:
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
内部异常为空。
明确地说,我能够在设置连接字符串时创建一个 SqlConnection 实例,这个错误被抛出。我还尝试了 System.Data.SqlClient,但是我无法创建 SqlConnection 实例,但只是使用不同的程序集名称引发了相同的异常。
我还尝试将依赖项添加到 csproj 中,就像我在示例中为 Microsoft.Data.SqlClient 所做的那样无济于事,还尝试将所需的程序集加载到应用程序域中,并尝试将程序集添加到上下文编译。
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 });
我怀疑我做错了什么,任何帮助将不胜感激。谢谢你。
我还应该提到,缺少的程序集是执行方法中 AppDomain.CurrentDomain.GetAssemblies() 中返回的程序集的一部分

最佳答案

您可以将所需的程序集映射到 app.config 中的所需版本,如下所示。

 <dependentAssembly>
        <assemblyIdentity name="System.Security.Principal.Windows" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="4.1.1.0" />
      </dependentAssembly

关于c# - netstandard 代码生成器 Microsoft.Data.SqlClient 抛出 FileNotFound System.Security.Principal.Windows,Version=4.1.1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66091441/

相关文章:

c# - 两个具有相同值的字符串如何存储在内存中?

c# - DbContext 代码生成策略在 Entity Framework 5 和 VS 2012 中失败

go - 如何在函数中多次添加一些行?

c# - 如何在保留琐事的同时替换节点?

c# - 找到属于 InvocationExpressionSyntax 的 UsingDirectiveSyntax

c# - 删除列表中除第一项以外的所有项

c# - 如何在 VS 2012 中获取 Crystal Reports?

java - 为什么Eclipse不在 "Generate Delegate Methods"重构中添加@Override注解?

c# - 如何防止字段在 EDMX 更新后被删除

c# - 如何从 Roslyn 中的 using 指令获取完全限定的命名空间?