I wrote a post a few days ago called "Creating a NuGet Package in 7 easy steps - Plus using NuGet to integrate ASP.NET MVC 3 into existing Web Forms applications." Long enough title? I think so.

The post exists for two reasons: First to show folks how insanely easy it is to create a NuGet package (and prompt you to make some yourself) and second to share with you some experiments where one enables new/interesting functionality after File|New Project. In that example, we had an existing ASP.NET WebForms application and added ASP.NET MVC to it in one line, making it an easy hybrid application.

Well, not really one line. In version 0.5 of the AddMvc3ToWebForms package you still have to hook up the Areas, Routes and Filters yourself in the Global.asax. I made a helper method that gets it done to one additional line, but still, it's ever so slightly lame.

Now, to THIS post that exists for two reasons: First, to show you how to create an update to a package, and what the update process looks like for the consumer. Second, to show you how (and why) you should put a tiny bit more effort in your packages up front so that things "just work" for the user.

CREATING AN UPDATE TO A NUGET PACKAGE

Step 1 - Update the NuSpec file

I opened up my AddMvc3ToWebForms.nuspec file and changed the version a notch to 0.6. I also added a dependency to another NuGet package called WebActivator that I'm going to use to make my package just work and avoid the need for that extra line of code. I'm being specific and asking for WebActivator 1.1 or higher because that version has a specific feature I need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <id>AddMvc3ToWebForms</id>
    <version>0.6</version>
    <authors>Scott Hanselman</authors>
    <owners>Scott Hanselman</owners>
    <iconUrl>http://www.hanselman.com/images/nugeticon.png</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>A totally unsupported way to quickly add ASP.NET MVC 3 support to your WebForms Application. Works on my machine.</description>
    <tags>MVC MVC3 ASP.NET WebForms</tags>
     <dependencies>
      <dependency id="WebActivator" version="1.1" />
    </dependencies>
  </metadata>
</package>

Step 2 - Make your new changes

When bringing in multiple NuGet packages it's common to want to have a few things run at application startup. Perhaps setting some context, connection strings, defaults for software factories, or dependency resolvers. There are a number of packages that need this functionality, and David Ebbo created the WebActivator package to make it easier. It's a formalized wrapper around a new ASP.NET 4 attributed called "PreApplicationStartMethod," and David also enables a PostApplicationStartMethod.

In my new version of the AddMvc3ToWebForms package I created a new file called AppStart_RegisterRoutesAreasFilters.cs.pp. Note the .pp extension that signals to NuGet to preprocess the file and replace tokens like $rootnamespace$, kind of like a mini-mini-code-generator.

The rest should look familiar; we're registering the Areas, GlobalFilters and Routes. The informal naming convention is to prefix the class and file with AppStart_ so that projects that include packages that add an AppStart_*.* will group the files together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Web.Infrastructure;
 
[assembly: WebActivator.PostApplicationStartMethod(typeof($rootnamespace$.AppStart_RegisterRoutesAreasFilters), "Start")]
 
namespace $rootnamespace$ {
    public static class AppStart_RegisterRoutesAreasFilters {
        public static void Start() {
            // Set everything up with you having to do any work.
            // I'm doing this because it means that
            // your app will just run. You might want to get rid of this
            // and integrate with your own Global.asax.
            // It's up to you.
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
 
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
 
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }
    }
}

Note the line:

[assembly: WebActivator.PostApplicationStartMethod(typeof($rootnamespace$.AppStart_RegisterRoutesAreasFilters), "Start")]

David added the "PostApplicationStart" option because PreApplicationStart happens too early in the process to register Areas, so for my package, I want things to run just after App_Start. This is all new and under development, so feel free to share with David if you have any thoughts, improvements or strong opinions.

Step 3 - Build a new NuPkg file

From the command line, call...

NuGet pack

...and you'll automatically get a new file, like this screenshot:

Step 4 - Publish the Update to http://nuget.org (or any NuGet server or file share)

I could log in to my account and upload the file from the browser interface, but since I'm thinking I'll being making changes here and there, I figure it'd be nice to publish from the command line.

I'll grab my API key from the site...

..and use it to publish from the command line (using my magic new publish.bat) with contents like these:

nuget push AddMvc3ToWebForms.0.6.nupkg e5c2bbe6-xxxx

...and the result...

C:\AddMvc3ToWebForms>nuget push AddMvc3ToWebForms.0.6.nupkg e5c2bbe6-xxxx
Creating an entry for your package [ID:AddMvc3ToWebForms Ver:0.6]...
Your package was uploaded to the server but not published.
Publishing your package [ID:AddMvc3ToWebForms Ver:0.6] to the live feed...
Your package was published to the feed.

Now I'm set. I've got version 0.6 live now. What's the experience for the user of this library?

GETTING AN UPDATE TO A NUGET PACKAGE

There's two ways to find out what NuGet packages my project is using and update them.

Package Manager Console

From the package manager console I can type Get-Package...

PM> Get-Package

Id                             Version              Description                                                                                                                    
-- ------- -----------
AddMvc3ToWebForms 0.5 A totally unsupported way to quickly add ASP.NET MVC 3 support to your WebForms Application. Works on my machine.

Looks like I have version 0.5. I can update it via...

PM> Update-Package AddMvc3ToWebForms
'WebActivator (≥ 1.1)' not installed. Attempting to retrieve dependency from source...
Done.
Successfully installed 'WebActivator 1.1.0.0'.
Successfully installed 'AddMvc3ToWebForms 0.6'.
Successfully removed 'AddMvc3ToWebForms 0.5' from WebApplication7.
Successfully uninstalled 'AddMvc3ToWebForms 0.5'.
Successfully added 'WebActivator 1.1.0.0' to WebApplication7.
Successfully added 'AddMvc3ToWebForms 0.6' to WebApplication7.

Notice that NuGet automatically removed AddMvc3ToWebForms 0.5 and installed AddMvc3ToWebForms 0.6. It also automatically brought in the dependency on WebActivator 1.1.

Add Library Reference

Alternatively, from Visual Studio right click on the References node in the Solution Explorer and select Add Library Reference (or select the same directly from the Tools menu).

Select Updates from the left side. A list of updates will appear. Click Update.

It's all good. Now my AddMvc3ToWebForms NuGet Package can add ASP.NET MVC functionality to an ASP.NET WebForms projects with no additional lines of code. This makes for a nice out of the box experience, especially if I bring in other projects that use the same functionality.

from:https://www.hanselman.com/blog/UpdatingAndPublishingANuGetPackagePlusMakingNuGetPackagesSmarterAndAvoidingSourceEditsWithWebActivator.aspx

Updating and Publishing a NuGet Package - Plus making NuGet packages smarter and avoiding source edits with WebActivator的更多相关文章

  1. 使用 dotnet CLI 来打包和发布 .NET Core nuget package

    原文链接:使用 dotnet CLI 来打包和发布 .NET Core nuget package 如何使用 visual studio 2015/2017 打包和发布 Nuget package, ...

  2. [转]Bootstrap 3.0.0 with ASP.NET Web Forms – Step by Step – Without NuGet Package

    本文转自:http://www.mytecbits.com/microsoft/dot-net/bootstrap-3-0-0-with-asp-net-web-forms In my earlier ...

  3. 使用NuGet Package Project快速制作NuGet包

    今天在visual studio gallery发现了一个插件NuGet Package Project,通过它可以在Visual Studio中建立Nuget Package工程,直接生成Nuget ...

  4. 用 NuGet Package Explorer 管理你的攻城武器

    缘由:每次新建一个工程,总是要从自己的“弹药库”或者之前的工程里面手动引用一些类库和脚本插件,难免有些繁琐和遗漏.想起经常用到的NuGet,跑到NuGet主页一看,发现有 NuGet Package ...

  5. Visual Studio 2010 更新NuGet Package Manager出错解决办法

    在Visual Studio 2010的扩展管理器中发现NuGet Package Manger有最新版本更新提示,选择更新安装提示以下错误信息: 2013/4/25 1:11:48 - Micros ...

  6. Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. T

    错误提示: Severity Code Description Project File Line Suppression StateError This project references NuG ...

  7. 搭建本地Nuget服务器并使用NuGet Package Explorer工具打包nuget包

    1.什么是Nuget: 百度百科描述: Nuget是 ASP .NET Gallery 的一员.NuGet 是免费.开源的包管理开发工具,专注于在 .NET 应用开发过程中,简单地合并第三方的组件库. ...

  8. This project references NuGet package(s) that are missing on this computer.

    Install Nuget. Right click on the solution and select "Enable NuGet Package Restore". Clic ...

  9. vs2017中生成.Net Standard Libarary的Nuget Package

    场景: Project A 对Project B存在 project to project reference.这种场景下必须为两者都生成nuget package.这样在load Project A ...

随机推荐

  1. Vue小问题汇总

    1.element-UI等组件更改默认样式: >>> https://vue-loader-v14.vuejs.org/zh-cn/features/scoped-css.html ...

  2. Failed to create Accelerated Display. Please check the display hardware and drivers meet the minimum requirements.

    ArcGIS Runtime for WPF开发中Map设置了属性UseAcceleratedDisplay="True",报错: Sample: LocalMap Error: ...

  3. hbase0.94.11版本和hbase1.4.9版本的benchamark区别

    1.起初使用ycsb对hbase进行benchmark,分别在100%写的情况下检测写性能:在100%读的情况下检测读的性能.实验数据如下: 2.新版本的habse写性能竟然不如老版本.!!!.于是我 ...

  4. 清除DNS缓存(解决能上QQ但是无法上网页问题)

    ipconfig/displaydnsipconfig/flushdns

  5. python josn包

    Python josn包中的编码与解码方法 对于Python数据类型进行编码解码 json.dumps  对python的数据类型进行json格式编码 :(将dict转为json格式) eg: imp ...

  6. Codeforces 854C Planning 【贪心】

    <题目链接> 题目大意: 表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟 ...

  7. poj1041 【无向图欧拉回路】 按最小升序输出

    题目链接:http://poj.org/problem?id=1041 题目大意: 题目大意:一个城镇有n个二叉路口,这些路口由m条街道连接,某人想要从某个路口出发,经过所有的街道且每条街道只走一次, ...

  8. UVA 1592 DataBase

    思路: 知识补充: ①make_pair和pair: /*pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用 ...

  9. SpringMVC框架简介

    1.简介 SpringMVC也叫Spring Web  mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 01.Spring mvc的优缺点 M ...

  10. [SPOJ-CIRU]The area of the union of circles/[BZOJ2178]圆的面积并

    [SPOJ-CIRU]The area of the union of circles/[BZOJ2178]圆的面积并 题目大意: 求\(n(n\le1000)\)个圆的面积并. 思路: 对于一个\( ...