Debug. NET Core 2.0 Preview with VS Code (traditional three-tier architecture) and corepreview

Source: Internet
Author: User

Debug. NET Core 2.0 Preview with VS Code (traditional three-tier architecture) and corepreview

Preparations

VS Code: https://vscode.cdn.azure.cn/stable/379d2efb5539b09112c793d3d9a413017d736f89/VSCodeSetup-ia32-1.13.1.exe

. NET Core 2.0 Preview: https://download.microsoft.com/download/6/1/ B /61B3E81F-5509-48D2-BB4F-5189E23CD29A/dotnet-sdk-2.0.0-preview2-006497-win-x64.exe

. NET Core Preview 2.0 after installation is complete, on the console, enter the Command "dotnet-v", will prompt version:. NET Command Line Tools (2.0.0-preview2-006497)

After VS Code is installed and started, click the "extension" menu on the left side of the screen, or press "Ctrl + Shift + X" to enter the extended installation interface, and enter "OmniSharp" in the search box ", click Install extension to restart VS Code.

  

Create a folder anywhere on your computer to store solution and project information. I chose to create "Poplar. Demo" on drive F ". After the folder is created, click "File> Open folder" on the VS Code menu bar and select the new folder. After the folder is selected, press Ctrl + 'to call out the terminal (you can also click View> integrated terminal on the menu bar "), at this time, VS Code will prompt whether to configure the default terminal. If you do not have this requirement, you can ignore it.

After the terminal is opened, the path is the default current workspace path.

  

Enter "dotnet new sln-n Poplar. Demo" in the terminal to create a solution file. The first "dotnet" of this command is called. NET Core CLI, the second section of the new parameter indicates that you want to create, the third section of the sln indicates that the new type is a solution file, the fourth section-n indicates that you want to specify the name, the fifth section of the Poplar. demo indicates the custom name. At this point, the solution file has been created. At this time, click "Resource Manager" in the left-side menu to find an additional. sln file in the folder.

  

Enter "dotnet new mvc-n Poplar. demo. portal "creates an MVC project. The third section of this command indicates that the newly created type is an MVC project. other meanings are the same as those of the previous command. After the command is executed, some newly added files are displayed in the resource manager.

Enter "dotnet new classlib-n Poplar. Demo. Services" in the terminal to create the business layer. The third section of this command indicates that the new type is a class library. The other meanings are the same as those of the previous command.

Enter "dotnet new classlib-n Poplar. Demo. DataAccess" and "" to create the data access layer and Models.

After the required project is created, you need to add references to other projects. Here, the UI Layer is used as an example. In the resource manager, open the previously created UI Layer "Poplar. Demo. Portal" and click "Poplar. Demo. Portal. csproj". The project file content should be similar to the following:

<Project Sdk="Microsoft.NET.Sdk.Web">  <PropertyGroup>    <TargetFramework>netcoreapp2.0</TargetFramework>    <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>    <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>    <UserSecretsId>aspnet-Poplar.Demo.Portal-D34843E8-3D14-4277-8CED-4D3BCACBF88C</UserSecretsId>  </PropertyGroup>  <ItemGroup>    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />  </ItemGroup>  <ItemGroup>    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0-preview2-final" />  </ItemGroup></Project>
Poplar. Demo. Portal. csproj

Create an ItemGroup in the project file to store references to other projects. <ProjectReference Include = ".. \ Poplar. demo. services \ Poplar. demo. services. csproj "/> indicates the project reference, and the Include value indicates the path of the referenced project. After the UI Layer is configured, the content of the entire project file is similar to the following.

<Project Sdk="Microsoft.NET.Sdk.Web">  <PropertyGroup>    <TargetFramework>netcoreapp2.0</TargetFramework>    <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>    <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>    <UserSecretsId>aspnet-Poplar.Demo.Portal-D34843E8-3D14-4277-8CED-4D3BCACBF88C</UserSecretsId>  </PropertyGroup>  <ItemGroup>    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />  </ItemGroup>  <ItemGroup>    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0-preview2-final" />  </ItemGroup>  <ItemGroup>    <ProjectReference Include="..\Poplar.Demo.Models\Poplar.Demo.Models.csproj" />    <ProjectReference Include="..\Poplar.Demo.Services\Poplar.Demo.Services.csproj" />  </ItemGroup></Project>
Poplar. Demo. Portal. csproj (after adding the service layer and Models references)

An ItemGroup and two references are added at the bottom. Configure the reference required by other projects in the same way.

Next, add the corresponding class to the corresponding layer. I have not found any good method here. All these are manually created cs files one by one, then, manually write keywords such as using, namespace, and class. Then, call the API at the UI Layer.

Service layer added classes:

using System;using Poplar.Demo.DataAccess;namespace Poplar.Demo.Services{    public class DemoService    {        public string Do()        {            return "DemoService.Do()" + " ——> " + new DemoDal().Do();        }    }}
Poplar. Demo. Services. DemoService

Newly Added classes at the DataAccess layer:

using System;using Poplar.Demo.DataAccess;namespace Poplar.Demo.DataAccess{    public class DemoDal    {        public string Do()        {            return "DemoDal.Do()";        }    }}
Poplar. Demo. DataAccess. DemoDal

Finally, call it at the UI Layer. I chose the HomeController that will be available by default when creating the MVC project above:

using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;using Poplar.Demo.Portal.Models;using Poplar.Demo.Services;namespace Poplar.Demo.Portal.Controllers{    public class HomeController : Controller    {        public IActionResult Index()        {            return View();        }        public IActionResult About()        {            ViewData["Message"] = new DemoService().Do();            return View();        }        public IActionResult Contact()        {            ViewData["Message"] = "Your contact page.";            return View();        }        public IActionResult Error()        {            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });        }    }}
Poplar. Demo. Portal. Controllers. HomeController

After all the sample code is written, When you directly press the "F5" button to select debugging, a window pops up prompting you to select the environment, and then select ". NET Core ". The "launch. json" file is opened. By default, three configurations are available under the deployments node of the launch. json file. In this case, we need to use the second one, that is

"Name": ". NET Core Launch (web)". Remember this name and click "debug" on the left-side menu bar"

After selecting the. NET Core Launch (web) item, press F5 to start debugging. The system will prompt that the preLaunchTask "build" cannot be found ". This means to execute a build called task before starting the debugging. Under the deployments node of launch. json,

Name is. NET Core Launch (web) is the internal preLaunchTask node of this object. The value is build. This configuration node needs to execute a compilation task before debugging, otherwise, the modified Code after the last compilation will not be executed. Click "Configure task running program" in the prompt bar, select ". NET Core" in the pop-up box, and add a task

After the task is added, You need to configure launch. json so that the corresponding. net program can be found during debugging. Configure first. the "cwd" node of the NET Core Launch (web) item. This node indicates the path for executing the dotnet command and configures it as "$ {workspaceRoot}/Poplar. demo. portal, and then configure.. NET Core Launch (web) item program node, so that the corresponding. net Program, configured as "bin/Debug/netcoreapp2.0/Poplar. demo. portal. dll ", launch. after the json configuration is complete, you need to configure the task and specify the object of the corresponding task during build. Add an item to the corresponding task.

            "options":{                "cwd": "${workspaceRoot}/Poplar.Demo.Portal"            }
Options

The $ {workspaceRoot} mentioned above indicates the root directory of the current workspace, that is, the path of the folder that VScode opened at the beginning. The launch. json and tasks. json after configuration are similar to the following.

{    "version": "0.2.0",    "configurations": [        {            "name": ".NET Core Launch (console)",            "type": "coreclr",            "request": "launch",            "preLaunchTask": "build",            "program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",            "args": [],            "cwd": "${workspaceRoot}",            "stopAtEntry": false,            "console": "internalConsole"        },        {            "name": ".NET Core Launch (web)",            "type": "coreclr",            "request": "launch",            "preLaunchTask": "build",            "program": "bin/Debug/netcoreapp2.0/Poplar.Demo.Portal.dll",            "args": [],            "cwd": "${workspaceRoot}/Poplar.Demo.Portal",            "stopAtEntry": false,            "launchBrowser": {                "enabled": true,                "args": "${auto-detect-url}",                "windows": {                    "command": "cmd.exe",                    "args": "/C start ${auto-detect-url}"                },                "osx": {                    "command": "open"                },                "linux": {                    "command": "xdg-open"                }            },            "env": {                "ASPNETCORE_ENVIRONMENT": "Development"            },            "sourceFileMap": {                "/Views": "${workspaceRoot}/Views"            }        },        {            "name": ".NET Core Attach",            "type": "coreclr",            "request": "attach",            "processId": "${command:pickProcess}"        }    ]}
Launch. json
{    // See https://go.microsoft.com/fwlink/?LinkId=733558    // for the documentation about the tasks.json format    "version": "0.1.0",    "command": "dotnet",    "isShellCommand": true,    "args": [],    "tasks": [        {            "taskName": "build",            "args": [ ],            "options":{                "cwd": "${workspaceRoot}/Poplar.Demo.Portal"            },            "isBuildCommand": true,            "showOutput": "silent",            "problemMatcher": "$msCompile"        }    ]}
Tasks. json

After the configuration is complete, press F5 to start debugging.

Sample Code: Poplar.Demo.rar

 

In the process of using VS Code, we found that smart prompts and Code formatting could not be used. Another problem was the creation of classes. Thanks for your understanding.

All the above content is my opinion. If you have any questions, please point out. Thank you.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.