{ "cells": [ { "cell_type": "markdown", "id": "065bfcac", "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "管理NuGet包\n", "===========" ] }, { "cell_type": "markdown", "id": "0baca368", "metadata": {}, "source": [ "## 浏览和查找NuGet包" ] }, { "cell_type": "markdown", "id": "18559f3e", "metadata": {}, "source": [ "VS Code 提供了查看和查找NuGet包的UI界面:打开一个`Polyglot Notebook`文件,打开一个\"终端\", 切换到 \"nuget\"项:打开一个UI管理窗口。如下图:\n", "\n", "![UI窗口](./assets/images/NugetUI.jpg)" ] }, { "cell_type": "markdown", "id": "c110c37f", "metadata": {}, "source": [ "## 导入 NuGet 包" ] }, { "cell_type": "markdown", "id": "7eecdf67", "metadata": {}, "source": [ "在`.NET Interactive`中,C#和F#核允许你使用`#r nuget`魔法命令将NuGet包导入到你的交互式会话中。两种语言的语法相同。" ] }, { "cell_type": "markdown", "id": "73546727", "metadata": {}, "source": [ "+ 导入最新版本的包,使用` #r nuget` 而无需指定版本号:" ] }, { "cell_type": "code", "execution_count": 1, "id": "ede3455f", "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#r \"nuget:Newtonsoft.Json\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{\"Name\":\"张三\",\"Age\":20},{\"Name\":\"李四\",\"Age\":69},{\"Name\":\"王五\",\"Age\":82}]\r\n" ] } ], "source": [ "//使用包:序列化一个匿名类集合\n", "\n", "//引入命名空间\n", "using Newtonsoft.Json;\n", "\n", "{ //花括号分隔作用域\n", " var persons = new[]\n", " {\n", " new {Name=\"张三\", Age = 20},\n", " new {Name=\"李四\", Age = 69},\n", " new {Name=\"王五\", Age = 82},\n", " }; \n", "\n", " var jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(persons);\n", " Console.WriteLine(jsonText);\n", "}\n" ] }, { "cell_type": "markdown", "id": "da4b55e2", "metadata": {}, "source": [ "+ 导入指定版本的包" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#r \"nuget:Newtonsoft.Json,13.0.3\"" ] }, { "cell_type": "markdown", "id": "07da3486", "metadata": {}, "source": [ "+ 导入预览版本的包" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Restore sources
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#r \"nuget:System.CommandLine,*-*\"" ] }, { "cell_type": "markdown", "id": "fe5a81be", "metadata": {}, "source": [ "## 设置包源" ] }, { "cell_type": "markdown", "id": "c8ce789d", "metadata": {}, "source": [ "如果包不是Nuget官方托管包,或者想明确包的来源,使用`#i`命令指定一个nuget包源" ] }, { "cell_type": "markdown", "id": "b2e11743", "metadata": {}, "source": [ "+ 默认包源\n", " 可以不用设置,默认使用的官方托管包" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Restore sources
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "//默认包源:可以不设置\n", "#i \"nuget:https://api.nuget.org/v3/index.json\"" ] }, { "cell_type": "markdown", "id": "39096d0b", "metadata": {}, "source": [ "+ 远程包源\n", "\n", " 组织通常会将包存储在私人或预发布源中" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Restore sources
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "//添加dotnet项目的预发布nuget源\n", "#i \"nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json\"" ] }, { "cell_type": "markdown", "id": "134c7d97", "metadata": {}, "source": [ "+ 本地包源\n", "\n", " 可以将一个本地文件夹作为nuget源" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "//\"C:\\Users\\用户账号\\.nuget\\packages\" 是 Windows 系统中 visual studio 默认的本机 nuget 包存储位置,可以当本地包源(注意路径用户账号换成自己的)\n", "//当然可使用任意文件夹,里面要有nuget包\n", "\n", "#i \"nuget:C:\\Users\\ruyu\\.nuget\\packages\"\n", "#r \"nuget:MyOrg.MyPackage\"" ] }, { "cell_type": "markdown", "id": "54d60e85", "metadata": {}, "source": [ "+ 需要认证的包源\n", "\n", " 一些企业搭建有自己的nuget私有包服务器(MyGet等),使用时需要认证。\n", "\n", " 但是:.NET Interactive 不直接支持访问需要身份验证的包源,但你可以通过在用户级别的 nuget.config 文件中放入包源的 PAT 来访问经过身份验证的源。你可以在这里了解更多关于这种方法的信息" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "languageName": "csharp", "name": "csharp" } ] } } }, "nbformat": 4, "nbformat_minor": 5 }