nuget 程序包源

nuget 程序包源

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.

我几天前写了一篇文章,“通过7个简单的步骤创建NuGet包-加上使用NuGet将ASP.NET MVC 3集成到现有Web Forms应用程序中” 。 标题足够长? 我想是这样。

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.

该帖子的存在有两个原因:首先向人们展示创建NuGet包有多么容易(并提示您自己创建一个),然后向您分享一些实验,其中有人可以在File | New Project之后启用新的/有趣的功能。 。 在该示例中,我们有一个现有的ASP.NET WebForms应用程序,并在同一行中添加了ASP.NET MVC,使其成为一个简单的混合应用程序。

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.

好吧,不是真的一行。 在AddMvc3ToWebForms包的0.5版中,您仍然必须在Global.asax中自行连接Areas,Routes和Filters。 我创建了一个辅助方法,可以将其完成另一行,但是仍然有点la脚。

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.

现在,该帖子的存在有两个原因:首先,向您展示如何为程序包创建更新,以及使用方的更新过程如何。 其次,向您展示如何(以及为什么)应该在包装中多花点功夫,以便对用户“起作用”。

创建对NuGet软件包的更新 (Creating an Update to a NuGet Package)

Step 1 - Update the NuSpec file

步骤1-更新NuSpec文件

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.

我打开了AddMvc3ToWebForms.nuspec文件,并将版本更改为0.6。 我还向另一个名为WebActivator的NuGet包添加了一个依赖关系,我将使用该依赖关系使我的包正常工作,并避免需要额外的代码行。 我是特定的,并要求WebActivator 1.1或更高版本,因为该版本具有我需要的特定功能。

<?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

第2步-进行新更改

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.

引入多个NuGet软件包时,通常希望在应用程序启动时运行一些操作。 也许设置一些上下文,连接字符串,软件工厂的默认值或依赖项解析器。 有许多需要此功能的软件包, David Ebbo创建了WebActivator软件包以使其变得更容易。 它是一个新的ASP.NET 4的形式化包装,该新ASP.NET 4的属性为“ PreApplicationStartMethod ”,并且David还启用了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.

在新版本的AddMvc3ToWebForms包中,我创建了一个名为AppStart_RegisterRoutesAreasFilters.cs.pp的新文件。 请注意.pp扩展名,它通知NuGet对该文件进行预处理,并替换$ rootnamespace $之类的令牌,有点像微型迷你代码生成器。

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.

其余的应该看起来很熟悉。 我们正在注册区域,全局过滤器和路线。 非正式的命名约定是在类和文件前加上AppStart_前缀,以便包含添加AppStart _ *。*包的项目将文件分组在一起。

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")]

[程序集:WebActivator.PostApplicationStartMethod(typeof($ rootnamespace $ .AppStart_RegisterRoutesAreasFilters),“开始”)]

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.

David添加了“ PostApplicationStart”选项,因为PreApplicationStart发生在注册区域的过程中太早了,因此对于我的包,我希望事情在App_Start之后运行。 这些都是全新的并且正在开发中,因此,如果您有任何想法,改进或强烈的意见,请随时与David分享

Step 3 - Build a new NuPkg file

步骤3-建立新的NuPkg档案

From the command line, call...

在命令行中,调用...

NuGet pack

NuGet包

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

...您将自动获得一个新文件,如以下屏幕截图所示:

Building a new package - Look at all my packages in the explorer window. Cool.

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

步骤4-将更新发布到http://nuget.org (或任何NuGet服务器或文件共享)

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...

我将从网站上获取我的API密钥...

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

..并使用它从命令行发布(使用我的新魔术publish.bat),其内容如下:

nuget push AddMvc3ToWebForms.0.6.nupkg e5c2bbe6-xxxx

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?

现在我开始了。 我现在0.6版直播。 该库的用户有什么经验?

获取NuGet软件包的更新 (Getting an Update to a NuGet Package)

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

有两种方法可以找出我的项目正在使用的NuGet软件包并进行更新。

Package Manager Console

程序包管理器控制台

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

在包管理器控制台中,我可以输入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...

看起来我的版本是0.5。 我可以通过...进行更新

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.

请注意,NuGet会自动删除AddMvc3ToWebForms 0.5并安装AddMvc3ToWebForms 0.6。 它还会自动引入对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).

或者,在Visual Studio中,右键单击“解决方案资源管理器”中的“引用”节点,然后选择“添加库引用” (或直接从“工具”菜单中选择相同的引用)。

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.

都很好。 现在,我的AddMvc3ToWebForms NuGet包可以将ASP.NET MVC功能添加到ASP.NET WebForms项目中,而无需其他代码行。 这提供了很好的开箱即用体验,尤其是当我引入其他使用相同功能的项目时。

翻译自: https://www.hanselman.com/blog/updating-and-publishing-a-nuget-package-plus-making-nuget-packages-smarter-and-avoiding-source-edits-with-webactivator

nuget 程序包源

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐