【问题标题】:netstandard code generator Microsoft.Data.SqlClient throwing FileNotFound System.Security.Principal.Windows, Version=4.1.1.0netstandard 代码生成器 Microsoft.Data.SqlClient 抛出 FileNotFound System.Security.Principal.Windows,版本 = 4.1.1.0
【发布时间】:2023-03-28 03:57:01
【问题描述】:

从核心 5.0 控制台应用程序引用我的 netstandard 2.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() 中返回的程序集的一部分

【问题讨论】:

  • 只需将the package添加到应用程序
  • 这不起作用,这不是运行时,这是编译时间,您必须在 csproj 中使用 msbuild 来使用 github.com/dotnet/roslyn-sdk/blob/master/samples/CSharp/… 这不像添加引用那么简单跨度>
  • 我在分析器中尝试使用 SqlClient 时遇到了同样的问题。问题是您需要为编译运行时引入正确的依赖项。我的预感是包的一个 Build 是不够的。分析器需要 Publish 中的所有内容(使用当前编译器平台的 RuntimeIdentifier - 尤其是运行时文件夹的内容),才能正常运行。
  • 您将 .NET Standard 2.0 和 C# 9 结合起来对我来说似乎很奇怪。AFAIK 这不是一个有效的配置
  • 如果你愿意,你可以结合 netstandard2.0 和 C#9。它可能会碰巧产生难以解决的问题。如果你不使用不受支持的语法,一切都很好。

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


【解决方案1】:

您可以将所需的程序集映射到 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

【讨论】:

  • 没有app.config文件怎么办?这是针对 netstandard 2.0 项目的。
  • 在调用 .net 标准程序集的 .net 5 客户端应用程序中添加映射
  • 谢谢,但这不起作用。我在控制台应用程序中添加了一个 app.config,因此 xml 看起来像 &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="System.Security.Principal.Windows" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="4.1.1.0" /&gt; &lt;/dependentAssembly&gt; &lt;/configuration&gt; 但是当构建控制台应用程序并且代码生成器运行时同样的问题
  • 有没有检查构建项目后生成的config文件是否有映射
  • 您必须在生成器的编译时运行期间使用 msbuild 添加引用,以这些示例github.com/dotnet/roslyn-sdk/blob/master/samples/CSharp/… 为例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多