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

Source: Internet
Author: User
Tags dotnet net command net command line custom name

Preparatory work

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

After the. NET Core Preview 2.0 installation is complete, on the console, enter the command "Dotnet-v", which prompts the version:. NET command line Tools (2.0.0-preview2-006497)

After the VS code is installed and started, click on the "Extensions" menu on the left side of the screen, or press "Ctrl + Shift + X" to go to the Extended installation screen, enter "Omnisharp" in the Search box, click Install extension, restart vs code.

  

Create a new folder anywhere on your computer to store solution and project information. I chose to create a new "Poplar.demo" on the F-drive. After the new folder has been created, on the VS Code menu bar, click File-open folder and select the new folder. After the folder selection is complete, press "Ctrl +" to exit the terminal (you can also click on the menu bar "view-plus Integration terminal"), this time vs code will prompt whether to configure the default terminal, without this requirement can be ignored.

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 new solution file. The first paragraph of this command, "Dotnet", is called the. NET Core CLI, the second segment of the new parameter indicates that you want to create a new, the third section of SLN indicates that the new type is a solution file, the fourth-n indicates the name to be specified, and the fifth paragraph Poplar.demo represents the custom name. Here, the solution file has been established. At this point, click "Explorer" on the left-hand menu, and you will find a more. sln file under the folder.

  

Continue to enter "dotnet new Mvc-n Poplar.Demo.Portal" in the terminal to create an MVC project, the third paragraph of this command indicates that the new type is an MVC project, the other meaning is consistent with the previous command, after the command execution is complete, You will see some additional files on the explorer.

Enter "dotnet new Classlib-n Poplar.Demo.Services" under terminal to create the business layer, the third paragraph of this command indicates that the new type is a class library, and the other meaning is consistent with the previous command.

Continue to enter "dotnet new Classlib-n Poplar.Demo.DataAccess", "" "Create data access layer and models.

Once the required project is created, you need to add a reference to the other project, which takes the UI layer as an example. In the Explorer, open the previously created UI layer "Poplar.Demo.Portal" and click "Poplar.Demo.Portal.csproj", at which point the project file contents should be similar to the following

<ProjectSDK= "Microsoft.NET.Sdk.Web">  <PropertyGroup>    <targetframework>netcoreapp2.0</targetframework>    <Mvcrazorcompileonpublish>True</Mvcrazorcompileonpublish>    <Assettargetfallback>$ (assettargetfallback);p ortable-net45+win8+wp8+wpa81;</Assettargetfallback>    <Usersecretsid>aspnet-poplar.demo.portal-d34843e8-3d14-4277-8ced-4d3bcacbf88c</Usersecretsid>  </PropertyGroup>  <ItemGroup>    <packagereferenceInclude= "Microsoft.AspNetCore.All"Version= "2.0.0-preview2-final" />  </ItemGroup>  <ItemGroup>    <dotnetclitoolreferenceInclude= "Microsoft.VisualStudio.Web.CodeGeneration.Tools"Version= "2.0.0-preview2-final" />  </ItemGroup></Project>
Poplar.Demo.Portal.csproj

Creates a new ItemGroup in the project file that holds references to other projects. Core 2.0 preview with <projectreference include= ". \poplar.demo.services\poplar.demo.services.csproj "/> Such a configuration node represents a project reference, the include value represents the path to the referenced project, and finally, after the UI layer configuration is complete, The contents of the entire project file are similar to the following.

<ProjectSDK= "Microsoft.NET.Sdk.Web">  <PropertyGroup>    <targetframework>netcoreapp2.0</targetframework>    <Mvcrazorcompileonpublish>True</Mvcrazorcompileonpublish>    <Assettargetfallback>$ (assettargetfallback);p ortable-net45+win8+wp8+wpa81;</Assettargetfallback>    <Usersecretsid>aspnet-poplar.demo.portal-d34843e8-3d14-4277-8ced-4d3bcacbf88c</Usersecretsid>  </PropertyGroup>  <ItemGroup>    <packagereferenceInclude= "Microsoft.AspNetCore.All"Version= "2.0.0-preview2-final" />  </ItemGroup>  <ItemGroup>    <dotnetclitoolreferenceInclude= "Microsoft.VisualStudio.Web.CodeGeneration.Tools"Version= "2.0.0-preview2-final" />  </ItemGroup>  <ItemGroup>    <projectreferenceInclude=".. \poplar.demo.models\poplar.demo.models.csproj "/>    <projectreferenceInclude=".. \poplar.demo.services\poplar.demo.services.csproj "/>  </ItemGroup></Project>
Poplar.Demo.Portal.csproj (after the business layer and models reference have been added)

There is a itemgroup and two references at the bottom. Use the same method to configure the references that other projects need to complete.

Next in the corresponding layer to add the corresponding class, in this I did not find any good method, all is a manually established CS file, and then manually put the using, namespace, class and other keywords written on. The call is then made at the UI layer.

New class for service tier:

 using   System;  using   Poplar.Demo.DataAccess;  namespace   poplar.demo.services{ public  class   Demoservice { pub Lic  string   do () { return   "  Demoservice.do ()   " + "  -->  "  + new   Demodal ().        Do (); }    }}
Poplar.Demo.Services.DemoService

New class of DataAccess layer:

using System; using Poplar.Demo.DataAccess; namespace poplar.demo.dataaccess{    publicclass  demodal    {        public  string  do ()        {            return'demodal.do ()' ;        }    }}
Poplar.Demo.DataAccess.DemoDal

Finally, it is called at the UI layer, and I choose the homecontroller that will be the default when creating an MVC project:

usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Threading.Tasks;usingMICROSOFT.ASPNETCORE.MVC;usingPoplar.Demo.Portal.Models;usingPoplar.Demo.Services;namespacepoplar.demo.portal.controllers{ Public classHomecontroller:controller { PublicIactionresult Index () {returnView (); }         PublicIactionresult About () {viewdata["Message"] =NewDemoservice ().            Do (); returnView (); }         PublicIactionresult Contact () {viewdata["Message"] ="Your Contact page."; returnView (); }         PublicIactionresult Error () {returnView (NewErrorviewmodel {RequestID = activity.current?. Id??httpcontext.traceidentifier}); }    }}
Poplar.Demo.Portal.Controllers.HomeController

When all the sample code is finished, select ". NET Core" When you press the "F5" button to choose Debug, and the popup prompts you to select the environment. The "Launch.json" file is opened, at this time the configurations node of the Launch.json file has three configurations by default, at this point we need to use the second one, that is

"Name": ". NET Core Launch (web)" This, remember this name, click "Debug" on the left menu bar

After you select the. NET Core Launch (Web) item, and then press F5 to start debugging, you are prompted that the Prelaunchtask "build" could not be found. This means that a build named task is executed before starting the debugging. Under the Configurations node of the Launch.json,

The name is the Prelaunchtask node inside the. NET Core Launch (Web) object, and the value is build, which is required to perform a compilation task before starting debugging, otherwise the code modified after the last compilation will not be executed. Click "Configure task Runner" in the Cue bar, select ". NET Core" In the box and add a task

After the task is added, you need to configure Launch.json so that you can find the corresponding. NET program when you start debugging. Configure the "CWD" node of the. NET Core Launch (Web) item first, which represents the path to execute the dotnet command, configures it as "${workspaceroot}/poplar.demo.portal", and then configures the. NET Core The program node of the Launch (Web) item enables the corresponding. NET programs to be started when debugging, configured as "Bin/debug/netcoreapp2.0/poplar.demo.portal.dll", After the Launch.json configuration is complete, the task needs to be configured to specify the object that corresponds to the task at build time. Add an item to the corresponding task

            "Options": {                "CWD": "${workspaceroot}/poplar.demo.portal"            }
Options

The above-mentioned ${workspaceroot} represents the root directory of the current workspace, which is the path to the folder that Vscode opened at the beginning, with the configuration completed Launch.json and Tasks.json 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.

Example code: Poplar.Demo.rar

In the process of using VS code, it is found that smart hints and formatting are not used, there is a class is the creation of more trouble, there are clear friends to help guide, thank you first.

All of the above is my own opinion, please point out, thank you.

  

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

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.