好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

VS2017-实验手册-中文.pdf

15页
  • 卖家[上传人]:简****9
  • 文档编号:113301029
  • 上传时间:2019-11-08
  • 文档格式:PDF
  • 文档大小:2.08MB
  • / 15 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 实验手册 目标 该导师引导的实验,可以使开发者理解与探索 Visual Studio 2017 的新特性,使用新的扩展,例如 C# 7.0、Azure App Service 等开展一些动手实验,以学习 Visual Studio 2017 的新特性 准备 在开始实验之前,您必须在您的开发机上已经安装了企业版 Visual Studio 2017,另外还需要一个 Azure 的账号 实验概览 实验内容 该实验包含五个部分: 1) C# 7.0 本地方法 2) C# 7.0 模式匹配 3) 实时单元测试 4) 创建 Azure App Service Linux 5) 为 Visual Studio 2017 Web 应用配置 Application Insight 该实验使用运行于 Windows 10 技术上的虚拟机登录虚拟机,请按 CTRL+ALT+END,然后输入您的登录凭据 Virtual Machine Role Win10-Base Windows 10 Client 在该实验中,用户 Admin 的口令是 Passw0rd! 实验一 C# 7.0 特性与 IDE 扩展 一、本地方法或内嵌方法: 定义在方法内的方法称为本地方法。

      • 打开 Visual Studio 2017,点击文件 - 新建项目 - Console App (.Net Framework) • 项目名称 “UseLocalFunctionsUseLocalFunctions” • 运行如下代码,理解本地方法是如何工作. using static System.Console; namespace UseLocalFunctions { // Exercise 1 class Program { static void Main(string[] args) { void Add(int x, int y) { WriteLine($“Sum of {x} and {y} is : {x + y}“); } void Multiply(int x, int y) { WriteLine($“Multiply of {x} and {y} is : {x * y}“); Add(30, 10); } Add(10, 30); Multiply(40, 30); ReadLine(); } } } 在上边的实验中,我们定义量两个内嵌方法: “Add” 和 “Multiply” 。

      您可以看到,我可以在定义它们的父方法中任意 调用,甚至可以在一个内联方法 Multiply 中调用另一内嵌的 Add 方法 1.1. 带有返回类型的本地方法 在第一个实验中,我们看到的本地方法返回 void,但是并没有对返回类型的限制您可以返回任意类型在这个实 验中,我们创建一个名为 TotalMarks 的本地函数,它将计算总分,并返回 decimal 类型 class Program { static void Main(string[] args) { PrintStudentMarks( 101, new Subject { SubjectName = “Math“, Marks = 96 }, new Subject { SubjectName = “physics“, Marks = 88 }, new Subject { SubjectName = “Chem“, Marks = 91 } ); ReadLine(); } public static void PrintStudentMarks(int studentId, params Subject[] subjects) { WriteLine($“Student Id {studentId} Total Marks: {CalculateMarks()}“); WriteLine($“Subject wise marks“); foreach (var subject in subjects) { WriteLine($“Subject Name: {subject.SubjectName} \t Marks: {subject.Marks}“); } decimal CalculateMarks() { decimal totalMarks = 0; foreach (var subject in subjects) { totalMarks += subject.Marks; } return totalMarks; } } } public class Subject { public string SubjectName { get; set; } public decimal Marks { get; set; } } 1.2 本地方法 vs 递归方法 本地方法不需要维护调用堆栈,而递归方法需要。

      通过执行下面的实验,来理解它们的性能差异 • 将上面实验的 Program 类注释掉,使用下面的代码 class Program { static void Main(string[] args) { //BigInteger factorial = GetFactorial(9000); BigInteger factorial = GetFactorialUsingLocal(9000); } private static BigInteger GetFactorialUsingLocal(int number) { if (number 1) { Multiply(number - 1); number--; } void Multiply(int x) = result *= x; return result; } private static BigInteger GetFactorial(int number) { if (number 项目 - Visual C# - Class Library (.NET Framework) • 命名项目为:LiveUnitTestingDemo,并确定 使用以下代码替换生成的代码。

      public static class MyMath { public static int Add(int value1, int value2) { return value1 + value2; } public static int Sub(int value1, int value2) { return value1 - value2; } } 2. 创建测试项目 在待测试的类名上,在右键弹出的菜单中,选择 Create Unit Tests ,可以直接创建相应的测试项目 在弹出的创建测试项目对话框中,完善测试项目信息 测试框架支持: • xU • NUnit • MSTest 默认的测试项目名称是原测试项目加上 Tests 后缀测试类的名称是在被测试类名之后也加上 Tests 后缀 此处,可以直接点击 Ok 完成 默认会生成基础的测试代码 将测试项目中的 MyMathTests 类修改为如下代码 using Microsoft.VisualStudio.TestTools.UnitTesting; namespace LiveUnitTestingDemo.Tests { [TestClass()] public class MyMathTests { [TestMethod()] public void AddTest() { var result = MyMath.Add(2, 3); Assert.IsTrue(5 == result); } [TestMethod()] public void SubTest() { var result = MyMath.Sub(3, 2); Assert.IsTrue(1 == result); } } } 此时,已经可以进行通常的单元测试了。

      3. 启动实时单元测试 现在,我们使用实时单元测试 点击菜单中的 Test - Live Unit Testing - Start, 启动实时单元测试 4. 使用实时单元测试 启动实时单元测试之后,在源码文件窗口中,可以直接看到代码已经通过了测试可以清楚地看到哪些行通过了测 试 实验二 使用 Azure 开发云应用 一、创建 Azure App Service Linux • 打开 Visual Studio 2017 ,然后点击 File - New Project - Web. • 选择 ASP.NET Core Web Application (.NET Core) • 选择 Web Application 并启用 Docker Support 然后点击 Ok. • 通过下列链接了解 Docker cases • 现在,可以到项目中的 Views 文件夹上,右键 Views 文件夹,并选择创建新文件夹,命名为 DemoPage • 在 DemoPage 文件夹右键,点击添加新项目,选择 MVC Viewpage,点击添加按钮 • 点击添加之后,一个新的视图文件出现在项目中。

      • 现在转到 Shared 文件夹 - _Layout.cshtml,在下列代码中添加新列表项 • 构建项目,在成功构建之后,在项目文件文件夹上右键,选择 Publish. • 点击 Publish 之后,您会看到如下屏幕该窗口允许您的应用发布到 Azure 的 Linux 环境 • 点击发布之后,你会看到如下窗口 • 通过点击 Container Registy 右边的 New… 按钮,可以配置 Container ,您可以看到如下 窗口 • All the azure location does not allow to create Container registry so need to select azure location carefully else it will fail in creating Azure App Service. • After creating and configuring container registry you can click on create and this create will take few minutes for creating a New App service on Azure where you can host your app for Linux. • You can login into your azure portal and can verify the App Service created on your portal where you can host your web and other Apps. • Please follow the below link for configuring your web app in Azure App service. 二、在 VS2017 中集成 Application Insight • 在资源管理器的项目内部,可以看到名为 Connected Services 项目,下图中高亮如下。

      双击图标之后,在左边的窗口中可以看到如下窗口 • Connected Services 窗口将在左边打开,如下所示 • 点击 Monitoring with Application Insights,将带您到 Application Insight 配置窗口,如下 所示: • 点击 Start Free 按钮,为您的应用注册 Application insights. 选择第二个选项,点击注册,将。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.