与ILMerge

时间:2016-04-28 08:24:50

标签: c# entity-framework ilmerge

希望你能帮助我,因为我没有想法。我在我的项目中使用Entity Framework 6,该项目经过良好测试并且工作得非常完美。

直到我决定将everythig合并到一个.exe文件中。从那以后,我遇到了问题。为此,我决定使用ILMerge。将它安装为nuget包并写了Ilmerge.CSharp.targets,如下所示:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Target Name="AfterBuild">
      <CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
         <Output ItemName="AssembliesToMerge" TaskParameter="Include" />
      </CreateItem>
      <PropertyGroup>
         <ReferenceAssemblies>C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5</ReferenceAssemblies>
      </PropertyGroup>     
      <Exec Command="&quot;..\packages\ilmerge.2.14.1208\tools\Ilmerge.exe&quot; /out:@(MainAssembly) &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')"/>
      <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
   </Target>               
</Project>

编译成功后,我运行我的解决方案并获得运行时错误

  

为entityFramework创建配置节处理程序时出错:无法加载文件或程序集'EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'或其依赖项之一。

有什么想法吗?顺便说一下...... Log4Net在合并后似乎也无法正常工作。

谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

在您的配置文件(app.config)中,您很可能会遇到以下情况:

<configuration>
  <configSections>    
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
         <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <!-- more stuff here -->
</configuration>

请注意,EntityFramework程序集中有多行引用类型,例如:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

但是在将所有内容合并到一个程序集中之后 - 再也找不到EntityFramework dll了。您必须修复这些引用以指向您的主程序集。假设您的应用程序.exe名为MergeTest.exe。然后要解决此问题,请将每个出现的EntityFramework引用替换为MergeTest:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, MergeTest" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, MergeTest">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, MergeTest" />
    </providers>
  </entityFramework>
  <!-- More stuff here -->
</configuration>

与log4net引用相同的故事。