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:請參閱我爲此提供的答案。它有一個示例項目。 –

相關問題