天天看點

Net包管理NuGet(4)打包nuget包的進階用法

nuget包不止可以放進dll,還可以引用其他包,建立項目檔案及替換配置檔案

  • 引用的時候同時引用依賴項假設引用某個dll需要依賴其他的dll如Zeta.Core.dll需要引用ZETA.Serialization.Json.dll如下圖的包如何生成?
Net包管理NuGet(4)打包nuget包的進階用法

  1,參照第2步修改包的基礎資訊及基礎的dllNet包管理NuGet(2)nuget包的生成方法

  2,

Net包管理NuGet(4)打包nuget包的進階用法

  

Net包管理NuGet(4)打包nuget包的進階用法
Net包管理NuGet(4)打包nuget包的進階用法

   (補充:上圖右邊點選show all versions可以選擇包的版本關聯,如有多個版本可以選擇适合自己的版本)

Net包管理NuGet(4)打包nuget包的進階用法

  最後點選ok依賴關系就設定好了.

  • 源碼及檔案夾

    引用包之後根據包的檔案夾及檔案建立如下圖項目目錄

    

Net包管理NuGet(4)打包nuget包的進階用法

    建立包的方法

Net包管理NuGet(4)打包nuget包的進階用法
Net包管理NuGet(4)打包nuget包的進階用法

    結果如下圖:

Net包管理NuGet(4)打包nuget包的進階用法

 (1)源碼檔案需要替換命名空間的需要在檔案後面加.pp

Net包管理NuGet(4)打包nuget包的進階用法

上圖源碼内有命名空間 每個項目命名空間都有差異 要讓引用的包自動更換命名空間那就修改源碼檔案命名空間替換代碼為$rootnamespace$ 安裝之後就會被替換為項目的命名空間,如下

Net包管理NuGet(4)打包nuget包的進階用法

同理其他檔案如果需要動态的替換屬性打包時候一定要在檔案字尾加上.pp

 (2)webconfig内容替換及新增

包的 

content

 檔案夾中的 

app.config.transform

 和 

web.config.transform

 僅包含要合并到項目現有 

app.config

web.config

 檔案中的元素。

例如,假設項目最初在 

web.config

 中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>

<!--
  有關如何配置 ASP.NET 應用程式的詳細資訊,請通路
  https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

<system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NPOI" publicKeyToken="0df73ec7942b34e1" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.6.0" newVersion="2.0.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="NPOI.OOXML" publicKeyToken="0df73ec7942b34e1" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.6.0" newVersion="2.0.6.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>      

若要在包安裝期間将 

MyNuModule

 元素添加到 

modules

 部分,請在包的 

content

 檔案夾中建立 

web.config.transform

 檔案,如下所示:

<configuration>
<system.webServer>
    <handlers>
      <add name="st" verb="*" path="*.html" type="System.Web.StaticFileHandler"/>
    </handlers>
    <defaultDocument>
      <files>
        <add value="forms/login.html"/>
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>      

NuGet 安裝包後,

web.config

 将顯示為:

<?xml version="1.0" encoding="utf-8"?>

<!--
  有關如何配置 ASP.NET 應用程式的詳細資訊,請通路
  https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

<system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data><system.webServer>
    <handlers>
      <add name="st" verb="*" path="*.html" type="System.Web.StaticFileHandler" />
    </handlers>
    <defaultDocument>
      <files>
        <add value="forms/login.html" />
      </files>
    </defaultDocument>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NPOI" publicKeyToken="0df73ec7942b34e1" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.6.0" newVersion="2.0.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="NPOI.OOXML" publicKeyToken="0df73ec7942b34e1" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.6.0" newVersion="2.0.6.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>      

更多詳細内容可閱讀微軟官方文檔

有夢想一定要去做

但是未必一定要實作