[译]使用NuGet管理共享代码
可以在内网部署自己的私人NuGet仓储服务。
Setting it up
本例中我们创建一个发邮件的类,将其作为我们自己的NuGet包:
using System;
using System.Net.Mail;
namespace MyCompany.WebCommon
{
/// <summary>
/// Contains code to send a HTML email using SMTP
/// </summary>
public class Emailer
{
/// <summary>
/// Contains the error message if SendMail fails
/// </summary>
public string Error { get; private set; }
/// <summary>
/// Sends an email, returns false if failed
/// </summary>
/// <param name="emailAddress">Email address to send to - only one address allowed</param>
/// <param name="subject">Subject line of email</param>
/// <param name="emailBodyHtml">Contents of email - make sure to escape any HTML</param>
/// <param name="emailFrom">Email address the email comes from (can be anything)</param>
/// <param name="smtpServerName">Smtp server name</param>
/// <returns>boolean indicating email was sent (not neccesarily delivered)</returns>
public bool SendEmail(
string emailAddress,
string subject,
string emailBodyHtml,
string emailFrom,
string smtpServerName = "localhost")
{
SmtpClient Smtp_Server = new SmtpClient();
MailMessage e_mail = new MailMessage();
Smtp_Server.Host = smtpServerName;
e_mail = new MailMessage();
e_mail.From = new MailAddress(emailFrom);
e_mail.To.Add(emailAddress);
e_mail.Subject = subject;
e_mail.IsBodyHtml = true;
e_mail.Body = emailBodyHtml;
try
{
Smtp_Server.Send(e_mail);
}
catch (Exception e)
{
this.Error = e.ToString();
return false;
}
return true;
}
}
}
假设我们有五个项目都要有发邮件的功能,我们希望这五个项目都能复用上面的类。
首先,创建一个解决方案,创建如上的类,编译项目:

这里有两处要注意。首先编辑Properties/AssemblyInfo.cs文件为你的程序集添加描述。同时可以设置公司名,作者等。

第二个要注意的是确保启用了xml comment文件生成,这样才有智能提示。注意修改XML扩展名为小写。

在控制台中cd到.csproj文件的所在目录,然后执行下面的命令:
nuget spec

这会生成一个名为MyCompany.WebCommon.nuspec的文件,内容如下:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<projectUrl>http://myserver/wiki/MyCompany.WebCommon.html</projectUrl>
<releaseNotes>Initial release of code library to send emails</releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>MyCompany Web Common Emailer Emails</tags>
</metadata>
</package>
这个文件被NuGet用来创建你的package。
注意上面的$XXX$,NuGet会自动查看AssemblyInfo.cs文件使用对应的meta data替代掉nuspec文件中的$XXX$。
然后编译项目,执行下面的命令:
NuGet Pack

这将创建一个名为MyCompany.WebCommon.1.0.0.0.nupkg的文件 - 这个就是你的第一个package。
设置你的私有NuGet server
Tools -> options -> NuGet Package Manager -> Package Sources

注意添加/nuget到你的私有仓储url。
And you can now add the package to your project! Right click on your project, select Manage NuGet packages and you should see:
更新的你包
修改下SendEmail() :

修改**AssemblyInfo.cs **文件中的AssemblyVersion和AssemblyFileVersion为1.1,重编译项目。然后使用nuget pack重新构建package。

新package的版本为1.1。这意味将它发布到你的私人仓储中去不会覆盖1.0的版本。The new package has the 1.1 version number at the end. This means we can publish it to our repository and not lose our earlier version. It also means that once published, it's easy to update your code to use the new package. Simply right click on your project and go to Manage NuGet packages, and then click on the "Updates" section. From here you can see what packages have been updated along with it's release notes:
[译]使用NuGet管理共享代码的更多相关文章
- 完美解决--用VS中的Git做代码管理器,与他人共享代码
1.创建代码仓库,这里说一下为什么要创建仓库,Git不能够作为源代码管理器,vs中自带的也只能够在本地进行管理,要和他们共享的话必须要有服务器端去存储代码,类似于SVN,它就有客户端和服务器端,这里推 ...
- [资源]--完美解决--用VS中的Git做代码管理器,与他人共享代码
1.创建代码仓库,这里说一下为什么要创建仓库,Git不能够作为源代码管理器,vs中自带的也只能够在本地进行管理,要和他们共享的话必须要有服务器端去存储代码,类似于SVN,它就有客户端和服务器端,这里推 ...
- 【译】在 ASP.NET 和 ASP.NET Core 之间共享代码
原文 | Ken 翻译 | 郑子铭 随着 .NET 6 的发布,使用 ASP.NET Core 可以获得更多好处.但是将现有代码迁移到 ASP.NET Core 通常听起来像是一项巨大的投资.今天我们 ...
- Nuget 命令 NuGet 管理项目库
因为可视化库程序包管理器的局限性,有很多需要的功能在界面中无法完成. 以下技巧均需要在"程序包管理器控制台"中使用命令来完成. 一.改变项目目标框架后,更新程序包 当改变项目的目标 ...
- 使用NuGet管理项目类库引用
NuGet 是微软开发平台(包括.NET平台)的一个包管理器,这里只介绍和.NET相关的NuGet Visual Studio扩展客户端, 在VS2010 ,VS2012 ,VS2013中默认集成了N ...
- 【转】使用 NuGet 管理项目库-Phil Haack
原文地址:https://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 ...
- 使用 NuGet 管理项目库
使用 NuGet 管理项目库 Phil Haack 本文转载自:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Micros ...
- NuGet管理
使用NuGet管理项目类库引用 NuGet 是微软开发平台(包括.NET平台)的一个包管理器,这里只介绍和.NET相关的NuGet Visual Studio扩展客户端, 在VS2010 ,VS2 ...
- [转载]使用 NuGet 管理项目库
原文:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 Mi ...
随机推荐
- 2018-2019 ACM-ICPC Pacific Northwest Regional Contest C Contest Setting(DP)
比赛链接:Contest Setting C题 题意:$n$道题目,每道题目难度为$ai$,选择$k$道难度不同的题目,有多少种选择方案.$1<=k<=n<=1000,1<=a ...
- POJ 1845 Sumdiv(逆元)
题目链接:Sumdiv 题意:给定两个自然数A,B,定义S为A^B所有的自然因子的和,求出S mod 9901的值. 题解:了解下以下知识点 1.整数的唯一分解定理 任意正整数都有且只有唯一的方式 ...
- centos7/rhel7安装较高版本ruby2.2/2.3/2.4+
环境需求: 在Centos7.3中,通过yum安装ruby的版本是2.0.0,但是如果有些应用需要高版本的ruby环境,比如2.2,2.3,2.4...那就有点麻烦了,譬如:我准备使用redis官方给 ...
- Android 架构 -- Room
gradle依赖: // add for room implementation "android.arch.persistence.room:runtime:1.1.1" // ...
- 解决SecureCRT无法用非root账号登录ssh
原文: http://blog.csdn.net/zxx2403/article/details/46959047 链接失败,提示这个: --------------------------- Sec ...
- 点击a标签不跳转的办法
方法1: <a href="http://www.baidu.com" onclick="return false"></a> 方法2: ...
- 如何查看本地电脑ip
1.快捷键 win+R打开命令窗口 输入 ipconfig查看你电脑的ip 2.输入netstat -an ,查看当前所有连接端口,显示所有的有效连接信息列表,包括已建立的连接(ESTABLISHED ...
- ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
Ubuntu下,运行django项目的时候失败,报错: (env36) root@JD:~/xueyiwang# python manage.py runserver 0.0.0.0:8000 Per ...
- 第一篇:安装Android Studio问题及其解决方案
ubuntu18.04配置android studio3.2.1环境 1.JDK安装与配置:https://www.cnblogs.com/yuanbo123/p/5819564.html(按照文档操 ...
- ElasticSearch6.3.2------入门
先去官网下载,方便测试用的Windows版本的 都解压了 --- 启动ElasticSearch和Kibana [E:\elasticsearch-]$ .\bin\elasticsearch.bat ...