6

这个问题可能已被多次询问,也许我只是在这里使用搜索功能和谷歌不好,但我还没有找到这个问题的答案。引用的DLL没有被复制到引用项目

我目前有两个项目,项目X,项目Y和项目Z(仅持有映射)

项目X是FluentNhibernate项目持有我sessionfactories和这样的事情。所以这也是映射加载和东西的地方(这很重要,我怀疑它可能是我遇到的问题的全部原因)

现在在Project X中,我引用了一个使用Microsoft的程序集。 SqlServer.Types.dll。

项目Y是一个服务,它使用项目X为它的数据库连接。

所有的问题都会在我的开发机器上完美地找到并运行,但是当部署到我们的服务器时,它们无法运行(运行时错误)。这个错误非常晦涩难懂,因为它指向的是缺少的FluentNHibernate程序集。

Procmon.exe幸运地显示代码尝试加载Microsoft.SqlServer.Types.dll,因为服务器没有安装SQL Server(我的客户端不是,但它有一个管理工作室,最有可能安装此.DLL)。

到目前为止好,复制DLL,它的工作(耶!)。

现在我想我会将程序集添加到项目X以确保将此引用复制到其他项目使用项目X.这没有发生....所以我试图设置'复制本地'设置为true。

可悲的是这仍然没有.dll文件复制到引用项目Y.

有没有办法做到这一点,或者这是Visual Studio的beeing巧妙,实现项目Y不需要这个.dll和从而拒绝复制它?

(由于项目ž需要实际的参考和项目X加载此组件@运行时没有Z和X之间的“硬”参考)

可以在任何#2天才一些线索这一点,因为老实说,我”我想知道为什么它会这样做(理想情况下,它的行为就像我想要的那样)。

回答

1

现在我想我会将程序集添加到项目X以确保将此引用复制到其他项目使用项目X.这没有发生....所以我试图设置'复制本地'设置为true。

你还在谈论Microsoft.SqlServer.Types.dll的权利?

前段时间我有同样的问题,实际上sqlserver类型不应该复制和重新分配您的安装。 “安装”它的正确方法是下载sql server运行时(它包含在sql server管理工具中,这就是为什么它在本地适用于你)。

因为它是前一段时间,我不是100%确定如果以下信息是正确的并且可以正常工作,但只是尝试仅安装Microsoft® System CLR Types for Microsoft® SQL Server® 2012。在此下载页面上,展开安装说明并向下滚动到CLR类型下载...

使用this link for SQL Server 2008

在目标服务器上安装该组件。这只会安装运行这种类型的DLL所需的东西...

+0

我还在谈Microsoft.SqlServer.Types.dll肯定。但是,为什么在这种情况下将它与您的实际软件分发不好呢?这里有什么最佳实践的东西吗? –

+0

类型的DLL只是一个包装的SQL Server自身也用于执行空间操作的DLL ...这些DLL是用C/C++编写的,如果您只是复制类型的DLL,则不会被复制...你可能会导致也复制SQLServerSpatial.dll(这是本机DLL) – MichaC

+0

嗯,我在源代码中使用这些类型的第三个,我已经改变使用流畅的nhibernate与地理信息和SQL Server 2012方言。根据您提供的信息安装CLR类型似乎可行。所以我会做更多的测试,如果那些成功,我会标记你的答案。这就是说我怀疑Visual Studio拒绝复制DLL,因为它们是用C/C++编写的。底层的问题仍然存在,我很想在这里回答,但其他人因为Microsoft.SqlServer.Types.dll而使用安装程序,因此在这里找到了方法。 –

7

这个问题已经回答了,但我想我会包括一个通用的故障安全方法,将所有间接引用复制到您的项目的输出目录。

  1. 设置复制你想要的所有引用本地真正将包含在你的输出。
  2. 将副本间接依赖关系代码(见下文)保存到名称为CopyIndirectDependencies.targets的文件中,并将目标文件放置在项目目录中。
  3. 编辑您的.csproj.vbproj以包含您添加的.targets文件的导入。看下面的例子。
  4. 建立你的项目,你应该有第二个依赖关系自动复制到生成输出。

CopyIndi​​rectDependencies.targets文件内容与进口

<?xml version="1.0" encoding="utf-8"?> 
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup> 
    <CopyIndirectDependencies 
     Condition="'$(CopyIndirectDependencies)'==''">true</CopyIndirectDependencies> 
    <CopyIndirectDependenciesPdb 
     Condition="'$(CopyIndirectDependenciesPdb)'==''">false</CopyIndirectDependenciesPdb> 
    <CopyIndirectDependenciesXml 
     Condition="'$(CopyIndirectDependenciesXml)'==''">false</CopyIndirectDependenciesXml> 
    </PropertyGroup> 


    <!-- BuildXxx part --> 

    <Target Name="CopyIndirectDependencies" 
      Condition="'$(CopyIndirectDependencies)'=='true'" 
      DependsOnTargets="DetectIndirectDependencies"> 
    <Copy Condition="'%(IndirectDependency.FullPath)'!=''" 
      SourceFiles="%(IndirectDependency.FullPath)" 
      DestinationFolder="$(OutputPath)" 
      SkipUnchangedFiles="true" > 
     <Output TaskParameter="CopiedFiles" 
       ItemName="IndirectDependencyCopied" /> 
    </Copy> 
    <Message Importance="low" 
      Condition="'%(IndirectDependencyCopied.FullPath)'!='' 
       and '%(IndirectDependencyCopied.Extension)'!='.pdb' 
       and '%(IndirectDependencyCopied.Extension)'!='.xml'" 
      Text="Indirect dependency copied: %(IndirectDependencyCopied.FullPath)" /> 
    </Target> 

    <Target Name="DetectIndirectDependencies" 
      DependsOnTargets="ResolveAssemblyReferences"> 

    <Message Importance="low" 
      Text="Direct dependency: %(ReferencePath.Filename)%(ReferencePath.Extension)" /> 
    <Message Importance="low" 
      Text="Indirect dependency: %(ReferenceDependencyPaths.Filename)%(ReferenceDependencyPaths.Extension)" /> 

    <!-- Creating indirect dependency list --> 
    <CreateItem Include="%(ReferenceDependencyPaths.FullPath)" 
       Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true'"> 
     <Output TaskParameter="Include" 
       ItemName="_IndirectDependency"/> 
    </CreateItem> 
    <CreateItem Include="%(ReferenceDependencyPaths.RootDir)%(ReferenceDependencyPaths.Directory)%(ReferenceDependencyPaths.Filename).xml" 
       Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true' and '$(CopyIndirectDependenciesXml)'=='true'"> 
     <Output TaskParameter="Include" 
       ItemName="_IndirectDependency"/> 
    </CreateItem> 
    <CreateItem Include="%(ReferenceDependencyPaths.RootDir)%(ReferenceDependencyPaths.Directory)%(ReferenceDependencyPaths.Filename).pdb" 
       Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true' and '$(CopyIndirectDependenciesPdb)'=='true'"> 
     <Output TaskParameter="Include" 
       ItemName="_IndirectDependency"/> 
    </CreateItem> 

    <!-- Filtering indirect dependency list by existence --> 
    <CreateItem Include="%(_IndirectDependency.FullPath)" 
       Condition="Exists('%(_IndirectDependency.FullPath)')"> 
     <Output TaskParameter="Include" 
       ItemName="IndirectDependency"/> 
    </CreateItem> 

    <!-- Creating copied indirect dependency list --> 
    <CreateItem Include="@(_IndirectDependency->'$(OutputPath)%(Filename)%(Extension)')"> 
     <Output TaskParameter="Include" 
       ItemName="_ExistingIndirectDependency"/> 
    </CreateItem> 

    <!-- Filtering copied indirect dependency list by existence --> 
    <CreateItem Include="%(_ExistingIndirectDependency.FullPath)" 
       Condition="Exists('%(_ExistingIndirectDependency.FullPath)')"> 
     <Output TaskParameter="Include" 
       ItemName="ExistingIndirectDependency"/> 
    </CreateItem> 

    </Target> 


    <!-- Build sequence modification --> 

    <PropertyGroup> 
    <CoreBuildDependsOn> 
     $(CoreBuildDependsOn); 
     CopyIndirectDependencies 
    </CoreBuildDependsOn> 
    </PropertyGroup> 
</Project> 

示例项目

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

    ... 

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 
    <Import Project="CopyIndirectDependencies.targets" /> <!-- ADD THIS LINE!! --> 

    ... 

</Project> 

来源:http://blog.alexyakunin.com/2009/09/making-msbuild-visual-studio-to.html

+0

在项目文件中添加步骤3中的行的位置是否重要?例如,它是否必须在AfterBuild目标下或其他地方(或任何地方)? – longda

+4

嗯,这在Visual Studio 2012中不起作用。我希望我错了。 – longda

+0

@longda:请参阅我为此提供的答案。它有一个示例项目。 –

相关问题