搭建公司团队内部的NuGetServer

一. NuGetServer 搭建和配置

1. 创建一个 “NuGetServer” 解决方案,然后新增 “NuGetServer” Asp.Net 网站。结构如下图


2. 在 “NuGetServer” 项目上,右键选择 “管理NuGet程序包” ,选择 “联机” ,右上角搜索框中输入“NuGet.Server”  Enter,在搜索结果中选择 NuGet.Server 项,进行安装,如图


如果安装最后,提示 替换 Web.config ,请选择 全是。

3. 编译“NuGetServer”项目,如果没有出异常,这里就创建项目完成,NuGetServer 就这么简单建成,稍后我部署到IIS上面,看看是不是真的可以了,先来讲一下这个网站WebConfig 要配置地方。

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.6" />
      </system.Web>
  --><system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <!-- maxRequestLength is specified in Kb --><httpRuntime targetFramework="4.6.1" maxRequestLength="30720" />
    <!--<compilation debug="true" targetFramework="4.6" />-->
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
    </compilers>
  </system.codedom>
<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    <remove name="WebDAV" /><!-- Depending on IIS configuration, these may have to be added.
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,PUT,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <remove name="OPTIONSVerbHandler" /><remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      -->
    </handlers>
  <staticContent>
      <mimeMap fileExtension=".nupkg" mimeType="application/zip" />
    </staticContent><modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules><security>
      <requestFiltering>
        <!-- maxAllowedContentLength is specified in Bytes -->
        <requestLimits maxAllowedContentLength="31457280" />
      </requestFiltering>
    </security></system.webServer><appSettings>
    <!--
    Determines if an Api Key is required to push\delete packages from the server. 
    -->
    <add key="requireApiKey" value="true" />

    <!-- 
    Set the value here to allow people to push/delete packages from the server.
    NOTE: This is a shared key (password) for all users.
    -->
    <add key="apiKey" value="0226651E-19EE-4F93-A172-5250C84DB16C" />

    <!--
    Change the path to the packages folder. Default is ~/Packages.
    This can be a virtual or physical path.
    -->
    <add key="packagesPath" value="" />

    <!--
    Change the name of the internal cache file. Default is machine name (System.Environment.MachineName).
    This is the name of the cache file in the packages folder. No paths allowed.
    -->
    <add key="cacheFileName" value="" />

    <!--
    Set allowOverrideExistingPackageOnPush to false to mimic NuGet.org's behaviour (do not allow overwriting packages with same id + version).
    -->
    <add key="allowOverrideExistingPackageOnPush" value="false" />

    <!--
    Set ignoreSymbolsPackages to true to filter out symbols packages. Since NuGet.Server does not come with a symbol server,
    it makes sense to ignore this type of packages. When enabled, files named `.symbols.nupkg` or packages containing a `/src` folder will be ignored.
    
    If you only push .symbols.nupkg packages, set this to false so that packages can be uploaded.
    -->
    <add key="ignoreSymbolsPackages" value="true" />

    <!--
    Set enableDelisting to true to enable delist instead of delete as a result of a "nuget delete" command.
    - delete: package is deleted from the repository's local filesystem.
    - delist: 
      - "nuget delete": the "hidden" file attribute of the corresponding nupkg on the repository local filesystem is turned on instead of deleting the file.
      - "nuget list" skips delisted packages, i.e. those that have the hidden attribute set on their nupkg.
      - "nuget install packageid -version version" command will succeed for both listed and delisted packages.
        e.g. delisted packages can still be downloaded by clients that explicitly specify their version.
    -->
    <add key="enableDelisting" value="false" />

    <!--
    Set enableFrameworkFiltering to true to enable filtering packages by their supported frameworks during search.
    -->
    <add key="enableFrameworkFiltering" value="false" />

    <!--
    When running NuGet.Server in a NAT network, ASP.NET may embed the server's internal IP address in the V2 feed.
    Uncomment the following configuration entry to enable NAT support.
    -->
    <!-- <add key="aspnet:UseHostHeaderForRequestUrl" value="true" /> -->
    <!--
    Set enableFileSystemMonitoring to true (default) to enable file system monitoring (which will update the package cache appropriately on file system changes).
    Set it to false to disable file system monitoring.
    NOTE: Disabling file system monitoring may result in increased storage capacity requirements as package cache may only be purged by a background job running 
    on a fixed 1-hour interval.
    -->
    <add key="enableFileSystemMonitoring" value="true" />
  </appSettings><system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel><runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime></configuration>



第一点: 程序包发布,存放的路径,在WebConfig appSettings PackagesPath 这个Key 设置,默认存放在部署的网站根目录Packages文件夹。PackagesPath可不赋值。

第二点:appSettings requireApiKey 这个key 如果设置成 true , appSettings  apiKey 是必须要设置的,这个就像密码一样。可以阻止不知道这个apiKey人访问到程序包

4. 部署到IIS, 打开IIS ,右键选择  “网站”  ,添加网站,填写 网站名称(NuGetServer),选择物理路径,端口号 改成 "1000" 确定, 如下图

5. 运行 NuGetServer 网站 "http://172.168.18.110:8025/", 出现下图效果,则说NuGet 服务器 已经建立完成。


6. 接着配置Visual Studio 连接 NuGetServer。选择“工具”菜单,选择“选项”,弹出“选项”界面,选择 “NuGet Package Manager” ,然后在选择 “程序包源”,

点击 “+”,在界面下方 设置 名称 “mynuget.org” 随便取,设置 源 “http://172.168.18.110:8025/nuget” (是不是上图有说),确定 关闭界面,回到项目。如下图


在项目右键,选择“管理NuGet程序包”,联机,下面是不是多出了一个 “MyNugetDLL” 程序源呢,虽然下面还没有发布公司内部的程序包。如下图

到此,NuGetServer 服务器,就搭建并配置完成。接一下来就是如何发布程序包,以及安装程序包了。

二. 发布程序包,以及安装程序包

1. NuGet Package Explorer

将程序包发布到NuGetServer,还要介绍到另外一个工具“NuGet Package Explorer”,这个工具是NuGetServer 程序包一个可视化的工具,它功能很多,可浏览已经发布的程序包信息,可以发布新的程序包(设置程序包版本,已经依赖程序包等),可以删除发布的程序包。

CodePlex:https://npe.codeplex.com/

GitHub:https://github.com/NuGetPackageExplorer

2.在CodePlex 网站上,下载  NuGet Package Explorer , 安装完成后,桌面会多出一个 “NuGet Package Explorer” 图标,如下图

 

3. 为了方便Demo,再创建一个 解决方案 “NuGetServerDemoSolution”,添加“NuGetServerDemo” 控制台项目,再添加 “NuGetServerDemoDLL” 类库项目,结构如下图。

“NuGetServerDemoDLL” 项目 主要会做成程序包发布

“NuGetServerDemo” 项目 安装“NuGetServerDemoDLL” 程序包

4 . 打开 桌面 “NuGet Package Explorer” ,界面如下

 

图片选项, 分别 意思是,1. 打开本地的nupkg,nuspec 文件。2. 打开指定 NuGetServer 所有的程序包列表。3.创建一个新程序包。4. 文档

5.把 “NuGetServerDemoDLL” 发布到NuGetServer,点 “Create  a new package” 未设置前截图如下

上图分为两个编辑区,一个是 Package Metadata 负责描述程序包信息的,Package Contents 负责程序包文件相关的。

点击   Package Metadata 区 “编辑” 按钮,想编辑 “NuGetServerDemoDLL”  程序包描述信息。

然后 将“NuGetServerDemoDLL” 项目 产生Dll,拖入 Package Contents   最后效果如下图

点击 上图 绿色的 √ 关闭编辑Package Metadata , 点击 ”File“ 菜单,选择 Publish 发布程序集,填写 PublishUrl(NuGetServer),PublishKey(apiKey),填写完成 点击“Publish” 发布,如果下方提示 “Package published successfully”,则发布成功。如下图。


6. 回到“NuGetServerDemoSolution” 解决方案,右键“NuGetServerDemo”,选择“管理NuGet程序包”,选择联机下“mynuget.org”,安装“NuGetServerDemoDLL” 程序包,如下图

主要看图左边的 ,是不是“NuGet Package Explorer” 中设置过的一些程序包信息。

7. 用“NuGet Package Explorer”查看 NuGetServer 以发布程序包,选择“File”菜单,选择"Open from feed", 就会查询到指定 NuGetServer 发布程序包。


猜你喜欢

转载自blog.csdn.net/qq_18145031/article/details/80704903