Now the C#-based development framework .net core has been open source and supports cross-platform operation. .net core is very mature and excellent. I recommend everyone to use it. So I also tried to use the Raspberry Pi for deployment and later use it for in-depth Internet of Things Some of the operations, the current example uses .net core2.2, of course, now the .net core3.1 operation is also similar, and then share experience, bring surprises to c# developers, and jointly contribute to the development of c# !
I bought a Raspberry Pi 3b+ from Taobao (more than two hundred yuan), bought it and tossed for an hour, assembled and disassembled twice, after the perfect assembly, it became like this. Pay attention to the position of the TF card.
The first step is to install the system first (take out the tf card that came with the Raspberry Pi and install the system through the following operations)
1. Download the system from the official website https://www.raspberrypi.org/downloads/raspbian/
Choose Raspbian Stretch with desktop and recommended software, download it back, and unzip it for use in step 3
2. Download tf card formatting tool: SD card formatter
Download address: https://www.sdcard.org/downloads/formatter_4/eula_windows/index.html , open the website and press Accept to start downloading
Insert the tf card into the computer and format the tf card
3. Download the system writing tool win32diskimager
Download link: https://jaist.dl.sourceforge.net/project/win32diskimager/Archive/win32diskimager-1.0.0-install.exe
Insert the tf card into the computer, open it and select the decompressed system image file downloaded in step 1, select the tf card for the device, and press "write"
4. Configure the wireless network (if you can plug in the network cable, you don't need to configure it)
Then set up the Internet, create a new wpa_supplicant.conf
file on the tf card, which is the current boot disk , and write the Internet configuration command:
country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={ ssid="iPhone hotspot wifi name" psk="104104104 wifi password"
key_mgmt=WPA-PSK
priority=1
}
network={ ssid="common router wifi name" psk="104104104 wifi password" priority=2 }
If you are using a mobile phone hotspot, add another line key_mgmt=WPA-PSK in the network
Here attention must be under the boot directory created this conf, otherwise the system will not start installed wifi connection.
5. To open the SSH connection function, you need to add an empty file named "ssh" in the root directory of the tf card. Remember that the file name without extension has only three letters of ssh
Congratulations, the system has been configured here. Insert the tf card into the small slot on the Raspberry Pi circuit board and connect it to the power supply! ! (With picture)
2. SSL connection to Raspberry Pi
Preparations: Insert the tf card into the slot of the Raspberry Pi. If the wireless network is not configured, plug in the network cable, plug in the power supply, turn on the power, and the machine will start automatically.
Go back to your own window computer
1. Download a computer IP viewing tool Advanced_IP_Scanner.rar
Check the name of a computer named Raspberry Pi, which is the Raspberry Pi, and write down his IP address
2. Download a ssh operation tool (cracked version) Xshell 6 and file upload tool Xftp 6, these two are used together
Open the Xsheel6 tool, enter the IP of the Raspberry Pi, connect, and enter the default username pi and password raspberry after the connection is successful
Congratulations, you have successfully connected the Raspberry Pi to this point, and the deployment process will begin.
Three, build the .net core operating environment
1) Download and install SDK or Runtime: official address: https://www.microsoft.com/net/download/linux
Here is the simplest method to download and install, copy the download link address directly, and execute the following three commands in order:
Download runtime
curl -sSL -o dotnet.tar.gz https://download.visualstudio.microsoft.com/download/pr/5ca39d79-c65c-4c03-bba1-e904c7255c44/4fb100d62b0a3fff5fbf5dd24f761d71/aspnetcore-runtime-2.2.2-linux-arm.tar.gz
Extract content
sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
Settings can execute DotNet:
sudo ln -s /opt/dotnet/dotnet /usr/local/bin
2) Test: dotnet --help or dotnet --info. If successful, it will look like the following figure
Third, deploy the .net core console program
1. Use VS to write a NetCore2.2 console program on your computer. I assume I will write Helloworld.
Publish project
Use xshell6+xftp6 to upload the program
Back in xshell, enter the directory and enter: cd consolepublish, run the program: dotnet ConsoleApp1.dll
Output hello world, success!
Fourth, deploy the .net core website program
1. Publish your project
Use xshell6+xftp6 to upload the program
Back to xshell6, f enter the directory and enter: cd webPublish Run the program: dotnet test2.2.dll
The visit address is: http://localhost:5000, try to visit as follows:
The html code has appeared, proved to be accessible, and the deployment was successful! However, it can only be accessed in Shumei Pie by default. If the web is to be accessed from other intranet computers through IP, it cannot be accessed. The following settings are required
How to access the web program of Raspberry Pi through the IP of the office domain name or the external network?
After the deployment of the project created by Net Core by default is completed, it can only be accessed inside the machine, and the external IP cannot be opened. This can be achieved by configuring Nginx. You can also modify Program.cs
public static void Main(string[] args) {
CreateWebHostBuilder(args).Build().Run();
}
By default, it can only be accessed locally on the Raspberry Pi through localhost:5000
To
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
Note: UseUrls(http://*:5000) can access the program through IP. Such as http://192.168.1.101:5000
Students who feel good, remember to like it!